udic 0.4.0 → 1.1.0
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/index.d.ts +33 -24
- package/index.js +1 -1
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -1,50 +1,49 @@
|
|
|
1
1
|
declare const _t: unique symbol;
|
|
2
2
|
declare const _d: unique symbol;
|
|
3
|
-
type
|
|
3
|
+
type MergeUnion<U> = (U extends any ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
|
|
4
|
+
type UnwrapReturn<T> = T extends () => infer R ? R : T;
|
|
4
5
|
type Prettify<T> = { [K in keyof T] : T[K] } & {};
|
|
6
|
+
type List<T> = [T, ...T[]];
|
|
7
|
+
export type Dependency = (() => Service) | Compute;
|
|
8
|
+
export type TDependency<T extends Dependency | Impl> = MergeUnion<UnwrapReturn<T>[typeof _d]>;
|
|
9
|
+
export type TResult<T extends Dependency | Impl> = UnwrapReturn<T>[typeof _t];
|
|
5
10
|
export interface Service<
|
|
6
|
-
K extends string,
|
|
7
|
-
T
|
|
11
|
+
K extends string = any,
|
|
12
|
+
T = any
|
|
8
13
|
> {
|
|
9
14
|
[_t]: T;
|
|
10
15
|
[_d]: undefined extends T ? { [k in K]? : T } : { [k in K] : T };
|
|
11
16
|
}
|
|
12
17
|
export interface Compute<
|
|
13
|
-
T,
|
|
14
|
-
R
|
|
18
|
+
T = any,
|
|
19
|
+
R = any
|
|
15
20
|
> {
|
|
16
21
|
[_t]: R;
|
|
17
22
|
[_d]: T;
|
|
18
23
|
(c: T): R;
|
|
19
24
|
}
|
|
20
|
-
export interface
|
|
21
|
-
K extends string,
|
|
22
|
-
D,
|
|
23
|
-
R
|
|
25
|
+
export interface Impl<
|
|
26
|
+
K extends string = any,
|
|
27
|
+
D = any,
|
|
28
|
+
R = any
|
|
24
29
|
> {
|
|
25
30
|
[_t]: { [k in K] : R };
|
|
26
31
|
[_d]: D;
|
|
27
32
|
}
|
|
28
|
-
export type InferResult<T extends {
|
|
29
|
-
[_t]: any
|
|
30
|
-
}> = T[typeof _t];
|
|
31
|
-
export type InferDependency<T extends {
|
|
32
|
-
[_d]: any
|
|
33
|
-
}> = Prettify<UnionToIntersection<T[typeof _d]>>;
|
|
34
33
|
/**
|
|
35
34
|
* Create a service
|
|
36
35
|
* @param name - The service name
|
|
37
36
|
*/
|
|
38
|
-
export declare const service: <const T extends string>(name: T) => (<K>() => Service<T, K>
|
|
37
|
+
export declare const service: <const T extends string>(name: T) => (<K>() => Service<T, K>);
|
|
39
38
|
/**
|
|
40
39
|
* Create a compute that relies on other services or computes
|
|
41
40
|
* @param deps - The service dependencies
|
|
42
41
|
* @param f
|
|
43
42
|
*/
|
|
44
|
-
export declare const
|
|
45
|
-
const T extends (Service<any, any> | Compute<any, any
|
|
43
|
+
export declare const use: <
|
|
44
|
+
const T extends List<(() => Service<any, any>) | Compute<any, any>>,
|
|
46
45
|
R
|
|
47
|
-
>(deps: T, f: (...args: { [K in keyof T] :
|
|
46
|
+
>(deps: T, f: (...args: { [K in keyof T] : TResult<T[K]> }) => R) => Compute<TDependency<T[number]>, R>;
|
|
48
47
|
/**
|
|
49
48
|
* Inject dependencies to the compute
|
|
50
49
|
* @param compute
|
|
@@ -55,13 +54,23 @@ export declare const inject: <
|
|
|
55
54
|
R,
|
|
56
55
|
D extends Partial<T>
|
|
57
56
|
>(compute: Compute<T, R>, d: D) => Compute<Prettify<Omit<T, keyof D>>, R>;
|
|
57
|
+
/**
|
|
58
|
+
* Create an implementation of the service that depends on other services
|
|
59
|
+
* @param service
|
|
60
|
+
* @param compute
|
|
61
|
+
*/
|
|
58
62
|
export declare const impl: <
|
|
59
63
|
K extends string,
|
|
60
64
|
T,
|
|
61
65
|
D
|
|
62
|
-
>(service: Service<K, T>, compute: Compute<D, T>) =>
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
66
|
+
>(service: () => Service<K, T>, compute: Compute<D, T>) => Impl<K, D, T>;
|
|
67
|
+
/**
|
|
68
|
+
* Provide dependencies to implementations
|
|
69
|
+
* @param impls
|
|
70
|
+
* @param deps
|
|
71
|
+
*/
|
|
72
|
+
export declare const link: <
|
|
73
|
+
const T extends List<Impl>,
|
|
74
|
+
D extends TDependency<T[number]>
|
|
75
|
+
>(impls: T, deps: D) => Prettify<D & TResult<T[number]>>;
|
|
67
76
|
export {};
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
let _t=Symbol();export let service=e=>
|
|
1
|
+
let _t=Symbol();export let service=e=>e;let _=i=>(i[_t]=Symbol(),i);export let use=(i,o)=>_(a=>o(...i.map(i=>typeof i===`function`?a[i[_t]]??=i(a):a[i])));export let inject=(e,i)=>_(a=>e({...a,...i}));export let impl=(e,i)=>[e,i];export let link=(e,i)=>{i={...i};for(let a=0;a<e.length;a++)i[e[a][0]]=e[a][1](i);return i};
|