calls/awaits any .init in the result from the factory
(instance: WithInit<ReturnType<TFactory>> | Promise<WithInit<ReturnType<TFactory>>>, args: any[])
| 11 | export function Factory<TFactory extends (...args: Parameters<TFactory>) => ReturnType<TFactory>>(factory: TFactory): new (...args: Parameters<TFactory>) => Promise<Awaited<ReturnType<TFactory>>> { |
| 12 | /** calls/awaits any .init in the result from the factory */ |
| 13 | async function initialize(instance: WithInit<ReturnType<TFactory>> | Promise<WithInit<ReturnType<TFactory>>>, args: any[]) { |
| 14 | if (instance !== null && instance !== undefined) { |
| 15 | if (is.promise(instance)) { |
| 16 | instance = await instance; |
| 17 | } |
| 18 | |
| 19 | // if .init is a function, call it, if it's a promise, await it |
| 20 | const pInit = typeof instance.init === 'function' ? instance.init(args) : instance.init; |
| 21 | if (is.promise(pInit)) { |
| 22 | await pInit; |
| 23 | } |
| 24 | } |
| 25 | return instance; |
| 26 | } |
| 27 | |
| 28 | class AsyncFactory extends Promise<ReturnType<TFactory>> { |
| 29 | protected factory: TFactory; |
no test coverage detected