Skip to content

Commit

Permalink
feat: add patronum defer
Browse files Browse the repository at this point in the history
  • Loading branch information
velialiev committed May 16, 2024
1 parent aa7b1eb commit 7f8674a
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/defer/index.ts
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,
combineEvents({
events: [calledBeforeCondition, condition.updates.filter({ fn: Boolean })],
reset: condition.updates.filter({ fn: (value) => !value }),
}),
]);
};

0 comments on commit 7f8674a

Please sign in to comment.