react-obsidian 2.9.1 → 2.10.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/dist/src/decorators/LifecycleBound.d.ts +5 -1
- package/dist/src/decorators/LifecycleBound.d.ts.map +1 -1
- package/dist/src/decorators/LifecycleBound.js +3 -1
- package/dist/src/decorators/LifecycleBound.js.map +1 -1
- package/dist/src/graph/registry/GraphRegistry.d.ts +5 -1
- package/dist/src/graph/registry/GraphRegistry.d.ts.map +1 -1
- package/dist/src/graph/registry/GraphRegistry.js +32 -6
- package/dist/src/graph/registry/GraphRegistry.js.map +1 -1
- package/dist/src/injectors/components/ComponentInjector.d.ts.map +1 -1
- package/dist/src/injectors/components/ComponentInjector.js +5 -2
- package/dist/src/injectors/components/ComponentInjector.js.map +1 -1
- package/dist/src/injectors/components/graphContext.d.ts +6 -0
- package/dist/src/injectors/components/graphContext.d.ts.map +1 -0
- package/dist/src/injectors/components/graphContext.js +6 -0
- package/dist/src/injectors/components/graphContext.js.map +1 -0
- package/dist/src/injectors/components/useGraph.d.ts +1 -1
- package/dist/src/injectors/components/useGraph.d.ts.map +1 -1
- package/dist/src/injectors/components/useGraph.js +2 -2
- package/dist/src/injectors/components/useGraph.js.map +1 -1
- package/dist/src/injectors/components/useInjectionToken.d.ts +3 -0
- package/dist/src/injectors/components/useInjectionToken.d.ts.map +1 -0
- package/dist/src/injectors/components/useInjectionToken.js +13 -0
- package/dist/src/injectors/components/useInjectionToken.js.map +1 -0
- package/package.json +2 -2
- package/src/decorators/LifecycleBound.ts +6 -1
- package/src/graph/registry/GraphRegistry.ts +35 -5
- package/src/injectors/components/ComponentInjector.tsx +9 -2
- package/src/injectors/components/graphContext.ts +7 -0
- package/src/injectors/components/useGraph.ts +8 -2
- package/src/injectors/components/useInjectionToken.ts +10 -0
|
@@ -1,2 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
type Options = {
|
|
2
|
+
scope?: 'component' | 'feature';
|
|
3
|
+
};
|
|
4
|
+
export declare function LifecycleBound(options?: Options): (constructor: any) => any;
|
|
5
|
+
export {};
|
|
2
6
|
//# sourceMappingURL=LifecycleBound.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LifecycleBound.d.ts","sourceRoot":"","sources":["../../../src/decorators/LifecycleBound.ts"],"names":[],"mappings":"AAAA,wBAAgB,cAAc,
|
|
1
|
+
{"version":3,"file":"LifecycleBound.d.ts","sourceRoot":"","sources":["../../../src/decorators/LifecycleBound.ts"],"names":[],"mappings":"AAAA,KAAK,OAAO,GAAG;IACb,KAAK,CAAC,EAAE,WAAW,GAAG,SAAS,CAAC;CACjC,CAAC;AAEF,wBAAgB,cAAc,CAAC,OAAO,CAAC,EAAE,OAAO,iBACzB,GAAG,SAKzB"}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.LifecycleBound = void 0;
|
|
4
|
-
function LifecycleBound() {
|
|
4
|
+
function LifecycleBound(options) {
|
|
5
5
|
return (constructor) => {
|
|
6
|
+
var _a;
|
|
6
7
|
Reflect.defineMetadata('isLifecycleBound', true, constructor);
|
|
8
|
+
Reflect.defineMetadata('lifecycleScope', (_a = options === null || options === void 0 ? void 0 : options.scope) !== null && _a !== void 0 ? _a : 'feature', constructor);
|
|
7
9
|
return constructor;
|
|
8
10
|
};
|
|
9
11
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LifecycleBound.js","sourceRoot":"","sources":["../../../src/decorators/LifecycleBound.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"LifecycleBound.js","sourceRoot":"","sources":["../../../src/decorators/LifecycleBound.ts"],"names":[],"mappings":";;;AAIA,SAAgB,cAAc,CAAC,OAAiB;IAC9C,OAAO,CAAC,WAAgB,EAAE,EAAE;;QAC1B,OAAO,CAAC,cAAc,CAAC,kBAAkB,EAAE,IAAI,EAAE,WAAW,CAAC,CAAC;QAC9D,OAAO,CAAC,cAAc,CAAC,gBAAgB,EAAE,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,KAAK,mCAAI,SAAS,EAAE,WAAW,CAAC,CAAC;QACnF,OAAO,WAAW,CAAC;IACrB,CAAC,CAAC;AACJ,CAAC;AAND,wCAMC"}
|
|
@@ -4,6 +4,8 @@ import { Middleware } from './Middleware';
|
|
|
4
4
|
export declare class GraphRegistry {
|
|
5
5
|
private readonly constructorToInstance;
|
|
6
6
|
private readonly instanceToConstructor;
|
|
7
|
+
private readonly injectionTokenToInstance;
|
|
8
|
+
private readonly instanceToInjectionToken;
|
|
7
9
|
private readonly nameToInstance;
|
|
8
10
|
private readonly graphToSubgraphs;
|
|
9
11
|
private readonly graphMiddlewares;
|
|
@@ -11,12 +13,14 @@ export declare class GraphRegistry {
|
|
|
11
13
|
ensureRegistered(graph: Graph): void;
|
|
12
14
|
getSubgraphs(graph: Graph): Graph[];
|
|
13
15
|
getGraphInstance(name: string): Graph;
|
|
14
|
-
resolve<T extends Graph>(Graph: Constructable<T>, source?: 'lifecycleOwner' | 'classInjection' | 'serviceLocator', props?: any): T;
|
|
16
|
+
resolve<T extends Graph>(Graph: Constructable<T>, source?: 'lifecycleOwner' | 'classInjection' | 'serviceLocator', props?: any, injectionToken?: string): T;
|
|
15
17
|
private has;
|
|
16
18
|
private getFirst;
|
|
19
|
+
private getByInjectionToken;
|
|
17
20
|
private set;
|
|
18
21
|
private isSingleton;
|
|
19
22
|
private isBoundToReactLifecycle;
|
|
23
|
+
private isComponentScopedLifecycleBound;
|
|
20
24
|
clearGraphAfterItWasMockedInTests(graphName: string): void;
|
|
21
25
|
clear(graph: Graph): void;
|
|
22
26
|
addGraphMiddleware(middleware: Middleware<Graph>): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GraphRegistry.d.ts","sourceRoot":"","sources":["../../../../src/graph/registry/GraphRegistry.ts"],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rE,OAAO,CAAC,QAAQ,CAAC,wBAAwB,CAA4B;IACrE,OAAO,CAAC,QAAQ,CAAC,cAAc,CAA4B;IAC3D,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAA8D;IAC/F,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAA8B;IAE/D,QAAQ,CAAC,WAAW,EAAE,aAAa,CAAC,KAAK,CAAC,EAAE,SAAS,GAAE,aAAa,CAAC,KAAK,CAAC,EAAO;IAIlF,gBAAgB,CAAC,KAAK,EAAE,KAAK;IAK7B,YAAY,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,EAAE;IAMnC,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,KAAK;IAIrC,OAAO,CAAC,CAAC,SAAS,KAAK,EACrB,KAAK,EAAE,aAAa,CAAC,CAAC,CAAC,EACvB,MAAM,GAAE,gBAAgB,GAAG,gBAAgB,GAAG,gBAAmC,EACjF,KAAK,GAAE,GAAe,EACtB,cAAc,CAAC,EAAE,MAAM,GACtB,CAAC;IAcJ,OAAO,CAAC,GAAG;IAaX,OAAO,CAAC,QAAQ;IAIhB,OAAO,CAAC,mBAAmB;IAQ3B,OAAO,CAAC,GAAG;IAYX,OAAO,CAAC,WAAW;IAInB,OAAO,CAAC,uBAAuB;IAI/B,OAAO,CAAC,+BAA+B;IAIvC,iCAAiC,CAAC,SAAS,EAAE,MAAM;IAgBnD,KAAK,CAAC,KAAK,EAAE,KAAK;IAQlB,kBAAkB,CAAC,UAAU,EAAE,UAAU,CAAC,KAAK,CAAC;IAIhD,qBAAqB;IAIrB,QAAQ;CAKT;;AAKD,wBAAqD"}
|
|
@@ -10,6 +10,8 @@ class GraphRegistry {
|
|
|
10
10
|
constructor() {
|
|
11
11
|
this.constructorToInstance = new Map();
|
|
12
12
|
this.instanceToConstructor = new Map();
|
|
13
|
+
this.injectionTokenToInstance = new Map();
|
|
14
|
+
this.instanceToInjectionToken = new Map();
|
|
13
15
|
this.nameToInstance = new Map();
|
|
14
16
|
this.graphToSubgraphs = new Map();
|
|
15
17
|
this.graphMiddlewares = new GraphMiddlewareChain_1.default();
|
|
@@ -31,27 +33,48 @@ class GraphRegistry {
|
|
|
31
33
|
getGraphInstance(name) {
|
|
32
34
|
return this.nameToInstance.get(name);
|
|
33
35
|
}
|
|
34
|
-
resolve(Graph, source = 'lifecycleOwner', props = undefined) {
|
|
35
|
-
if ((this.isSingleton(Graph) || this.isBoundToReactLifecycle(Graph)) && this.has(Graph)) {
|
|
36
|
-
return this.
|
|
36
|
+
resolve(Graph, source = 'lifecycleOwner', props = undefined, injectionToken) {
|
|
37
|
+
if ((this.isSingleton(Graph) || this.isBoundToReactLifecycle(Graph)) && this.has(Graph, injectionToken)) {
|
|
38
|
+
return this.isComponentScopedLifecycleBound(Graph) ?
|
|
39
|
+
this.getByInjectionToken(Graph, injectionToken) :
|
|
40
|
+
this.getFirst(Graph);
|
|
37
41
|
}
|
|
38
42
|
if (this.isBoundToReactLifecycle(Graph) && source !== 'lifecycleOwner') {
|
|
39
43
|
throw new ObtainLifecycleBoundGraphException_1.ObtainLifecycleBoundGraphException(Graph);
|
|
40
44
|
}
|
|
41
45
|
const graph = this.graphMiddlewares.resolve(Graph, props);
|
|
42
|
-
this.set(Graph, graph);
|
|
46
|
+
this.set(Graph, graph, injectionToken);
|
|
43
47
|
return graph;
|
|
44
48
|
}
|
|
45
|
-
has(Graph) {
|
|
49
|
+
has(Graph, injectionToken) {
|
|
46
50
|
var _a, _b;
|
|
51
|
+
const instances = this.constructorToInstance.get(Graph);
|
|
52
|
+
if (!instances)
|
|
53
|
+
return false;
|
|
54
|
+
if (this.isComponentScopedLifecycleBound(Graph)) {
|
|
55
|
+
return Array
|
|
56
|
+
.from(instances)
|
|
57
|
+
.some((graph) => this.instanceToInjectionToken.get(graph) === injectionToken);
|
|
58
|
+
}
|
|
47
59
|
return ((_b = (_a = this.constructorToInstance.get(Graph)) === null || _a === void 0 ? void 0 : _a.size) !== null && _b !== void 0 ? _b : 0) > 0;
|
|
48
60
|
}
|
|
49
61
|
getFirst(Graph) {
|
|
50
62
|
return this.constructorToInstance.get(Graph).values().next().value;
|
|
51
63
|
}
|
|
52
|
-
|
|
64
|
+
getByInjectionToken(Graph, injectionToken) {
|
|
65
|
+
return Array
|
|
66
|
+
.from(this.constructorToInstance.get(Graph))
|
|
67
|
+
.find((graph) => {
|
|
68
|
+
return this.instanceToInjectionToken.get(graph) === injectionToken;
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
set(Graph, graph, injectionToken) {
|
|
53
72
|
var _a;
|
|
54
73
|
const graphs = (_a = this.constructorToInstance.get(Graph)) !== null && _a !== void 0 ? _a : new Set();
|
|
74
|
+
if (injectionToken && this.isComponentScopedLifecycleBound(Graph)) {
|
|
75
|
+
this.injectionTokenToInstance.set(injectionToken, graph);
|
|
76
|
+
this.instanceToInjectionToken.set(graph, injectionToken);
|
|
77
|
+
}
|
|
55
78
|
graphs.add(graph);
|
|
56
79
|
this.constructorToInstance.set(Graph, graphs);
|
|
57
80
|
this.instanceToConstructor.set(graph, Graph);
|
|
@@ -65,6 +88,9 @@ class GraphRegistry {
|
|
|
65
88
|
var _a;
|
|
66
89
|
return (_a = Reflect.getMetadata('isLifecycleBound', Graph)) !== null && _a !== void 0 ? _a : false;
|
|
67
90
|
}
|
|
91
|
+
isComponentScopedLifecycleBound(Graph) {
|
|
92
|
+
return Reflect.getMetadata('lifecycleScope', Graph) === 'component';
|
|
93
|
+
}
|
|
68
94
|
clearGraphAfterItWasMockedInTests(graphName) {
|
|
69
95
|
const graphNames = this.nameToInstance.keys();
|
|
70
96
|
for (const name of graphNames) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GraphRegistry.js","sourceRoot":"","sources":["../../../../src/graph/registry/GraphRegistry.ts"],"names":[],"mappings":";;;;;;AAGA,kFAA0D;AAC1D,6FAA0F;AAE1F,MAAa,aAAa;IAA1B;QACmB,0BAAqB,GAAG,IAAI,GAAG,EAAoC,C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C;gBACrD,IAAI,CAAC,cAAc,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;aACxC;SACF;IACH,CAAC;IAED,KAAK,CAAC,KAAY;QAChB,MAAM,KAAK,GAAG,IAAI,CAAC,qBAAqB,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QACpD,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC;YAAE,OAAO;QAC9C,IAAI,CAAC,qBAAqB,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACzC,IAAI,CAAC,qBAAqB,CAAC,GAAG,CAAC,KAAK,CAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACrD,IAAI,CAAC,cAAc,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACzC,CAAC;IAED,kBAAkB,CAAC,UAA6B;QAC9C,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IACxC,CAAC;IAED,qBAAqB;QACnB,IAAI,CAAC,gBAAgB,CAAC,KAAK,EAAE,CAAC;IAChC,CAAC;IAED,QAAQ;QACN,IAAI,CAAC,qBAAqB,CAAC,KAAK,EAAE,CAAC;QACnC,IAAI,CAAC,qBAAqB,CAAC,KAAK,EAAE,CAAC;QACnC,IAAI,CAAC,cAAc,CAAC,KAAK,EAAE,CAAC;IAC9B,CAAC;CACF;AArID,sCAqIC;AAED,aAAa;AACb,MAAM,CAAC,aAAa,GAAG,MAAM,CAAC,aAAa,IAAI,IAAI,aAAa,EAAE,CAAC;AACnE,aAAa;AACb,kBAAe,MAAM,CAAC,aAA8B,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ComponentInjector.d.ts","sourceRoot":"","sources":["../../../../src/injectors/components/ComponentInjector.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA4B,MAAM,OAAO,CAAC;AAEjD,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAGtD,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"ComponentInjector.d.ts","sourceRoot":"","sources":["../../../../src/injectors/components/ComponentInjector.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA4B,MAAM,OAAO,CAAC;AAEjD,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAGtD,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAK5C,MAAM,CAAC,OAAO,OAAO,iBAAiB;IACpC,MAAM,CAAC,CAAC,EACN,MAAM,EAAE,KAAK,CAAC,iBAAiB,CAAC,CAAC,CAAC,EAClC,KAAK,EAAE,aAAa,CAAC,WAAW,CAAC,GAChC,KAAK,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;IAMtC,OAAO,CAAC,aAAa;CAoBtB"}
|
|
@@ -8,6 +8,8 @@ const hoist_non_react_statics_1 = __importDefault(require("hoist-non-react-stati
|
|
|
8
8
|
const PropsInjector_1 = __importDefault(require("./PropsInjector"));
|
|
9
9
|
const useGraph_1 = __importDefault(require("./useGraph"));
|
|
10
10
|
const React_1 = require("../../utils/React");
|
|
11
|
+
const graphContext_1 = require("./graphContext");
|
|
12
|
+
const useInjectionToken_1 = require("./useInjectionToken");
|
|
11
13
|
class ComponentInjector {
|
|
12
14
|
inject(Target, Graph) {
|
|
13
15
|
const Wrapped = this.wrapComponent(Target, Graph);
|
|
@@ -19,9 +21,10 @@ class ComponentInjector {
|
|
|
19
21
|
const Target = isMemoized ? InjectionCandidate.type : InjectionCandidate;
|
|
20
22
|
const compare = isMemoized ? InjectionCandidate.compare : undefined;
|
|
21
23
|
return (0, React_1.genericMemo)((passedProps) => {
|
|
22
|
-
const
|
|
24
|
+
const injectionToken = (0, useInjectionToken_1.useInjectionToken)(Graph);
|
|
25
|
+
const graph = (0, useGraph_1.default)(Graph, Target, passedProps, injectionToken);
|
|
23
26
|
const proxiedProps = new PropsInjector_1.default(graph).inject(passedProps);
|
|
24
|
-
return react_1.default.createElement(
|
|
27
|
+
return (react_1.default.createElement(graphContext_1.GraphContext.Provider, { value: { injectionToken } }, Target(proxiedProps)));
|
|
25
28
|
}, compare);
|
|
26
29
|
}
|
|
27
30
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ComponentInjector.js","sourceRoot":"","sources":["../../../../src/injectors/components/ComponentInjector.tsx"],"names":[],"mappings":";;;;;AAAA,kDAAiD;AACjD,sFAA2D;AAE3D,oEAA4C;AAC5C,0DAAkC;AAElC,6CAAqE;
|
|
1
|
+
{"version":3,"file":"ComponentInjector.js","sourceRoot":"","sources":["../../../../src/injectors/components/ComponentInjector.tsx"],"names":[],"mappings":";;;;;AAAA,kDAAiD;AACjD,sFAA2D;AAE3D,oEAA4C;AAC5C,0DAAkC;AAElC,6CAAqE;AACrE,iDAA8C;AAC9C,2DAAwD;AAExD,MAAqB,iBAAiB;IACpC,MAAM,CACJ,MAAkC,EAClC,KAAiC;QAEjC,MAAM,OAAO,GAAG,IAAI,CAAC,aAAa,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;QAClD,IAAA,iCAAoB,EAAC,OAAO,EAAE,MAAM,CAAC,CAAC;QACtC,OAAO,OAAO,CAAC;IACjB,CAAC;IAEO,aAAa,CACnB,kBAA8C,EAC9C,KAAiC;QAEjC,MAAM,UAAU,GAAG,IAAA,2BAAmB,EAAC,kBAAkB,CAAC,CAAC;QAC3D,MAAM,MAAM,GAAG,UAAU,CAAC,CAAC,CAAC,kBAAkB,CAAC,IAAI,CAAC,CAAC,CAAC,kBAAkB,CAAC;QACzE,MAAM,OAAO,GAAG,UAAU,CAAC,CAAC,CAAC,kBAAkB,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC;QAEpE,OAAO,IAAA,mBAAW,EAAC,CAAC,WAAc,EAAE,EAAE;YACpC,MAAM,cAAc,GAAG,IAAA,qCAAiB,EAAC,KAAK,CAAC,CAAC;YAChD,MAAM,KAAK,GAAG,IAAA,kBAAQ,EAAI,KAAK,EAAE,MAAM,EAAE,WAAW,EAAE,cAAc,CAAC,CAAC;YACtE,MAAM,YAAY,GAAG,IAAI,uBAAa,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;YAElE,OAAO,CACL,8BAAC,2BAAY,CAAC,QAAQ,IAAC,KAAK,EAAE,EAAC,cAAc,EAAC,IAC3C,MAAM,CAAC,YAA+C,CAAC,CAClC,CACzB,CAAC;QACJ,CAAC,EAAE,OAAO,CAAC,CAAC;IACd,CAAC;CACF;AA9BD,oCA8BC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"graphContext.d.ts","sourceRoot":"","sources":["../../../../src/injectors/components/graphContext.ts"],"names":[],"mappings":"AAEA,UAAU,OAAO;IACf,cAAc,EAAE,MAAM,CAAC;CACxB;AAED,eAAO,MAAM,YAAY,8CAAgD,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"graphContext.js","sourceRoot":"","sources":["../../../../src/injectors/components/graphContext.ts"],"names":[],"mappings":";;;AAAA,iCAAsC;AAMzB,QAAA,YAAY,GAAG,IAAA,qBAAa,EAAsB,SAAS,CAAC,CAAC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Constructable } from '../../types';
|
|
2
2
|
import { ObjectGraph } from '../../graph/ObjectGraph';
|
|
3
|
-
declare const _default: <P>(Graph: Constructable<ObjectGraph>, target: any, props?: Partial<P> | undefined) => ObjectGraph<unknown>;
|
|
3
|
+
declare const _default: <P>(Graph: Constructable<ObjectGraph>, target: any, props?: Partial<P> | undefined, injectionToken?: string) => ObjectGraph<unknown>;
|
|
4
4
|
export default _default;
|
|
5
5
|
//# sourceMappingURL=useGraph.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useGraph.d.ts","sourceRoot":"","sources":["../../../../src/injectors/components/useGraph.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;
|
|
1
|
+
{"version":3,"file":"useGraph.d.ts","sourceRoot":"","sources":["../../../../src/injectors/components/useGraph.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;mCAK7C,cAAc,WAAW,CAAC,UACzB,GAAG,mDAEM,MAAM;AAJzB,wBAiBE"}
|
|
@@ -6,9 +6,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
const react_1 = require("react");
|
|
7
7
|
const GraphRegistry_1 = __importDefault(require("../../graph/registry/GraphRegistry"));
|
|
8
8
|
const ReferenceCounter_1 = __importDefault(require("../../ReferenceCounter"));
|
|
9
|
-
exports.default = (Graph, target, props) => {
|
|
9
|
+
exports.default = (Graph, target, props, injectionToken) => {
|
|
10
10
|
const [graph] = (0, react_1.useState)(() => {
|
|
11
|
-
const resolvedGraph = GraphRegistry_1.default.resolve(Graph, 'lifecycleOwner', props);
|
|
11
|
+
const resolvedGraph = GraphRegistry_1.default.resolve(Graph, 'lifecycleOwner', props, injectionToken);
|
|
12
12
|
resolvedGraph.onBind(target);
|
|
13
13
|
return resolvedGraph;
|
|
14
14
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useGraph.js","sourceRoot":"","sources":["../../../../src/injectors/components/useGraph.ts"],"names":[],"mappings":";;;;;AAAA,iCAA4C;AAG5C,uFAA+D;AAC/D,8EAAsD;AAEtD,kBAAe,
|
|
1
|
+
{"version":3,"file":"useGraph.js","sourceRoot":"","sources":["../../../../src/injectors/components/useGraph.ts"],"names":[],"mappings":";;;;;AAAA,iCAA4C;AAG5C,uFAA+D;AAC/D,8EAAsD;AAEtD,kBAAe,CACb,KAAiC,EACjC,MAAW,EACX,KAAkB,EAClB,cAAuB,EACvB,EAAE;IAEF,MAAM,CAAC,KAAK,CAAC,GAAG,IAAA,gBAAQ,EAAC,GAAG,EAAE;QAC5B,MAAM,aAAa,GAAG,uBAAa,CAAC,OAAO,CAAC,KAAK,EAAE,gBAAgB,EAAE,KAAK,EAAE,cAAc,CAAC,CAAC;QAC5F,aAAa,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QAC7B,OAAO,aAAa,CAAC;IACvB,CAAC,CAAC,CAAC;IACH,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,0BAAgB,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAC/B,OAAO,GAAG,EAAE,CAAC,0BAAgB,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,uBAAa,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;IAC9E,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;IACZ,OAAO,KAAK,CAAC;AACf,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useInjectionToken.d.ts","sourceRoot":"","sources":["../../../../src/injectors/components/useInjectionToken.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,OAAO,CAAC;AAGxD,eAAO,MAAM,iBAAiB,UAAW,cAAc,WAAW,CAAC,WAIlE,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useInjectionToken = void 0;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const graphContext_1 = require("./graphContext");
|
|
6
|
+
const uniqueId_1 = require("../../utils/uniqueId");
|
|
7
|
+
const useInjectionToken = (Graph) => {
|
|
8
|
+
const ctx = (0, react_1.useContext)(graphContext_1.GraphContext);
|
|
9
|
+
const [injectionToken] = (0, react_1.useState)(() => { var _a; return (_a = ctx === null || ctx === void 0 ? void 0 : ctx.injectionToken) !== null && _a !== void 0 ? _a : (0, uniqueId_1.uniqueId)(Graph.name); });
|
|
10
|
+
return injectionToken;
|
|
11
|
+
};
|
|
12
|
+
exports.useInjectionToken = useInjectionToken;
|
|
13
|
+
//# sourceMappingURL=useInjectionToken.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useInjectionToken.js","sourceRoot":"","sources":["../../../../src/injectors/components/useInjectionToken.ts"],"names":[],"mappings":";;;AAAA,iCAA6C;AAC7C,iDAA8C;AAE9C,mDAAgD;AAEzC,MAAM,iBAAiB,GAAG,CAAC,KAAiC,EAAE,EAAE;IACrE,MAAM,GAAG,GAAG,IAAA,kBAAU,EAAC,2BAAY,CAAC,CAAC;IACrC,MAAM,CAAC,cAAc,CAAC,GAAG,IAAA,gBAAQ,EAAC,GAAG,EAAE,WAAC,OAAA,MAAA,GAAG,aAAH,GAAG,uBAAH,GAAG,CAAE,cAAc,mCAAI,IAAA,mBAAQ,EAAC,KAAK,CAAC,IAAI,CAAC,CAAA,EAAA,CAAC,CAAC;IACrF,OAAO,cAAc,CAAC;AACxB,CAAC,CAAC;AAJW,QAAA,iBAAiB,qBAI5B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-obsidian",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.10.0",
|
|
4
4
|
"description": "Dependency injection framework for React and React Native applications",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"prepack": "npm run lint && tsc --project tsconfig.prod.json",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"eslint-plugin-import": "^2.25.2",
|
|
48
48
|
"eslint-plugin-import-newlines": "^1.1.5",
|
|
49
49
|
"eslint-plugin-jest-formatting": "^3.1.0",
|
|
50
|
-
"eslint-plugin-obsidian": "2.
|
|
50
|
+
"eslint-plugin-obsidian": "2.10.0",
|
|
51
51
|
"eslint-plugin-react": "^7.26.1",
|
|
52
52
|
"eslint-plugin-react-hooks": "^4.2.0",
|
|
53
53
|
"eslint-plugin-unused-imports": "3.1.x",
|
|
@@ -1,6 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
type Options = {
|
|
2
|
+
scope?: 'component' | 'feature';
|
|
3
|
+
};
|
|
4
|
+
|
|
5
|
+
export function LifecycleBound(options?: Options) {
|
|
2
6
|
return (constructor: any) => {
|
|
3
7
|
Reflect.defineMetadata('isLifecycleBound', true, constructor);
|
|
8
|
+
Reflect.defineMetadata('lifecycleScope', options?.scope ?? 'feature', constructor);
|
|
4
9
|
return constructor;
|
|
5
10
|
};
|
|
6
11
|
}
|
|
@@ -7,6 +7,8 @@ import { ObtainLifecycleBoundGraphException } from './ObtainLifecycleBoundGraphE
|
|
|
7
7
|
export class GraphRegistry {
|
|
8
8
|
private readonly constructorToInstance = new Map<Constructable<Graph>, Set<Graph>>();
|
|
9
9
|
private readonly instanceToConstructor = new Map<Graph, Constructable<Graph>>();
|
|
10
|
+
private readonly injectionTokenToInstance = new Map<string, Graph>();
|
|
11
|
+
private readonly instanceToInjectionToken = new Map<Graph, string>();
|
|
10
12
|
private readonly nameToInstance = new Map<string, Graph>();
|
|
11
13
|
private readonly graphToSubgraphs = new Map<Constructable<Graph>, Set<Constructable<Graph>>>();
|
|
12
14
|
private readonly graphMiddlewares = new GraphMiddlewareChain();
|
|
@@ -34,19 +36,31 @@ export class GraphRegistry {
|
|
|
34
36
|
Graph: Constructable<T>,
|
|
35
37
|
source: 'lifecycleOwner' | 'classInjection' | 'serviceLocator' = 'lifecycleOwner',
|
|
36
38
|
props: any = undefined,
|
|
39
|
+
injectionToken?: string,
|
|
37
40
|
): T {
|
|
38
|
-
if ((this.isSingleton(Graph) || this.isBoundToReactLifecycle(Graph)) && this.has(Graph)) {
|
|
39
|
-
return this.
|
|
41
|
+
if ((this.isSingleton(Graph) || this.isBoundToReactLifecycle(Graph)) && this.has(Graph, injectionToken)) {
|
|
42
|
+
return this.isComponentScopedLifecycleBound(Graph) ?
|
|
43
|
+
this.getByInjectionToken(Graph, injectionToken) :
|
|
44
|
+
this.getFirst(Graph);
|
|
40
45
|
}
|
|
41
46
|
if (this.isBoundToReactLifecycle(Graph) && source !== 'lifecycleOwner') {
|
|
42
47
|
throw new ObtainLifecycleBoundGraphException(Graph);
|
|
43
48
|
}
|
|
44
49
|
const graph = this.graphMiddlewares.resolve(Graph, props);
|
|
45
|
-
this.set(Graph, graph);
|
|
50
|
+
this.set(Graph, graph, injectionToken);
|
|
46
51
|
return graph as T;
|
|
47
52
|
}
|
|
48
53
|
|
|
49
|
-
private has(Graph: Constructable<Graph
|
|
54
|
+
private has(Graph: Constructable<Graph>, injectionToken?: string): boolean {
|
|
55
|
+
const instances = this.constructorToInstance.get(Graph);
|
|
56
|
+
if (!instances) return false;
|
|
57
|
+
|
|
58
|
+
if (this.isComponentScopedLifecycleBound(Graph)) {
|
|
59
|
+
return Array
|
|
60
|
+
.from(instances)
|
|
61
|
+
.some((graph) => this.instanceToInjectionToken.get(graph) === injectionToken);
|
|
62
|
+
}
|
|
63
|
+
|
|
50
64
|
return (this.constructorToInstance.get(Graph)?.size ?? 0) > 0;
|
|
51
65
|
}
|
|
52
66
|
|
|
@@ -54,8 +68,20 @@ export class GraphRegistry {
|
|
|
54
68
|
return this.constructorToInstance.get(Graph)!.values().next().value;
|
|
55
69
|
}
|
|
56
70
|
|
|
57
|
-
private
|
|
71
|
+
private getByInjectionToken<T extends Graph>(Graph: Constructable<T>, injectionToken?: string): T {
|
|
72
|
+
return Array
|
|
73
|
+
.from(this.constructorToInstance.get(Graph)!)
|
|
74
|
+
.find((graph) => {
|
|
75
|
+
return this.instanceToInjectionToken.get(graph) === injectionToken;
|
|
76
|
+
}) as T;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
private set(Graph: Constructable<Graph>, graph: Graph, injectionToken?: string) {
|
|
58
80
|
const graphs = this.constructorToInstance.get(Graph) ?? new Set();
|
|
81
|
+
if (injectionToken && this.isComponentScopedLifecycleBound(Graph)) {
|
|
82
|
+
this.injectionTokenToInstance.set(injectionToken, graph);
|
|
83
|
+
this.instanceToInjectionToken.set(graph, injectionToken);
|
|
84
|
+
}
|
|
59
85
|
graphs.add(graph);
|
|
60
86
|
this.constructorToInstance.set(Graph, graphs);
|
|
61
87
|
this.instanceToConstructor.set(graph, Graph);
|
|
@@ -70,6 +96,10 @@ export class GraphRegistry {
|
|
|
70
96
|
return Reflect.getMetadata('isLifecycleBound', Graph) ?? false;
|
|
71
97
|
}
|
|
72
98
|
|
|
99
|
+
private isComponentScopedLifecycleBound(Graph: Constructable<Graph>): boolean {
|
|
100
|
+
return Reflect.getMetadata('lifecycleScope', Graph) === 'component';
|
|
101
|
+
}
|
|
102
|
+
|
|
73
103
|
clearGraphAfterItWasMockedInTests(graphName: string) {
|
|
74
104
|
const graphNames = this.nameToInstance.keys();
|
|
75
105
|
for (const name of graphNames) {
|
|
@@ -5,6 +5,8 @@ import PropsInjector from './PropsInjector';
|
|
|
5
5
|
import useGraph from './useGraph';
|
|
6
6
|
import { Constructable } from '../../types';
|
|
7
7
|
import { genericMemo, isMemoizedComponent } from '../../utils/React';
|
|
8
|
+
import { GraphContext } from './graphContext';
|
|
9
|
+
import { useInjectionToken } from './useInjectionToken';
|
|
8
10
|
|
|
9
11
|
export default class ComponentInjector {
|
|
10
12
|
inject<P>(
|
|
@@ -25,10 +27,15 @@ export default class ComponentInjector {
|
|
|
25
27
|
const compare = isMemoized ? InjectionCandidate.compare : undefined;
|
|
26
28
|
|
|
27
29
|
return genericMemo((passedProps: P) => {
|
|
28
|
-
const
|
|
30
|
+
const injectionToken = useInjectionToken(Graph);
|
|
31
|
+
const graph = useGraph<P>(Graph, Target, passedProps, injectionToken);
|
|
29
32
|
const proxiedProps = new PropsInjector(graph).inject(passedProps);
|
|
30
33
|
|
|
31
|
-
return
|
|
34
|
+
return (
|
|
35
|
+
<GraphContext.Provider value={{injectionToken}}>
|
|
36
|
+
{Target(proxiedProps as unknown as PropsWithChildren<P>)}
|
|
37
|
+
</GraphContext.Provider>
|
|
38
|
+
);
|
|
32
39
|
}, compare);
|
|
33
40
|
}
|
|
34
41
|
}
|
|
@@ -4,9 +4,15 @@ import { ObjectGraph } from '../../graph/ObjectGraph';
|
|
|
4
4
|
import graphRegistry from '../../graph/registry/GraphRegistry';
|
|
5
5
|
import referenceCounter from '../../ReferenceCounter';
|
|
6
6
|
|
|
7
|
-
export default <P>(
|
|
7
|
+
export default <P>(
|
|
8
|
+
Graph: Constructable<ObjectGraph>,
|
|
9
|
+
target: any,
|
|
10
|
+
props?: Partial<P>,
|
|
11
|
+
injectionToken?: string,
|
|
12
|
+
) => {
|
|
13
|
+
|
|
8
14
|
const [graph] = useState(() => {
|
|
9
|
-
const resolvedGraph = graphRegistry.resolve(Graph, 'lifecycleOwner', props);
|
|
15
|
+
const resolvedGraph = graphRegistry.resolve(Graph, 'lifecycleOwner', props, injectionToken);
|
|
10
16
|
resolvedGraph.onBind(target);
|
|
11
17
|
return resolvedGraph;
|
|
12
18
|
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { useContext, useState } from 'react';
|
|
2
|
+
import { GraphContext } from './graphContext';
|
|
3
|
+
import type { Constructable, ObjectGraph } from '../..';
|
|
4
|
+
import { uniqueId } from '../../utils/uniqueId';
|
|
5
|
+
|
|
6
|
+
export const useInjectionToken = (Graph: Constructable<ObjectGraph>) => {
|
|
7
|
+
const ctx = useContext(GraphContext);
|
|
8
|
+
const [injectionToken] = useState(() => ctx?.injectionToken ?? uniqueId(Graph.name));
|
|
9
|
+
return injectionToken;
|
|
10
|
+
};
|