@kaokei/di 3.0.2 → 3.0.4
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.cjs +1 -1
- package/dist/index.js +8 -8
- package/dist/interfaces.d.cts +1 -0
- package/dist/interfaces.d.ts +1 -0
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var w=Object.defineProperty;var _=(n,t,e)=>t in n?w(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e;var
|
|
1
|
+
"use strict";var w=Object.defineProperty;var _=(n,t,e)=>t in n?w(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e;var a=(n,t,e)=>_(n,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const h={INJECTED_PARAMS:"injected:params",INJECTED_PROPS:"injected:props",INJECT:"inject",SELF:"self",SKIP_SELF:"skipSelf",OPTIONAL:"optional",POST_CONSTRUCT:"postConstruct",PRE_DESTROY:"preDestroy"},p={DEFAULT:"default",INITING:"initing",ACTIVATED:"activated"},d={Invalid:"Invalid",Instance:"Instance",ConstantValue:"ConstantValue",DynamicValue:"DynamicValue"},I={POST_CONSTRUCT:"Cannot apply @PostConstruct decorator multiple times in the same class.",PRE_DESTROY:"Cannot apply @PreDestroy decorator multiple times in the same class.",MISS_INJECT:"Expected a @Inject decorator to explicitly specify the token.",MISS_CONTAINER:"@LazyInject decorator cannot find the corresponding container."},P=Symbol(),S=new WeakMap;function j(n){return typeof n=="function"&&Object.getPrototypeOf(n)!==Function.prototype}function N(n,t,e){const s=S.get(e)||{};s[n]=t,S.set(e,s)}function C(n,t){return(S.get(t)||{})[n]}function g(n,t){const e=C(n,t);if(!j(t))return e;const s=g(n,Object.getPrototypeOf(t));if(s||e)return{...s||{},...e||{}}}class B{constructor(t){a(this,"_","");a(this,"name");this.name=t}}class O{constructor(t){a(this,"callback");this.callback=t}resolve(){return this.callback()}}function v(n){if(!n)throw new Error(I.MISS_INJECT);return n instanceof O?n.resolve():n}class T extends Error{constructor(t,e){super(),this.name=this.constructor.name,this.message=`${t}${e==null?void 0:e.name}`}}class b extends T{constructor(t){super("");const e=[];let s=t;for(;s&&s.token;)e.push(s.token),s=s.parent;const i=e.reverse().map(o=>o.name).join(" --> ");this.message=`Circular dependency found: ${i}`}}class k extends T{constructor(t){super("Invalid binding: ",t)}}class M extends b{constructor(t){super(t),this.name="CircularDependencyError inside @PostConstruct"}}class L{constructor(t,e){a(this,"container");a(this,"context");a(this,"token");a(this,"type",d.Invalid);a(this,"status",p.DEFAULT);a(this,"classValue");a(this,"constantValue");a(this,"dynamicValue");a(this,"cache");a(this,"postConstructResult",P);a(this,"onActivationHandler");a(this,"onDeactivationHandler");this.container=e,this.context={container:this.container},this.token=t}onActivation(t){this.onActivationHandler=t}onDeactivation(t){this.onDeactivationHandler=t}activate(t){const e=this.onActivationHandler?this.onActivationHandler(this.context,t):t;return this.container.activate(e,this.token)}deactivate(){this.onDeactivationHandler&&this.onDeactivationHandler(this.cache)}to(t){return this.type=d.Instance,this.classValue=t,this}toSelf(){return this.to(this.token)}toConstantValue(t){return this.type=d.ConstantValue,this.constantValue=t,this}toDynamicValue(t){return this.type=d.DynamicValue,this.dynamicValue=t,this}toService(t){return this.toDynamicValue(e=>e.container.get(t,{parent:{token:this.token}}))}get(t){if(p.INITING===this.status)throw new b(t);if(p.ACTIVATED===this.status)return this.cache;if(d.Instance===this.type)return this.resolveInstanceValue(t);if(d.ConstantValue===this.type)return this.resolveConstantValue();if(d.DynamicValue===this.type)return this.resolveDynamicValue();throw new k(this.token)}getAwaitBindings(t,e){return e===!0?t:Array.isArray(e)?t.filter(s=>e.includes(s.token)):typeof e=="function"?t.filter(e):[]}postConstruct(t,e,s){if(d.Instance===this.type){const{key:i,value:o}=g(h.POST_CONSTRUCT,this.classValue)||{};if(i)if(o){const r=[...e,...s].filter(u=>d.Instance===(u==null?void 0:u.type)),c=this.getAwaitBindings(r,o);for(const u of c)if(u&&u.postConstructResult===P)throw new M({token:u.token,parent:t});const l=c.map(u=>u.postConstructResult);this.postConstructResult=Promise.all(l).then(()=>this.execute(i))}else this.postConstructResult=this.execute(i);else this.postConstructResult=void 0}}preDestroy(){if(d.Instance===this.type){const{key:t}=g(h.PRE_DESTROY,this.classValue)||{};if(t)return this.execute(t)}this.container=null,this.context=null}execute(t){const e=this.cache[t];return e==null?void 0:e.call(this.cache)}resolveInstanceValue(t){this.status=p.INITING;const e=this.classValue,[s,i]=this.getConstructorParameters(t),o=new e(...s);this.cache=this.activate(o),this.status=p.ACTIVATED,m.set(this.cache,this.container);const[r,c]=this.getInjectProperties(t);return Object.assign(this.cache,r),this.postConstruct(t,i,c),this.cache}resolveConstantValue(){return this.status=p.INITING,this.cache=this.activate(this.constantValue),this.status=p.ACTIVATED,this.cache}resolveDynamicValue(){this.status=p.INITING;const t=this.dynamicValue.call(this,this.context);return this.cache=this.activate(t),this.status=p.ACTIVATED,this.cache}getConstructorParameters(t){const e=C(h.INJECTED_PARAMS,this.classValue)||[],s=[],i=[];for(let o=0;o<e.length;o++){const r=e[o],{inject:c,...l}=r;l.parent=t;const u=this.container.get(v(c),l);s.push(u),i.push(l.binding)}return[s,i]}getInjectProperties(t){const e=g(h.INJECTED_PROPS,this.classValue)||{},s=Object.keys(e),i=Object.create(null),o=[];for(let r=0;r<s.length;r++){const c=s[r],l=e[c],{inject:u,...f}=l;f.parent=t;const y=this.container.get(v(u),f);y===void 0&&l.optional||(i[c]=y),o.push(f.binding)}return[i,o]}}class x extends T{constructor(t){super("No matching binding found for token: ",t)}}class H extends T{constructor(t){super("Cannot bind token multiple times: ",t)}}class D{constructor(){a(this,"parent");a(this,"bindings",new Map);a(this,"onActivationHandler");a(this,"onDeactivationHandler")}bind(t){if(this.bindings.has(t))throw new H(t);const e=this.buildBinding(t);return this.bindings.set(t,e),e}unbind(t){if(this.bindings.has(t)){const e=this.getBinding(t);this.deactivate(e),e.preDestroy(),this.bindings.delete(t)}}unbindAll(){this.bindings.forEach(t=>{this.unbind(t.token)})}isCurrentBound(t){return this.bindings.has(t)}isBound(t){return this.isCurrentBound(t)||!!this.parent&&this.parent.isBound(t)}createChild(){const t=new D;return t.parent=this,t}get(t,e={}){const s=this.getBinding(t);if(e.skipSelf){if(this.parent)return e.skipSelf=!1,this.parent.get(t,e)}else if(e.self||s){if(s)return e.token=t,e.binding=s,s.get(e)}else if(this.parent)return this.parent.get(t,e);return this.checkBindingNotFoundError(t,e)}onActivation(t){this.onActivationHandler=t}onDeactivation(t){this.onDeactivationHandler=t}activate(t,e){return this.onActivationHandler?this.onActivationHandler({container:this},t,e):t}deactivate(t){this.onDeactivationHandler&&this.onDeactivationHandler(t.cache,t.token),t.deactivate()}buildBinding(t){return new L(t,this)}getBinding(t){return this.bindings.get(t)}checkBindingNotFoundError(t,e){if(!e.optional)throw new x(t)}}const m=new WeakMap;function E(n,t){return function(e){return function(s,i,o){const r=typeof o=="number",c=r?s:s.constructor,l=r?o:i,u=r?h.INJECTED_PARAMS:h.INJECTED_PROPS,f=r?C(u,c)||[]:g(u,c)||{},y=f[l]||{};y[n]=e===void 0?t:e,f[l]=y,N(u,f,c)}}}function V(n,t){return e=>(s,i)=>{if(C(n,s.constructor))throw new Error(t);N(n,{key:i,value:e},s.constructor)}}const F=E(h.INJECT),J=E(h.SELF,!0),U=E(h.SKIP_SELF,!0),z=E(h.OPTIONAL,!0),G=V(h.POST_CONSTRUCT,I.POST_CONSTRUCT),Y=V(h.PRE_DESTROY,I.PRE_DESTROY);function A(n,t,e,s){for(let i=n.length-1;i>=0;i--)n[i](t,e,s)}function K(n,t,e){n=Array.isArray(n)?n:[n],typeof e=="number"?A(n,t,void 0,e):typeof e=="string"&&A(n,t.prototype,e)}function $(n,t,e,s){function i(){const r=Symbol.for(t);if(!this.hasOwnProperty(r)){const c=s||m.get(this),l=this.constructor;if(!c)throw new Error(`${I.MISS_CONTAINER} ${l.name}`);this[r]=c.get(v(e),{parent:{token:l}})}return this[r]}function o(r){const c=Symbol.for(t);this[c]=r}Object.defineProperty(n,t,{configurable:!0,enumerable:!0,get:i,set:o})}function R(n,t){return function(e,s){$(e,s,n,t)}}function W(n){return function(t){return R(t,n)}}exports.CONTAINER_MAP=m;exports.Container=D;exports.Inject=F;exports.LazyInject=R;exports.LazyToken=O;exports.Optional=z;exports.PostConstruct=G;exports.PreDestroy=Y;exports.Self=J;exports.SkipSelf=U;exports.Token=B;exports.createLazyInject=W;exports.decorate=K;
|
package/dist/index.js
CHANGED
|
@@ -99,12 +99,12 @@ class B extends E {
|
|
|
99
99
|
super("Invalid binding: ", t);
|
|
100
100
|
}
|
|
101
101
|
}
|
|
102
|
-
class
|
|
102
|
+
class j extends P {
|
|
103
103
|
constructor(t) {
|
|
104
104
|
super(t), this.name = "CircularDependencyError inside @PostConstruct";
|
|
105
105
|
}
|
|
106
106
|
}
|
|
107
|
-
class
|
|
107
|
+
class M {
|
|
108
108
|
constructor(t, n) {
|
|
109
109
|
a(this, "container");
|
|
110
110
|
a(this, "context");
|
|
@@ -168,7 +168,7 @@ class j {
|
|
|
168
168
|
}
|
|
169
169
|
postConstruct(t, n, s) {
|
|
170
170
|
if (d.Instance === this.type) {
|
|
171
|
-
const { key: i, value: o } = y(l.POST_CONSTRUCT, this.
|
|
171
|
+
const { key: i, value: o } = y(l.POST_CONSTRUCT, this.classValue) || {};
|
|
172
172
|
if (i)
|
|
173
173
|
if (o) {
|
|
174
174
|
const r = [...n, ...s].filter(
|
|
@@ -176,7 +176,7 @@ class j {
|
|
|
176
176
|
), c = this.getAwaitBindings(r, o);
|
|
177
177
|
for (const u of c)
|
|
178
178
|
if (u && u.postConstructResult === S)
|
|
179
|
-
throw new
|
|
179
|
+
throw new j({
|
|
180
180
|
token: u.token,
|
|
181
181
|
parent: t
|
|
182
182
|
});
|
|
@@ -192,7 +192,7 @@ class j {
|
|
|
192
192
|
}
|
|
193
193
|
preDestroy() {
|
|
194
194
|
if (d.Instance === this.type) {
|
|
195
|
-
const { key: t } = y(l.PRE_DESTROY, this.
|
|
195
|
+
const { key: t } = y(l.PRE_DESTROY, this.classValue) || {};
|
|
196
196
|
if (t)
|
|
197
197
|
return this.execute(t);
|
|
198
198
|
}
|
|
@@ -238,7 +238,7 @@ class j {
|
|
|
238
238
|
return [i, o];
|
|
239
239
|
}
|
|
240
240
|
}
|
|
241
|
-
class
|
|
241
|
+
class k extends E {
|
|
242
242
|
constructor(t) {
|
|
243
243
|
super("No matching binding found for token: ", t);
|
|
244
244
|
}
|
|
@@ -307,14 +307,14 @@ class N {
|
|
|
307
307
|
this.onDeactivationHandler && this.onDeactivationHandler(t.cache, t.token), t.deactivate();
|
|
308
308
|
}
|
|
309
309
|
buildBinding(t) {
|
|
310
|
-
return new
|
|
310
|
+
return new M(t, this);
|
|
311
311
|
}
|
|
312
312
|
getBinding(t) {
|
|
313
313
|
return this.bindings.get(t);
|
|
314
314
|
}
|
|
315
315
|
checkBindingNotFoundError(t, n) {
|
|
316
316
|
if (!n.optional)
|
|
317
|
-
throw new
|
|
317
|
+
throw new k(t);
|
|
318
318
|
}
|
|
319
319
|
}
|
|
320
320
|
const b = /* @__PURE__ */ new WeakMap();
|
package/dist/interfaces.d.cts
CHANGED
|
@@ -5,6 +5,7 @@ import { KEYS } from './constants';
|
|
|
5
5
|
export type Newable<TInstance = unknown, TArgs extends unknown[] = any[]> = new (...args: TArgs) => TInstance;
|
|
6
6
|
export type InjectFunction<R extends (...args: any) => any> = (token: GenericToken) => ReturnType<R>;
|
|
7
7
|
export type CommonToken<T = unknown> = Token<T> | Newable<T>;
|
|
8
|
+
export type TokenType<T> = T extends CommonToken<infer U> ? U : never;
|
|
8
9
|
export type GenericToken<T = unknown> = Token<T> | Newable<T> | LazyToken<T>;
|
|
9
10
|
export type LazyTokenCallback<T = unknown> = () => CommonToken<T>;
|
|
10
11
|
export interface Context {
|
package/dist/interfaces.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { KEYS } from './constants';
|
|
|
5
5
|
export type Newable<TInstance = unknown, TArgs extends unknown[] = any[]> = new (...args: TArgs) => TInstance;
|
|
6
6
|
export type InjectFunction<R extends (...args: any) => any> = (token: GenericToken) => ReturnType<R>;
|
|
7
7
|
export type CommonToken<T = unknown> = Token<T> | Newable<T>;
|
|
8
|
+
export type TokenType<T> = T extends CommonToken<infer U> ? U : never;
|
|
8
9
|
export type GenericToken<T = unknown> = Token<T> | Newable<T> | LazyToken<T>;
|
|
9
10
|
export type LazyTokenCallback<T = unknown> = () => CommonToken<T>;
|
|
10
11
|
export interface Context {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kaokei/di",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Tiny di library depends on typescript decorator.",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"@types/fs-extra": "^11.0.4",
|
|
51
51
|
"@types/node": "^22.13.4",
|
|
52
52
|
"@vitejs/plugin-vue": "^5.2.1",
|
|
53
|
-
"@vitest/coverage-v8": "^3.
|
|
53
|
+
"@vitest/coverage-v8": "^3.1.1",
|
|
54
54
|
"@vue/test-utils": "^2.4.6",
|
|
55
55
|
"fs-extra": "^11.3.0",
|
|
56
56
|
"inversify": "^6.2.2",
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
"typescript": "~5.6.3",
|
|
61
61
|
"vite": "^5.4.14",
|
|
62
62
|
"vite-plugin-dts": "^4.5.0",
|
|
63
|
-
"vitest": "^3.
|
|
63
|
+
"vitest": "^3.1.1",
|
|
64
64
|
"vue": "^3.5.13",
|
|
65
65
|
"vue-router": "^4.5.0",
|
|
66
66
|
"vue-tsc": "^2.2.2"
|