udic 0.0.1 → 0.0.3
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/README.md +18 -12
- package/di.d.ts +29 -0
- package/di.js +1 -0
- package/index.d.ts +2 -2
- package/index.js +1 -1
- package/package.json +2 -2
- package/main.d.ts +0 -44
- package/main.js +0 -1
package/README.md
CHANGED
|
@@ -1,17 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
A simple dependency injection library
|
|
1
|
+
A simple dependency injection library.
|
|
3
2
|
```ts
|
|
4
|
-
|
|
3
|
+
import { di } from 'udic';
|
|
5
4
|
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
}, rand);
|
|
5
|
+
const rand = di.service('rand')<number>();
|
|
6
|
+
const computed = di.compute((val) => val + 1, rand);
|
|
9
7
|
|
|
10
|
-
const
|
|
8
|
+
const rand1 = di.service('rand1')<number>();
|
|
9
|
+
const computed1 = di.compute(
|
|
10
|
+
(val, computed0) => val + computed0,
|
|
11
|
+
rand1,
|
|
12
|
+
computed,
|
|
13
|
+
);
|
|
11
14
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
console.log(
|
|
16
|
+
computed1({
|
|
17
|
+
rand: 9,
|
|
18
|
+
rand1: 9,
|
|
19
|
+
}),
|
|
20
|
+
);
|
|
17
21
|
```
|
|
22
|
+
|
|
23
|
+
This should only be used to replace singletons.
|
package/di.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
declare const tag: unique symbol;
|
|
2
|
+
export interface Service<
|
|
3
|
+
T extends string | symbol,
|
|
4
|
+
K
|
|
5
|
+
> {
|
|
6
|
+
0: K;
|
|
7
|
+
1: K extends undefined ? { [k in T]? : K } : { [k in T] : K };
|
|
8
|
+
readonly [tag]: null;
|
|
9
|
+
}
|
|
10
|
+
export interface Compute<
|
|
11
|
+
T extends Dependency[],
|
|
12
|
+
R
|
|
13
|
+
> {
|
|
14
|
+
(args: InferDependenciesRecord<T>): R;
|
|
15
|
+
0: R;
|
|
16
|
+
1: InferDependenciesRecord<T>;
|
|
17
|
+
readonly [tag]: null;
|
|
18
|
+
}
|
|
19
|
+
export type AnyService = Service<string | symbol, any>;
|
|
20
|
+
export type AnyCompute = Compute<any[], any>;
|
|
21
|
+
export type Dependency = AnyService | AnyCompute;
|
|
22
|
+
export type InferDependencies<T extends Dependency[]> = T extends [infer A extends Dependency, ...infer B extends Dependency[]] ? [A[0], ...InferDependencies<B>] : [];
|
|
23
|
+
export type InferDependenciesRecord<T extends Dependency[]> = T extends [infer A extends Dependency, ...infer B extends Dependency[]] ? A[1] & InferDependenciesRecord<B> : {};
|
|
24
|
+
export declare const service: <T extends string | symbol>(t: T) => (<K>() => Service<T, K>);
|
|
25
|
+
export declare const compute: <
|
|
26
|
+
const T extends Dependency[],
|
|
27
|
+
const R
|
|
28
|
+
>(f: (...args: InferDependencies<NoInfer<T>>) => R, ...deps: T) => Compute<T, R>;
|
|
29
|
+
export {};
|
package/di.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export let service=e=>()=>e;export let compute=(e,...t)=>n=>e(...t.map(e=>typeof e===`function`?n[e]??=e(n):n[e]));
|
package/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * as
|
|
2
|
-
export * from "./
|
|
1
|
+
export * as di from "./di.js";
|
|
2
|
+
export * from "./di.js";
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export*as
|
|
1
|
+
export*as di from"./di.js";export*from"./di.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "udic",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "A simple dependency injection library",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"license": "MIT",
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"main": "./index.js",
|
|
9
9
|
"types": "./index.d.ts",
|
|
10
10
|
"exports": {
|
|
11
|
-
"./
|
|
11
|
+
"./di": "./di.js",
|
|
12
12
|
".": "./index.js"
|
|
13
13
|
}
|
|
14
14
|
}
|
package/main.d.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
type UnionToIntersection<U> = (U extends any ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
|
|
2
|
-
export type Dependency<
|
|
3
|
-
Tag extends {
|
|
4
|
-
0: any
|
|
5
|
-
} = any,
|
|
6
|
-
T = any
|
|
7
|
-
> = {
|
|
8
|
-
0: Record<Tag[0], T>
|
|
9
|
-
1: T
|
|
10
|
-
} & symbol;
|
|
11
|
-
export interface Compute<
|
|
12
|
-
T extends Params[] = any,
|
|
13
|
-
R = any
|
|
14
|
-
> {
|
|
15
|
-
(c: UnionToIntersection<T[number][0] & {}>): R;
|
|
16
|
-
0: UnionToIntersection<T[number]>;
|
|
17
|
-
1: R;
|
|
18
|
-
}
|
|
19
|
-
type Params = Dependency | Compute;
|
|
20
|
-
type InferParams<T extends Params[]> = T extends [infer A extends Params, ...infer B extends Params[]] ? [A[1] & {}, ...InferParams<B>] : [];
|
|
21
|
-
/**
|
|
22
|
-
* Create a dependency
|
|
23
|
-
*/
|
|
24
|
-
export declare const dependency: <T>() => Dependency<{
|
|
25
|
-
readonly 0: unique symbol
|
|
26
|
-
}, T>;
|
|
27
|
-
/**
|
|
28
|
-
* Compute another value based on dependencies or other computes
|
|
29
|
-
* @param f
|
|
30
|
-
* @param deps
|
|
31
|
-
* @returns a compute
|
|
32
|
-
*/
|
|
33
|
-
export declare const compute: <
|
|
34
|
-
const T extends Params[],
|
|
35
|
-
const R
|
|
36
|
-
>(f: (...data: InferParams<NoInfer<T>>) => R, ...deps: T) => Compute<T, R>;
|
|
37
|
-
/**
|
|
38
|
-
* Create a dependency context
|
|
39
|
-
* @param x
|
|
40
|
-
*/
|
|
41
|
-
export declare const provide: <T extends {
|
|
42
|
-
[K: Dependency]: (typeof K)[1]
|
|
43
|
-
}>(x: T) => Readonly<UnionToIntersection<keyof T extends Dependency ? (keyof T)[0] : any>>;
|
|
44
|
-
export {};
|
package/main.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export let dependency=Symbol;export let compute=(e,...t)=>n=>{for(let e=0;e<t.length;e++)t[e]=typeof t[e]===`symbol`?n[t[e]]:n[t[e]]??=t[e](n);return e(...t)};export let provide=e=>e;
|