forked from openclaw/openclaw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlazy-runtime.ts
More file actions
44 lines (40 loc) · 1.59 KB
/
Copy pathlazy-runtime.ts
File metadata and controls
44 lines (40 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
export function createLazyRuntimeSurface<TModule, TSurface>(
importer: () => Promise<TModule>,
select: (module: TModule) => TSurface,
): () => Promise<TSurface> {
let cached: Promise<TSurface> | null = null;
return () => {
cached ??= importer().then(select);
return cached;
};
}
/** Cache the raw dynamically imported runtime module behind a stable loader. */
export function createLazyRuntimeModule<TModule>(
importer: () => Promise<TModule>,
): () => Promise<TModule> {
return createLazyRuntimeSurface(importer, (module) => module);
}
/** Cache a single named runtime export without repeating a custom selector closure per caller. */
export function createLazyRuntimeNamedExport<TModule, const TKey extends keyof TModule>(
importer: () => Promise<TModule>,
key: TKey,
): () => Promise<TModule[TKey]> {
return createLazyRuntimeSurface(importer, (module) => module[key]);
}
export function createLazyRuntimeMethod<TSurface, TArgs extends unknown[], TResult>(
load: () => Promise<TSurface>,
select: (surface: TSurface) => (...args: TArgs) => TResult,
): (...args: TArgs) => Promise<Awaited<TResult>> {
const invoke = async (...args: TArgs): Promise<Awaited<TResult>> => {
const method = select(await load());
return await method(...args);
};
return invoke;
}
export function createLazyRuntimeMethodBinder<TSurface>(load: () => Promise<TSurface>) {
return function <TArgs extends unknown[], TResult>(
select: (surface: TSurface) => (...args: TArgs) => TResult,
): (...args: TArgs) => Promise<Awaited<TResult>> {
return createLazyRuntimeMethod(load, select);
};
}