Skip to content

Instantly share code, notes, and snippets.

@cowboyd
Created February 18, 2021 12: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/23bc78afcf716b7d8a79d8ea98d27e4b to your computer and use it in GitHub Desktop.
Save cowboyd/23bc78afcf716b7d8a79d8ea98d27e4b to your computer and use it in GitHub Desktop.
map a key-value object to an operation
export function* map<A>(slice: Slice<Record<string, A>>, operation: (slice: Slice<A>) => Operation<void>): Operation<void> {
let contexts = new Map<string,Context>();
function* synchronize(record: Record<string, A>) {
let keep = new Set<string>();
for (let key of Object.keys(record)) {
if (!contexts.has(key)) {
contexts.set(key, yield spawn(operation(slice.slice(key))))
}
keep.add(key);
}
for (let [key, context] of contexts.entries()) {
if (!keep.has(key)) {
context.halt();
}
}
}
yield synchronize(slice.get());
yield subscribe(slice).forEach(synchronize);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment