-
-
Notifications
You must be signed in to change notification settings - Fork 40
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 defer operator #330
base: main
Are you sure you want to change the base?
Add defer operator #330
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Store, Unit, merge, sample } from 'effector'; | ||
import { combineEvents } from '../combine-events'; | ||
import { not } from '../not'; | ||
|
||
export interface DeferArgs { | ||
clock: Unit<any>; | ||
until: Store<boolean>; | ||
} | ||
|
||
export const defer = (args: DeferArgs) => { | ||
const { clock, until: condition } = args; | ||
|
||
const calledAfterCondition = sample({ | ||
clock: clock, | ||
filter: condition, | ||
}); | ||
|
||
const calledBeforeCondition = sample({ | ||
clock: clock, | ||
filter: not(condition), | ||
}); | ||
|
||
return merge([ | ||
calledAfterCondition, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if condition is already true then call event immediately |
||
combineEvents({ | ||
events: [calledBeforeCondition, condition.updates.filter({ fn: Boolean })], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if condition is not true yet then defer event call until condition became true |
||
reset: condition.updates.filter({ fn: (value) => !value }), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if condition became true and then false we should reset combine events since event call should be deferred until condition will be true again |
||
}), | ||
]); | ||
}; |
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.
there could be 4 cases:
IMPORTANT: if clock will be called more than 1 time only the last event will be deferred and called. Other events will be ignored