-
-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add handling of leading
/trailing
edges
#283
base: main
Are you sure you want to change the base?
Conversation
Run & review this pull request in StackBlitz Codeflow. |
🚛 size-compare reportComparing 786f7452...786f7452 Files wasn't changed
|
Awesome! Could you also add some tests to https://github.com/effector/patronum/blob/main/src/throttle/throttle.fork.test.ts and some type tests to https://github.com/effector/patronum/blob/main/test-typings/throttle.ts ? |
|
||
const triggerTick = createEvent<T>(); | ||
|
||
const $leading = toStoreBoolean(leading, '$leading', false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In lodash
both leading
and trailing
are true
by default. Why not make the same?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't want to change the default behavior making it breaking change, but I agree, I'd rather made them both true by default
const $leading = toStoreBoolean(leading, '$leading', false); | ||
const $trailing = toStoreBoolean(trailing, '$trailing', true); | ||
|
||
const $neverCalled = createStore(true).on(target, () => false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one should be reset every time when timeout finishes, I guess
const trigger = createEvent()
const throttled = throttle({ source: trigger, timeout: 100, leading: true })
trigger(1)
trigger(2)
trigger(3)
await wait(110)
trigger(4)
throttled
should be called with 4
immediately
leading
/trailing
edges
|
||
const triggerTick = createEvent<T>(); | ||
|
||
const $leading = toStoreBoolean(leading, '$leading', false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const $leading = toStoreBoolean(leading, '$leading', false); | |
const $leading = toStoreBoolean(leading ?? false, '$leading'); |
|
||
const triggerTick = createEvent<T>(); | ||
|
||
const $leading = toStoreBoolean(leading, '$leading', false); | ||
const $trailing = toStoreBoolean(trailing, '$trailing', true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const $trailing = toStoreBoolean(trailing, '$trailing', true); | |
const $trailing = toStoreBoolean(trailing ?? true, '$trailing'); |
function toStoreBoolean( | ||
value: boolean | Store<boolean> | undefined, | ||
name: string, | ||
defaultValue: boolean, | ||
): Store<boolean> { | ||
if (is.store(value)) return value; | ||
if (typeof value === 'boolean') { | ||
return createStore(value, { name }); | ||
} else { | ||
return createStore(defaultValue, { name }); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function toStoreBoolean( | |
value: boolean | Store<boolean> | undefined, | |
name: string, | |
defaultValue: boolean, | |
): Store<boolean> { | |
if (is.store(value)) return value; | |
if (typeof value === 'boolean') { | |
return createStore(value, { name }); | |
} else { | |
return createStore(defaultValue, { name }); | |
} | |
} | |
function toStoreBoolean(value: boolean | Store<boolean> | undefined, name: string): Store<boolean> { | |
if (is.store(value)) return value; | |
return createStore(value, { name }); | |
} |
Description
Fixes #37
Checklist for a new method
src
directory inparam-case
src/method-name/index.ts
in ESModules export incamelCase
named exportsrc/method-name/method-name.test.ts
src/method-name/method-name.fork.test.ts
test-typings/method-name.ts
// @ts-expect-error
to mark expected type errorimport { expectType } from 'tsd'
to check expected return typesrc/method-name/readme.md
Patronum/MethodName
Motivation
,Formulae
,Arguments
andReturn
sections for each overloadExample
section for each overloadREADME.md
in the repository root- [MethodName](#methodname) - description.
## MethodName
[Method documentation & API](/src/method-name)
into section