Comparing version 0.14.0 to 0.100.0
{ | ||
"name": "patronum", | ||
"version": "0.14.0", | ||
"version": "0.100.0", | ||
"description": "☄️ Effector utility library delivering modularity and convenience", | ||
@@ -62,5 +62,4 @@ "main": "index.js", | ||
"dependencies": { | ||
"effector-debounce": "^1.1.0", | ||
"effector-throttle": "^1.1.0" | ||
"effector-debounce": "^1.1.0" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# effecto patronum ✨ | ||
# ☄️ effecto patronum ✨ | ||
@@ -15,3 +15,5 @@ [](http://commitizen.github.io/cz-cli/) [](https://conventionalcommits.org) [](http://prettier.io)  | ||
- [Every](#every) | ||
- [Reshape](#reshape) | ||
- [Some](#some) | ||
- [SplitMap](#splitmap) | ||
- [Spread](#spread) | ||
@@ -21,7 +23,2 @@ - [Status](#status) | ||
--- | ||
- [Reshape](#reshape) | ||
- [SplitMap](#splitmap) | ||
## Usage | ||
@@ -114,3 +111,3 @@ | ||
const target = throttle(trigger, 200); | ||
const target = throttle({ source: trigger, timeout: 200 }); | ||
@@ -239,8 +236,4 @@ target.watch((payload) => console.info('throttled', payload)); | ||
--- | ||
## [Reshape](/reshape 'Documentation') | ||
> No tests yet | ||
```ts | ||
@@ -265,4 +258,2 @@ import { createStore } from 'effector'; | ||
> No tests yet | ||
```ts | ||
@@ -269,0 +260,0 @@ import { createEvent } from 'effector'; |
@@ -1,3 +0,13 @@ | ||
import { createThrottle } from 'effector-throttle'; | ||
import { Unit, Event } from 'effector'; | ||
export const throttle = createThrottle; | ||
export function throttle<T>(_: { | ||
source: Unit<T>; | ||
timeout: number; | ||
name?: string; | ||
}): Event<T>; | ||
export function throttle<T, R extends Unit<T>>(_: { | ||
source: Unit<T>; | ||
timeout: number; | ||
target: R; | ||
name?: string; | ||
}): R; |
@@ -1,5 +0,51 @@ | ||
const { createThrottle } = require('effector-throttle'); | ||
const { createEffect, createEvent, guard, is, sample } = require('effector'); | ||
const { readConfig } = require('../library'); | ||
module.exports = { | ||
throttle: createThrottle, | ||
}; | ||
function throttle(argument) { | ||
const { source, timeout, target, sid, loc, name } = readConfig(argument, [ | ||
'source', | ||
'timeout', | ||
'target', | ||
'loc', | ||
'name', | ||
'sid', | ||
]); | ||
if (!is.unit(source)) | ||
throw new TypeError('callee must be unit from effector'); | ||
if (typeof timeout !== 'number' || timeout < 0) | ||
throw new Error('timeout must be positive number or zero'); | ||
const actualName = name || source.shortName || 'unknown'; | ||
const tick = | ||
target || | ||
createEvent({ | ||
name: `${actualName}ThrottleTick`, | ||
loc, | ||
}); | ||
const timerFx = createEffect({ | ||
name: `${actualName}ThrottleTimer`, | ||
sid, | ||
loc, | ||
handler: () => new Promise((resolve) => setTimeout(resolve, timeout)), | ||
}); | ||
guard({ | ||
source, | ||
filter: timerFx.pending.map((pending) => !pending), | ||
target: timerFx, | ||
}); | ||
sample({ | ||
source, | ||
clock: timerFx.done, | ||
target: tick, | ||
}); | ||
return tick; | ||
} | ||
module.exports = { throttle }; |
@@ -7,6 +7,23 @@ # Patronum/Throttle | ||
## Example | ||
## `throttle({ source, timeout })` | ||
### Formulae | ||
```ts | ||
result = throttle({ source, timeout }); | ||
``` | ||
### Usage | ||
Create event that should be throttled: | ||
```ts | ||
import { createEvent } from 'effector'; | ||
const someHappened = createEvent<number>(); | ||
``` | ||
Create throttled event from it: | ||
```ts | ||
import { throttle } from 'patronum/throttle'; | ||
@@ -16,5 +33,11 @@ | ||
const someHappened = createEvent<number>(); | ||
const throttled = createThrottle(someHappened, THROTTLE_TIMEOUT_IN_MS); | ||
const throttled = throttle({ | ||
source: someHappened, | ||
timeout: THROTTLE_TIMEOUT_IN_MS, | ||
}); | ||
``` | ||
When you call `someHappened` it will make throttled call the `throttled` event: | ||
```ts | ||
throttled.watch((payload) => { | ||
@@ -28,2 +51,49 @@ console.info('someHappened now', payload); | ||
someHappened(4); | ||
// after 200 ms after first call | ||
// => someHappened now 4 | ||
``` | ||
Also you can use `Effect` and `Store` as trigger. `throttle` always returns `Event`: | ||
```ts | ||
const event = createEvent<number>(); | ||
const throttledEvent: Event<number> = throttle({ source: event, timeout: 100 }); | ||
const fx = createEffect<number, void>(); | ||
const throttledEffect: Event<number> = throttle({ source: fx, timeout: 100 }); | ||
const $store = createStore<number>(0); | ||
const throttledStore: Event<number> = throttle({ | ||
source: $store, | ||
timeout: 100, | ||
}); | ||
``` | ||
## `throttle({ source, timeout, target })` | ||
### Formulae | ||
```ts | ||
throttle({ source, timeout, target }); | ||
``` | ||
### Usage | ||
```ts | ||
const change = createEvent(); | ||
const $source = createStore(0).on(change, (state) => state + 1); | ||
const $dumped = createStore(0); | ||
$dumped.watch((payload) => { | ||
localStorage.setItem('dump', JSON.stringify(payload)); | ||
}); | ||
throttle({ source: $source, timeout: 40, target: $dumped }); | ||
change(); | ||
change(); | ||
change(); | ||
// after 40ms after first call, 3 will be saved to localStorage | ||
``` |
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
40589
2
401
0
275
- Removedeffector-throttle@^1.1.0
- Removedeffector-throttle@1.1.0(transitive)