@spaceteams/warp 0.3.0 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +43 -16
- package/dist/index.mjs +69 -15
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,8 +1,37 @@
|
|
|
1
|
+
//#region src/component/component-meta.d.ts
|
|
2
|
+
type ComponentKind = "repo" | "service" | "usecase" | "client";
|
|
3
|
+
type ComponentMeta = {
|
|
4
|
+
name?: string;
|
|
5
|
+
kind?: ComponentKind;
|
|
6
|
+
tags?: string[];
|
|
7
|
+
};
|
|
8
|
+
//#endregion
|
|
1
9
|
//#region src/run.d.ts
|
|
10
|
+
type WarpMeta = {
|
|
11
|
+
component?: ComponentMeta;
|
|
12
|
+
componentPath?: string;
|
|
13
|
+
componentKey?: string;
|
|
14
|
+
};
|
|
2
15
|
type Run<AmbientContext, ScopeContext = unknown, RunOptions = unknown> = AmbientContext & {
|
|
3
16
|
run: <T>(options: RunOptions, inner: (app: Run<AmbientContext & ScopeContext, ScopeContext, RunOptions>) => Promise<T> | T) => Promise<T> | T;
|
|
17
|
+
warp?: WarpMeta;
|
|
4
18
|
};
|
|
5
19
|
//#endregion
|
|
20
|
+
//#region src/middleware.d.ts
|
|
21
|
+
type NoRunOptions = NonNullable<unknown>;
|
|
22
|
+
type NoScopeContext = NonNullable<unknown>;
|
|
23
|
+
type Middleware<AmbientContext, RunOptions = NoRunOptions, ScopeContext = NoScopeContext> = <T>(ctx: AmbientContext, options: Partial<RunOptions>, next: (ctx: AmbientContext & ScopeContext) => Promise<T> | T, warp?: WarpMeta | undefined) => Promise<T> | T;
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region src/component/infer.d.ts
|
|
26
|
+
type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
|
|
27
|
+
type AnyFactory = ((...args: never[]) => unknown) & {
|
|
28
|
+
meta?: ComponentMeta;
|
|
29
|
+
};
|
|
30
|
+
type InferCtx<T> = T extends ComponentFactory<infer Ctx, infer _SC, infer _RO, infer _Deps, infer _Out> ? Ctx : unknown;
|
|
31
|
+
type InferScopeContext<T> = T extends ComponentFactory<infer _Ctx, infer SC, infer _RO, infer _Deps, infer _Out> ? SC : NoScopeContext;
|
|
32
|
+
type InferRunOptions<T> = T extends ComponentFactory<infer _Ctx, infer _SC, infer RO, infer _Deps, infer _Out> ? RO : NoRunOptions;
|
|
33
|
+
type InferOut<T> = T extends ComponentFactory<infer _Ctx, infer _SC, infer _RO, infer _Deps, infer Out> ? Out : never;
|
|
34
|
+
//#endregion
|
|
6
35
|
//#region src/component/index.d.ts
|
|
7
36
|
type NoDeps = NonNullable<unknown>;
|
|
8
37
|
declare const COMPONENT: unique symbol;
|
|
@@ -13,12 +42,6 @@ type ComponentRef<Ctx, ScopeContext, RunOptions, Out> = {
|
|
|
13
42
|
readonly __runOptions?: RunOptions;
|
|
14
43
|
readonly __out?: Out;
|
|
15
44
|
};
|
|
16
|
-
type ComponentKind = "repo" | "service" | "usecase" | "client";
|
|
17
|
-
type ComponentMeta = {
|
|
18
|
-
name?: string;
|
|
19
|
-
kind?: ComponentKind;
|
|
20
|
-
tags?: string[];
|
|
21
|
-
};
|
|
22
45
|
type ComponentFactoryFn<Ctx, ScopeContext, RunOptions, Deps, Out> = (ctx: Run<Ctx & Deps, ScopeContext, RunOptions>) => Out;
|
|
23
46
|
type ComponentFactory<Ctx, ScopeContext, RunOptions, Deps, Out> = ComponentFactoryFn<Ctx, ScopeContext, RunOptions, Deps, Out> & {
|
|
24
47
|
meta?: ComponentMeta;
|
|
@@ -37,11 +60,6 @@ type ComponentDefinition<Ctx, ScopeContext, RunOptions, Deps, Out> = {
|
|
|
37
60
|
declare function brandComponent<T extends object, Ctx, ScopeContext, RunOptions, Out>(obj: T): T & ComponentRef<Ctx, ScopeContext, RunOptions, Out>;
|
|
38
61
|
declare function isComponent(value: unknown): value is Component<unknown, unknown, unknown, unknown, unknown>;
|
|
39
62
|
//#endregion
|
|
40
|
-
//#region src/middleware.d.ts
|
|
41
|
-
type NoRunOptions = NonNullable<unknown>;
|
|
42
|
-
type NoScopeContext = NonNullable<unknown>;
|
|
43
|
-
type Middleware<AmbientContext, RunOptions = NoRunOptions, ScopeContext = NoScopeContext> = <T>(ctx: AmbientContext, options: Partial<RunOptions>, next: (ctx: AmbientContext & ScopeContext) => Promise<T> | T) => Promise<T> | T;
|
|
44
|
-
//#endregion
|
|
45
63
|
//#region src/explain/index.d.ts
|
|
46
64
|
type ExplainResult = {
|
|
47
65
|
name: string | undefined;
|
|
@@ -51,11 +69,11 @@ type ExplainResult = {
|
|
|
51
69
|
};
|
|
52
70
|
//#endregion
|
|
53
71
|
//#region src/lazy.d.ts
|
|
54
|
-
type Lazy<T> = () => T;
|
|
72
|
+
type Lazy<T, Args extends unknown[] = []> = (...args: Args) => T;
|
|
55
73
|
declare const Lazy: {
|
|
56
|
-
cached<T>(factory:
|
|
57
|
-
and<S, T>(l: Lazy<S>, r: Lazy<T>): Lazy<S & T>;
|
|
58
|
-
map<S, T>(l: Lazy<S>, fn: (v: S) => T): Lazy<T>;
|
|
74
|
+
cached<T, Args extends unknown[] = []>(factory: Lazy<T, Args>): Lazy<T, Args>;
|
|
75
|
+
and<S, T, Args extends unknown[] = []>(l: Lazy<S, Args>, r: Lazy<T, Args>): Lazy<S & T, Args>;
|
|
76
|
+
map<S, T, Args extends unknown[] = []>(l: Lazy<S, Args>, fn: (v: S) => T): Lazy<T, Args>;
|
|
59
77
|
};
|
|
60
78
|
//#endregion
|
|
61
79
|
//#region src/runtime/runtime.d.ts
|
|
@@ -71,9 +89,13 @@ declare class Runtime<Ctx, ActualContext extends Ctx, ScopeContext, RunOptions,
|
|
|
71
89
|
require<Extension>(): Runtime<Ctx, ActualContext, ScopeContext, RunOptions, SafeIntersect<Requirements, Extension>>;
|
|
72
90
|
resolve: <Deps, Out>(component: Component<SafeIntersect<Requirements, ActualContext>, ScopeContext, RunOptions, Deps, Out>, ...requirements: OptionalArg<Requirements>) => Out | Promise<Out>;
|
|
73
91
|
get component(): <Deps, F extends ComponentFactory<SafeIntersect<Requirements, ActualContext>, ScopeContext, RunOptions, Deps, ReturnType<F>>>(factory: F, deps?: { [T in keyof Deps]: ComponentInput<SafeIntersect<Requirements, ActualContext>, ScopeContext, RunOptions, Deps[T]> } | undefined, meta?: ComponentMeta | undefined) => Component<SafeIntersect<Requirements, ActualContext>, ScopeContext, RunOptions, Deps, ReturnType<F>>;
|
|
92
|
+
get singleton(): <Deps, F extends ComponentFactory<SafeIntersect<Requirements, ActualContext>, {}, RunOptions, Deps, ReturnType<F>>>(factory: F, deps?: { [T in keyof Deps]: ComponentInput<SafeIntersect<Requirements, ActualContext>, {}, RunOptions, Deps[T]> } | undefined, meta?: ComponentMeta | undefined) => Component<SafeIntersect<Requirements, ActualContext>, {}, RunOptions, Deps, ReturnType<F>>;
|
|
74
93
|
get classComponent(): <Deps, Ctor extends (new (deps: Run<SafeIntersect<Requirements, ActualContext> & Deps, ScopeContext, RunOptions>) => InstanceType<Ctor>) & {
|
|
75
94
|
meta?: ComponentMeta | undefined;
|
|
76
95
|
}>(ctor: Ctor, deps?: { [K in keyof Deps]: ComponentInput<SafeIntersect<Requirements, ActualContext>, ScopeContext, RunOptions, Deps[K]> } | undefined, meta?: ComponentMeta | undefined) => Component<SafeIntersect<Requirements, ActualContext>, ScopeContext, RunOptions, Deps, InstanceType<Ctor>>;
|
|
96
|
+
get classSingleton(): <Deps, Ctor extends (new (deps: Run<SafeIntersect<Requirements, ActualContext> & Deps, {}, RunOptions>) => InstanceType<Ctor>) & {
|
|
97
|
+
meta?: ComponentMeta | undefined;
|
|
98
|
+
}>(ctor: Ctor, deps?: { [K in keyof Deps]: ComponentInput<SafeIntersect<Requirements, ActualContext>, {}, RunOptions, Deps[K]> } | undefined, meta?: ComponentMeta | undefined) => Component<SafeIntersect<Requirements, ActualContext>, {}, RunOptions, Deps, InstanceType<Ctor>>;
|
|
77
99
|
explain<Deps, Out>(component: Component<SafeIntersect<Requirements, ActualContext>, ScopeContext, RunOptions, Deps, Out>, format: "native", showMeta?: boolean): ExplainResult;
|
|
78
100
|
explain<Deps, Out>(component: Component<SafeIntersect<Requirements, ActualContext>, ScopeContext, RunOptions, Deps, Out>, format: "ascii", showMeta?: boolean): string;
|
|
79
101
|
explain<Deps, Out>(component: Component<SafeIntersect<Requirements, ActualContext>, ScopeContext, RunOptions, Deps, Out>, format: "mermaid", showMeta?: boolean): string;
|
|
@@ -103,6 +125,11 @@ type Client<Ctx, ScopeContext, Out, RunOptions> = ComponentFactory<Ctx, ScopeCon
|
|
|
103
125
|
declare function client<Ctx, Out, RunOptions = NoRunOptions, ScopeContext = NoScopeContext>(options: Omit<ComponentMeta, "kind">, fn: (app: Run<Ctx, ScopeContext, RunOptions>) => Out): Client<Ctx, ScopeContext, Out, RunOptions>;
|
|
104
126
|
type InferClient<T> = T extends Client<infer _Ctx, infer _ScopeContext, infer Out, infer _RunOptions> ? Out : never;
|
|
105
127
|
//#endregion
|
|
128
|
+
//#region src/semantic/combine.d.ts
|
|
129
|
+
type CombinedOutput<T extends Record<string, AnyFactory>> = { [K in keyof T]: ReturnType<T[K]> };
|
|
130
|
+
declare function combine<const T extends Record<string, AnyFactory>>(factories: T): ComponentFactory<UnionToIntersection<InferCtx<T[keyof T]>>, UnionToIntersection<InferScopeContext<T[keyof T]>>, UnionToIntersection<InferRunOptions<T[keyof T]>>, unknown, CombinedOutput<T>>;
|
|
131
|
+
type InferCombined<T> = T extends ComponentFactory<infer _Ctx, infer _ScopeContext, infer _RunOptions, infer _Deps, infer Out> ? Out : never;
|
|
132
|
+
//#endregion
|
|
106
133
|
//#region src/semantic/repo.d.ts
|
|
107
134
|
type Repo<Ctx, ScopeContext, Out, RunOptions> = ComponentFactory<Ctx, ScopeContext, RunOptions, unknown, Out>;
|
|
108
135
|
declare function repo<Ctx, Out, RunOptions = NoRunOptions, ScopeContext = NoScopeContext>(options: Omit<ComponentMeta, "kind">, fn: (app: Run<Ctx, ScopeContext, RunOptions>) => Out): Repo<Ctx, ScopeContext, Out, RunOptions>;
|
|
@@ -118,4 +145,4 @@ type Usecase<Ctx, ScopeContext, Args extends unknown[], Result, RunOptions> = Ca
|
|
|
118
145
|
declare function usecase<Ctx, Args extends unknown[], Result, RunOptions = NoRunOptions, ScopeContext = NoScopeContext>(options: RunOptions & Omit<ComponentMeta, "kind">, fn: (app: Run<Ctx, ScopeContext, RunOptions>) => (...args: Args) => Promise<Result>): Usecase<Ctx, ScopeContext, Args, Result, RunOptions>;
|
|
119
146
|
type InferUsecase<T> = InferCallable<T>;
|
|
120
147
|
//#endregion
|
|
121
|
-
export { Callable, Client, Component, ComponentDefinition, ComponentFactory, ComponentFactoryFn, ComponentInput, ComponentKind, ComponentMeta, ComponentRef, InferCallable, InferClient, InferRepo, InferService, InferUsecase, Middleware, NoDeps, NoRunOptions, NoScopeContext, Repo, Run, Service, Usecase, brandComponent, buildRuntime, callable, client, isComponent, repo, service, usecase };
|
|
148
|
+
export { type AnyFactory, Callable, Client, CombinedOutput, Component, ComponentDefinition, ComponentFactory, ComponentFactoryFn, ComponentInput, ComponentKind, ComponentMeta, ComponentRef, InferCallable, InferClient, InferCombined, type InferCtx, type InferOut, InferRepo, type InferRunOptions, type InferScopeContext, InferService, InferUsecase, Middleware, NoDeps, NoRunOptions, NoScopeContext, Repo, Run, Service, type UnionToIntersection, Usecase, WarpMeta, brandComponent, buildRuntime, callable, client, combine, isComponent, repo, service, usecase };
|
package/dist/index.mjs
CHANGED
|
@@ -17,19 +17,19 @@ function isComponent(value) {
|
|
|
17
17
|
const Lazy = {
|
|
18
18
|
cached(factory) {
|
|
19
19
|
let cached;
|
|
20
|
-
return () => {
|
|
21
|
-
if (cached === void 0) cached = factory();
|
|
20
|
+
return (...args) => {
|
|
21
|
+
if (cached === void 0) cached = factory(...args);
|
|
22
22
|
return cached;
|
|
23
23
|
};
|
|
24
24
|
},
|
|
25
25
|
and(l, r) {
|
|
26
|
-
return () => ({
|
|
27
|
-
...l(),
|
|
28
|
-
...r()
|
|
26
|
+
return (...args) => ({
|
|
27
|
+
...l(...args),
|
|
28
|
+
...r(...args)
|
|
29
29
|
});
|
|
30
30
|
},
|
|
31
31
|
map(l, fn) {
|
|
32
|
-
return () => fn(l());
|
|
32
|
+
return (...args) => fn(l(...args));
|
|
33
33
|
}
|
|
34
34
|
};
|
|
35
35
|
//#endregion
|
|
@@ -51,6 +51,24 @@ function defineClassComponent() {
|
|
|
51
51
|
};
|
|
52
52
|
}
|
|
53
53
|
//#endregion
|
|
54
|
+
//#region src/component/class-singleton.ts
|
|
55
|
+
function defineClassSingleton() {
|
|
56
|
+
return (ctor, deps, meta) => {
|
|
57
|
+
const factory = (ctx) => {
|
|
58
|
+
return new ctor(ctx);
|
|
59
|
+
};
|
|
60
|
+
return brandComponent({
|
|
61
|
+
factory: Lazy.cached(factory),
|
|
62
|
+
deps,
|
|
63
|
+
meta: {
|
|
64
|
+
name: (meta?.name ?? ctor.meta?.name) || void 0,
|
|
65
|
+
tags: (meta?.tags ?? ctor.meta?.tags) || void 0,
|
|
66
|
+
kind: (meta?.kind ?? ctor.meta?.kind) || void 0
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
//#endregion
|
|
54
72
|
//#region src/component/functional-component.ts
|
|
55
73
|
function defineFunctionalComponent() {
|
|
56
74
|
return (factory, deps, meta) => {
|
|
@@ -66,6 +84,21 @@ function defineFunctionalComponent() {
|
|
|
66
84
|
};
|
|
67
85
|
}
|
|
68
86
|
//#endregion
|
|
87
|
+
//#region src/component/functional-singleton.ts
|
|
88
|
+
function defineFunctionalSingleton() {
|
|
89
|
+
return (factory, deps, meta) => {
|
|
90
|
+
return brandComponent({
|
|
91
|
+
factory: Lazy.cached(factory),
|
|
92
|
+
deps,
|
|
93
|
+
meta: {
|
|
94
|
+
name: (meta?.name ?? factory.meta?.name) || void 0,
|
|
95
|
+
tags: (meta?.tags ?? factory.meta?.tags) || void 0,
|
|
96
|
+
kind: (meta?.kind ?? factory.meta?.kind) || void 0
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
//#endregion
|
|
69
102
|
//#region src/explain/index.ts
|
|
70
103
|
const explain = (c) => {
|
|
71
104
|
const deps = {};
|
|
@@ -146,7 +179,7 @@ function createResolver(mw) {
|
|
|
146
179
|
get() {
|
|
147
180
|
if (cache.has(depName)) return cache.get(depName);
|
|
148
181
|
checkCyclicDependency(depPath);
|
|
149
|
-
const out = withStackTracking(depPath, () => bindInScope(depComp, scopeCtx, depPath));
|
|
182
|
+
const out = withStackTracking(depPath, () => bindInScope(depComp, scopeCtx, depPath, depName));
|
|
150
183
|
cache.set(depName, out);
|
|
151
184
|
return out;
|
|
152
185
|
}
|
|
@@ -155,29 +188,34 @@ function createResolver(mw) {
|
|
|
155
188
|
const attachDependencies = (runCtx, deps, scopeCtx, pathPrefix, cache) => {
|
|
156
189
|
for (const [depName, depComp] of Object.entries(deps)) defineDependencyProperty(runCtx, depName, pathPrefix ? `${pathPrefix}.${depName}` : depName, depComp, scopeCtx, cache);
|
|
157
190
|
};
|
|
158
|
-
const createBaseRunContext = (scopeCtx) => {
|
|
191
|
+
const createBaseRunContext = (scopeCtx, warp) => {
|
|
159
192
|
return {
|
|
160
193
|
...scopeCtx,
|
|
161
|
-
|
|
194
|
+
warp,
|
|
195
|
+
run: (nestedOptions, nestedInner) => runWithContext(scopeCtx, nestedOptions, nestedInner, warp)
|
|
162
196
|
};
|
|
163
197
|
};
|
|
164
|
-
const bindInScope = (comp, scopeCtx, path) => {
|
|
198
|
+
const bindInScope = (comp, scopeCtx, path, depName) => {
|
|
165
199
|
const localDepCache = /* @__PURE__ */ new Map();
|
|
166
|
-
const runCtx = createBaseRunContext(scopeCtx);
|
|
167
200
|
if (!isComponent(comp)) return comp;
|
|
201
|
+
const runCtx = createBaseRunContext(scopeCtx, {
|
|
202
|
+
component: comp.meta,
|
|
203
|
+
componentPath: path,
|
|
204
|
+
componentKey: depName
|
|
205
|
+
});
|
|
168
206
|
attachDependencies(runCtx, comp.deps ?? {}, scopeCtx, path, localDepCache);
|
|
169
207
|
return comp.factory(runCtx);
|
|
170
208
|
};
|
|
171
209
|
const makeRootRunContext = (scopeCtx) => {
|
|
172
210
|
const rootCache = /* @__PURE__ */ new Map();
|
|
173
|
-
const runCtx = createBaseRunContext(scopeCtx);
|
|
211
|
+
const runCtx = createBaseRunContext(scopeCtx, { component: root.meta });
|
|
174
212
|
attachDependencies(runCtx, root.deps ?? {}, scopeCtx, "", rootCache);
|
|
175
213
|
return runCtx;
|
|
176
214
|
};
|
|
177
|
-
const runWithContext = async (currentCtx, options, inner) => {
|
|
215
|
+
const runWithContext = async (currentCtx, options, inner, warp) => {
|
|
178
216
|
return await mw(currentCtx, options, (scopedCtx) => {
|
|
179
217
|
return inner(makeRootRunContext(scopedCtx));
|
|
180
|
-
});
|
|
218
|
+
}, warp);
|
|
181
219
|
};
|
|
182
220
|
return root.factory(makeRootRunContext(ctx));
|
|
183
221
|
};
|
|
@@ -211,9 +249,15 @@ var Runtime = class Runtime {
|
|
|
211
249
|
get component() {
|
|
212
250
|
return defineFunctionalComponent();
|
|
213
251
|
}
|
|
252
|
+
get singleton() {
|
|
253
|
+
return defineFunctionalSingleton();
|
|
254
|
+
}
|
|
214
255
|
get classComponent() {
|
|
215
256
|
return defineClassComponent();
|
|
216
257
|
}
|
|
258
|
+
get classSingleton() {
|
|
259
|
+
return defineClassSingleton();
|
|
260
|
+
}
|
|
217
261
|
explain(component, format = "native", showMeta = false) {
|
|
218
262
|
const result = explain(component);
|
|
219
263
|
switch (format) {
|
|
@@ -284,6 +328,16 @@ function client(options, fn) {
|
|
|
284
328
|
return factory;
|
|
285
329
|
}
|
|
286
330
|
//#endregion
|
|
331
|
+
//#region src/semantic/combine.ts
|
|
332
|
+
function combine(factories) {
|
|
333
|
+
const factory = (ctx) => {
|
|
334
|
+
const result = {};
|
|
335
|
+
for (const [key, f] of Object.entries(factories)) result[key] = f(ctx);
|
|
336
|
+
return result;
|
|
337
|
+
};
|
|
338
|
+
return factory;
|
|
339
|
+
}
|
|
340
|
+
//#endregion
|
|
287
341
|
//#region src/semantic/repo.ts
|
|
288
342
|
function repo(options, fn) {
|
|
289
343
|
const factory = fn;
|
|
@@ -314,4 +368,4 @@ function usecase(options, fn) {
|
|
|
314
368
|
}, fn);
|
|
315
369
|
}
|
|
316
370
|
//#endregion
|
|
317
|
-
export { brandComponent, buildRuntime, callable, client, isComponent, repo, service, usecase };
|
|
371
|
+
export { brandComponent, buildRuntime, callable, client, combine, isComponent, repo, service, usecase };
|