( localProvider: TProvider, normalize: (input: TInput) => TProvider = (input) => input as unknown as TProvider, )
| 7 | }; |
| 8 | |
| 9 | export function createScopedProvider<TProvider, TInput = TProvider>( |
| 10 | localProvider: TProvider, |
| 11 | normalize: (input: TInput) => TProvider = (input) => input as unknown as TProvider, |
| 12 | ): ScopedProvider<TProvider, TInput> { |
| 13 | const storage = new AsyncLocalStorage<TProvider>(); |
| 14 | |
| 15 | return { |
| 16 | resolve(input) { |
| 17 | return input ? normalize(input) : (storage.getStore() ?? localProvider); |
| 18 | }, |
| 19 | async run(input, fn) { |
| 20 | if (!input) return await fn(); |
| 21 | return await storage.run(normalize(input), fn); |
| 22 | }, |
| 23 | hasScope() { |
| 24 | return Boolean(storage.getStore()); |
| 25 | }, |
| 26 | }; |
| 27 | } |
no outgoing calls
no test coverage detected