From be72e8e0d56412c36613281cfa9db29b82ca9be6 Mon Sep 17 00:00:00 2001 From: Sergey Sova Date: Thu, 23 Apr 2020 21:18:32 +0300 Subject: [PATCH] feat(throttle): introduce throttle --- README.md | 6 +++--- package.json | 3 ++- throttle/index.d.ts | 3 +++ throttle/index.js | 5 +++++ throttle/readme.md | 26 ++++++++++++++++++++++++++ yarn.lock | 5 +++++ 6 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 throttle/index.d.ts create mode 100644 throttle/index.js create mode 100644 throttle/readme.md diff --git a/README.md b/README.md index c20a1469..2675ab05 100644 --- a/README.md +++ b/README.md @@ -60,16 +60,16 @@ trigger(4); // => debounced 4 ``` -## ⏺ Throttle +## [🧁 Throttle](/throttle 'Documentation') ```ts import { createEvent } from 'effector'; -import { createThrottle } from 'patronum/throttle'; +import { throttle } from 'patronum/throttle'; // You should call this event const trigger = createEvent(); -const target = createThrottle(trigger, 200); +const target = throttle(trigger, 200); target.watch((payload) => console.info('throttled', payload)); diff --git a/package.json b/package.json index 8d791fb8..d649339c 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,7 @@ } }, "dependencies": { - "effector-debounce": "^1.1.0" + "effector-debounce": "^1.1.0", + "effector-throttle": "^1.1.0" } } diff --git a/throttle/index.d.ts b/throttle/index.d.ts new file mode 100644 index 00000000..04c731af --- /dev/null +++ b/throttle/index.d.ts @@ -0,0 +1,3 @@ +import { createThrottle } from 'effector-throttle'; + +export const throttle = createThrottle; diff --git a/throttle/index.js b/throttle/index.js new file mode 100644 index 00000000..6dcfff25 --- /dev/null +++ b/throttle/index.js @@ -0,0 +1,5 @@ +const { createThrottle } = require('effector-throttle'); + +module.exports = { + throttle: createThrottle, +}; diff --git a/throttle/readme.md b/throttle/readme.md new file mode 100644 index 00000000..32edd403 --- /dev/null +++ b/throttle/readme.md @@ -0,0 +1,26 @@ +# Patronum/Throttle + +```ts +import { throttle } from 'patronum/throttle'; +``` + +## Example + +```ts +import { createEvent } from 'effector'; +import { throttle } from 'patronum/throttle'; + +const THROTTLE_TIMEOUT_IN_MS = 200; + +const someHappened = createEvent(); +const throttled = createThrottle(someHappened, THROTTLE_TIMEOUT_IN_MS); + +throttled.watch((payload) => { + console.info('someHappened now', payload); +}); + +someHappened(1); +someHappened(2); +someHappened(3); +someHappened(4); +``` diff --git a/yarn.lock b/yarn.lock index 275b8871..0c30dfd5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1821,6 +1821,11 @@ effector-debounce@^1.1.0: resolved "https://registry.yarnpkg.com/effector-debounce/-/effector-debounce-1.1.0.tgz#e6aa74efd302d480f57b2e945e71ab34772ba360" integrity sha512-UfJLUQx2fIjNIGx9kmPvmCjClInXvJP+jmREpVMamLQkEoMw3PqdnzoLUqFPJQI/tXGD9LQ1OYrL9eANVpIAtg== +effector-throttle@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/effector-throttle/-/effector-throttle-1.1.0.tgz#8d3b4b7c24ed60be746fdfdfc2bf627f0c9f6983" + integrity sha512-hkFwr8W6QZMnvxb8XIqwFyQQCT4kwzHV2eaoVCGfRSixwPs0GrTcZIrXTg03pTHRLXn+4x2owRD/OXoAfTQTYQ== + effector@^20.7.0: version "20.14.0" resolved "https://registry.yarnpkg.com/effector/-/effector-20.14.0.tgz#890ca40f2a7cf9041e6f22b7baf8c4273042dbcb"