Created
February 18, 2021 12:29
-
-
Save cowboyd/23bc78afcf716b7d8a79d8ea98d27e4b to your computer and use it in GitHub Desktop.
map a key-value object to an operation
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
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