Provides time-based store transformations such as debounce and throttle.
# pnpm
$ pnpm add @crikey/stores-temporal
# npm
$ npm add @crikey/stores-temporal
# yarn
$ yarn add @crikey/stores-temporal
Throttle changes from store
.
Changes from store
beyond the frequency specified by period_ms
will be discarded.
+---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+
| | | | | | | | | | | | | | | |
+--+ 1 +--+ 2 +--+ 3 +--+ 4 +-----------------+ 5 +--+ 6 +--+ 7 +--+ 8 +-------------->
| | | | | | | | | | | | | | | |
+---+ +---+ +---+ +---+ +---+ +---+ +---+ +---+
| | | | | |
| +-----+ +----+ | +-----+ +----+
| | | | | |
v v v v v v
+---+ +---+ +---+ +---+ +---+ +---+
| | | | | | | | | | | |
+--+ 1 +--------+ 2 +--------+ 4 +------------+ 5 +--------+ 6 +--------+ 8 +--------->
| | | | | | | | | | | |
+---+ +---+ +---+ +---+ +---+ +---+
Example:
From ./packages/stores-temporal/examples/throttle.test.ts#9~42
const wait = (ms: number) => new Promise(resolve => {
setTimeout(resolve, ms);
})
const store = writable(trigger_strict_not_equal, 1);
const throttled = throttle(store, 500);
throttled.subscribe(value => console.log(value));
await wait(10);
store.set(2);
await wait(10);
store.set(3);
await wait(10);
store.set(4);
await wait(500);
store.set(5);
await wait(200);
store.set(6);
await wait(200);
store.set(7);
await wait(200);
store.set(8);
await wait(600);
// > 1
// > 4
// > 7
// > 8
Generated using TypeDoc
Debounce changes from
store
.Changes from
store
will be delayed bydelay_ms
. Fresh changes fromstore
will cancel any pending events, effectively discarding any changes with a higher frequency thandelay_ms
.Example:
From ./packages/stores-temporal/examples/debounce.test.ts#9~53