Spaces:
Running
Running
Download src/single-flight.ts from Shoraky/Sporalize-api: direct link, hf CLI and curl.
- Browser
- Download file 534 Bytes
-
https://huggingface.co/spaces/Shoraky/Sporalize-api/resolve/main/src/single-flight.ts
- Command line
-
hf download hf://spaces/Shoraky/Sporalize-api/src/single-flight.ts
-
curl -L -o single-flight.ts https://huggingface.co/spaces/Shoraky/Sporalize-api/resolve/main/src/single-flight.ts
534 Bytes
| /** Share only concurrent work. Completed values and failures are never cached. */ | |
| export function singleFlight<Key, Value>(load: (key: Key) => PromiseLike<Value>) { | |
| const pending = new Map<Key, Promise<Value>>(); | |
| return (key: Key): Promise<Value> => { | |
| const existing = pending.get(key); | |
| if (existing) return existing; | |
| const request = Promise.resolve().then(() => load(key)).finally(() => { | |
| if (pending.get(key) === request) pending.delete(key); | |
| }); | |
| pending.set(key, request); | |
| return request; | |
| }; | |
| } | |