(initializer: () => T)
| 5 | * {@link https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/util/lazy-init/lazy-init.ts} |
| 6 | */ |
| 7 | export function lazyInit<T extends object>(initializer: () => T): T { |
| 8 | let object: T | null = null |
| 9 | |
| 10 | const initializeObject = () => { |
| 11 | if (!object) { |
| 12 | object = untracked(() => initializer()) |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | queueMicrotask(() => initializeObject()) |
| 17 | |
| 18 | const table = () => {} |
| 19 | |
| 20 | return new Proxy<T>(table as T, { |
| 21 | apply(target: T, thisArg: any, argArray: any[]): any { |
| 22 | initializeObject() |
| 23 | if (typeof object === 'function') { |
| 24 | return Reflect.apply(object, thisArg, argArray) |
| 25 | } |
| 26 | return Reflect.apply(target as any, thisArg, argArray) |
| 27 | }, |
| 28 | get(_, prop, receiver) { |
| 29 | initializeObject() |
| 30 | return Reflect.get(object as T, prop, receiver) |
| 31 | }, |
| 32 | has(_, prop) { |
| 33 | initializeObject() |
| 34 | return Reflect.has(object as T, prop) |
| 35 | }, |
| 36 | ownKeys() { |
| 37 | initializeObject() |
| 38 | return Reflect.ownKeys(object as T) |
| 39 | }, |
| 40 | getOwnPropertyDescriptor() { |
| 41 | return { |
| 42 | enumerable: true, |
| 43 | configurable: true, |
| 44 | } |
| 45 | }, |
| 46 | }) |
| 47 | } |
no test coverage detected