mirror of
https://github.com/morten-olsen/morten-olsen.github.io.git
synced 2026-02-08 01:46:28 +01:00
init
This commit is contained in:
27
bin/utils/observable/index.ts
Normal file
27
bin/utils/observable/index.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { Observable } from "../../observable";
|
||||
|
||||
const forEach = async <T extends Observable<any[]>>(
|
||||
observable: T,
|
||||
fn: (
|
||||
value: T extends Observable<infer U>
|
||||
? U extends Array<infer A>
|
||||
? A
|
||||
: never
|
||||
: never
|
||||
) => Promise<void>
|
||||
) => {
|
||||
const knownValues = new Set();
|
||||
const update = async () => {
|
||||
for (let value of await observable.data) {
|
||||
if (knownValues.has(value)) {
|
||||
continue;
|
||||
}
|
||||
await fn(value);
|
||||
knownValues.add(value);
|
||||
}
|
||||
};
|
||||
await update();
|
||||
observable.subscribe(update);
|
||||
};
|
||||
|
||||
export { forEach };
|
||||
Reference in New Issue
Block a user