Last active
July 17, 2023 14:29
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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