Skip to content

Instantly share code, notes, and snippets.

@cowboyd
Last active July 17, 2023 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cowboyd/9a67dc7a6574ec8ed412d5ea77bbed88 to your computer and use it in GitHub Desktop.
Save cowboyd/9a67dc7a6574ec8ed412d5ea77bbed88 to your computer and use it in GitHub Desktop.
Use a stream combinator to filter the actions from a channel and return a stream that is only
import { filter } from "effection";
export function* useActions(pattern: ActionPattern): Stream<AnyAction> {
let match = matcher(pattern);
let { output } = yield* ActionContext;
// return a subscription to the filtered actions.
return yield* filter(match)(output);
}
export function* take(pattern: ActionPattern): Operation<AnyAction> {
let actions = yield* useActions(pattern);
let first = yield* actions.next();
return first.value as AnyAction;
}
export function takeEvery<T>(
pattern: ActionPattern,
op: (action: AnyAction) => Operation<T>,
): Operation<Task<void>> {
return spawn(function*() {
let actions = yield* useActions(pattern);
let next = yield* actions.next();
while (!next.done) {
yield* spawn(() => op(next.value));
next = yield* actions.next();
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment