@electron-tools/ioc
Advanced tools
Comparing version 1.0.7 to 1.0.8
@@ -18,19 +18,2 @@ var __defProp = Object.defineProperty; | ||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
var __accessCheck = (obj, member, msg) => { | ||
if (!member.has(obj)) | ||
throw TypeError("Cannot " + msg); | ||
}; | ||
var __privateGet = (obj, member, getter) => { | ||
__accessCheck(obj, member, "read from private field"); | ||
return getter ? getter.call(obj) : member.get(obj); | ||
}; | ||
var __privateAdd = (obj, member, value) => { | ||
if (member.has(obj)) | ||
throw TypeError("Cannot add the same private member more than once"); | ||
member instanceof WeakSet ? member.add(obj) : member.set(obj, value); | ||
}; | ||
var __privateMethod = (obj, member, method) => { | ||
__accessCheck(obj, member, "access private method"); | ||
return method; | ||
}; | ||
@@ -138,13 +121,9 @@ // src/index.ts | ||
var INSTANTIATION_SERVICE_ID = Symbol.for("instantiationService"); | ||
var _serviceStore, _createAndCacheService, createAndCacheService_fn, _getServiceDependencies, getServiceDependencies_fn, _getServiceCtorById, getServiceCtorById_fn; | ||
var InstantiationService = class { | ||
constructor() { | ||
__privateAdd(this, _createAndCacheService); | ||
__privateAdd(this, _getServiceDependencies); | ||
__privateAdd(this, _getServiceCtorById); | ||
__privateAdd(this, _serviceStore, /* @__PURE__ */ new Map()); | ||
__privateGet(this, _serviceStore).set(INSTANTIATION_SERVICE_ID, this); | ||
this.serviceStore = /* @__PURE__ */ new Map(); | ||
this.serviceStore.set(INSTANTIATION_SERVICE_ID, this); | ||
} | ||
get services() { | ||
return __privateGet(this, _serviceStore); | ||
return this.serviceStore; | ||
} | ||
@@ -157,63 +136,59 @@ init() { | ||
registerService(id, service2) { | ||
__privateGet(this, _serviceStore).set(id, service2); | ||
this.serviceStore.set(id, service2); | ||
} | ||
getService(id) { | ||
if (__privateGet(this, _serviceStore).has(id)) | ||
return __privateGet(this, _serviceStore).get(id); | ||
return __privateMethod(this, _createAndCacheService, createAndCacheService_fn).call(this, id); | ||
if (this.serviceStore.has(id)) | ||
return this.serviceStore.get(id); | ||
return this.createAndCacheService(id); | ||
} | ||
}; | ||
_serviceStore = new WeakMap(); | ||
_createAndCacheService = new WeakSet(); | ||
createAndCacheService_fn = function(serviceId) { | ||
const ServiceCtor = __privateMethod(this, _getServiceCtorById, getServiceCtorById_fn).call(this, serviceId); | ||
if (!ServiceCtor) | ||
throw new Error(`[InstantiationService] service ${serviceId} not found!`); | ||
const graph = new Graph((node) => node.serviceId.toString()); | ||
const stack = [{ ctor: ServiceCtor, serviceId }]; | ||
while (stack.length) { | ||
const node = stack.pop(); | ||
graph.lookupOrInsertNode(node); | ||
const dependencies = (__privateMethod(this, _getServiceDependencies, getServiceDependencies_fn).call(this, node.ctor) || []).sort((a, b) => a.parameterIndex - b.parameterIndex); | ||
for (const dependency of dependencies) { | ||
if (__privateGet(this, _serviceStore).has(dependency.id)) | ||
continue; | ||
const ServiceCtor2 = __privateMethod(this, _getServiceCtorById, getServiceCtorById_fn).call(this, dependency.id); | ||
const dependencyNode = { ctor: ServiceCtor2, serviceId: dependency.id }; | ||
if (!graph.lookup(dependencyNode)) { | ||
stack.push(dependencyNode); | ||
createAndCacheService(serviceId) { | ||
const ServiceCtor = this.getServiceCtorById(serviceId); | ||
if (!ServiceCtor) | ||
throw new Error(`[InstantiationService] service ${serviceId} not found!`); | ||
const graph = new Graph((node) => node.serviceId.toString()); | ||
const stack = [{ ctor: ServiceCtor, serviceId }]; | ||
while (stack.length) { | ||
const node = stack.pop(); | ||
graph.lookupOrInsertNode(node); | ||
const dependencies = (this.getServiceDependencies(node.ctor) || []).sort((a, b) => a.parameterIndex - b.parameterIndex); | ||
for (const dependency of dependencies) { | ||
if (this.serviceStore.has(dependency.id)) | ||
continue; | ||
const ServiceCtor2 = this.getServiceCtorById(dependency.id); | ||
const dependencyNode = { ctor: ServiceCtor2, serviceId: dependency.id }; | ||
if (!graph.lookup(dependencyNode)) { | ||
stack.push(dependencyNode); | ||
} | ||
graph.insertEdge(node, dependencyNode); | ||
} | ||
graph.insertEdge(node, dependencyNode); | ||
} | ||
} | ||
while (true) { | ||
const roots = graph.roots(); | ||
if (roots.length === 0) { | ||
if (!graph.isEmpty()) { | ||
throw new CyclicDependencyError(graph); | ||
while (true) { | ||
const roots = graph.roots(); | ||
if (roots.length === 0) { | ||
if (!graph.isEmpty()) { | ||
throw new CyclicDependencyError(graph); | ||
} | ||
break; | ||
} | ||
break; | ||
for (const root of roots) { | ||
const { ctor: ServiceCtor2, serviceId: serviceId2 } = root.data; | ||
const dependencies = this.getServiceDependencies(ServiceCtor2) || []; | ||
const args = dependencies.map(({ id }) => this.getService(id)); | ||
const service2 = new ServiceCtor2(...args); | ||
this.serviceStore.set(serviceId2, service2); | ||
graph.removeNode(root.data); | ||
} | ||
} | ||
for (const root of roots) { | ||
const { ctor: ServiceCtor2, serviceId: serviceId2 } = root.data; | ||
const dependencies = __privateMethod(this, _getServiceDependencies, getServiceDependencies_fn).call(this, ServiceCtor2) || []; | ||
const args = dependencies.map(({ id }) => this.getService(id)); | ||
const service2 = new ServiceCtor2(...args); | ||
__privateGet(this, _serviceStore).set(serviceId2, service2); | ||
graph.removeNode(root.data); | ||
return this.getService(serviceId); | ||
} | ||
getServiceDependencies(Ctor) { | ||
return Reflect.getOwnMetadata(dependencyMetadataKey, Ctor); | ||
} | ||
getServiceCtorById(id) { | ||
if (!serviceCtorStore.has(id)) { | ||
throw new Error(`service ${id} not found!`); | ||
} | ||
return serviceCtorStore.get(id); | ||
} | ||
return this.getService(serviceId); | ||
}; | ||
_getServiceDependencies = new WeakSet(); | ||
getServiceDependencies_fn = function(Ctor) { | ||
return Reflect.getOwnMetadata(dependencyMetadataKey, Ctor); | ||
}; | ||
_getServiceCtorById = new WeakSet(); | ||
getServiceCtorById_fn = function(id) { | ||
if (!serviceCtorStore.has(id)) { | ||
throw new Error(`service ${id} not found!`); | ||
} | ||
return serviceCtorStore.get(id); | ||
}; | ||
var CyclicDependencyError = class extends Error { | ||
@@ -220,0 +195,0 @@ constructor(graph) { |
@@ -1,19 +0,1 @@ | ||
var __accessCheck = (obj, member, msg) => { | ||
if (!member.has(obj)) | ||
throw TypeError("Cannot " + msg); | ||
}; | ||
var __privateGet = (obj, member, getter) => { | ||
__accessCheck(obj, member, "read from private field"); | ||
return getter ? getter.call(obj) : member.get(obj); | ||
}; | ||
var __privateAdd = (obj, member, value) => { | ||
if (member.has(obj)) | ||
throw TypeError("Cannot add the same private member more than once"); | ||
member instanceof WeakSet ? member.add(obj) : member.set(obj, value); | ||
}; | ||
var __privateMethod = (obj, member, method) => { | ||
__accessCheck(obj, member, "access private method"); | ||
return method; | ||
}; | ||
// src/instantiation.ts | ||
@@ -109,13 +91,9 @@ import "reflect-metadata"; | ||
var INSTANTIATION_SERVICE_ID = Symbol.for("instantiationService"); | ||
var _serviceStore, _createAndCacheService, createAndCacheService_fn, _getServiceDependencies, getServiceDependencies_fn, _getServiceCtorById, getServiceCtorById_fn; | ||
var InstantiationService = class { | ||
constructor() { | ||
__privateAdd(this, _createAndCacheService); | ||
__privateAdd(this, _getServiceDependencies); | ||
__privateAdd(this, _getServiceCtorById); | ||
__privateAdd(this, _serviceStore, /* @__PURE__ */ new Map()); | ||
__privateGet(this, _serviceStore).set(INSTANTIATION_SERVICE_ID, this); | ||
this.serviceStore = /* @__PURE__ */ new Map(); | ||
this.serviceStore.set(INSTANTIATION_SERVICE_ID, this); | ||
} | ||
get services() { | ||
return __privateGet(this, _serviceStore); | ||
return this.serviceStore; | ||
} | ||
@@ -128,63 +106,59 @@ init() { | ||
registerService(id, service2) { | ||
__privateGet(this, _serviceStore).set(id, service2); | ||
this.serviceStore.set(id, service2); | ||
} | ||
getService(id) { | ||
if (__privateGet(this, _serviceStore).has(id)) | ||
return __privateGet(this, _serviceStore).get(id); | ||
return __privateMethod(this, _createAndCacheService, createAndCacheService_fn).call(this, id); | ||
if (this.serviceStore.has(id)) | ||
return this.serviceStore.get(id); | ||
return this.createAndCacheService(id); | ||
} | ||
}; | ||
_serviceStore = new WeakMap(); | ||
_createAndCacheService = new WeakSet(); | ||
createAndCacheService_fn = function(serviceId) { | ||
const ServiceCtor = __privateMethod(this, _getServiceCtorById, getServiceCtorById_fn).call(this, serviceId); | ||
if (!ServiceCtor) | ||
throw new Error(`[InstantiationService] service ${serviceId} not found!`); | ||
const graph = new Graph((node) => node.serviceId.toString()); | ||
const stack = [{ ctor: ServiceCtor, serviceId }]; | ||
while (stack.length) { | ||
const node = stack.pop(); | ||
graph.lookupOrInsertNode(node); | ||
const dependencies = (__privateMethod(this, _getServiceDependencies, getServiceDependencies_fn).call(this, node.ctor) || []).sort((a, b) => a.parameterIndex - b.parameterIndex); | ||
for (const dependency of dependencies) { | ||
if (__privateGet(this, _serviceStore).has(dependency.id)) | ||
continue; | ||
const ServiceCtor2 = __privateMethod(this, _getServiceCtorById, getServiceCtorById_fn).call(this, dependency.id); | ||
const dependencyNode = { ctor: ServiceCtor2, serviceId: dependency.id }; | ||
if (!graph.lookup(dependencyNode)) { | ||
stack.push(dependencyNode); | ||
createAndCacheService(serviceId) { | ||
const ServiceCtor = this.getServiceCtorById(serviceId); | ||
if (!ServiceCtor) | ||
throw new Error(`[InstantiationService] service ${serviceId} not found!`); | ||
const graph = new Graph((node) => node.serviceId.toString()); | ||
const stack = [{ ctor: ServiceCtor, serviceId }]; | ||
while (stack.length) { | ||
const node = stack.pop(); | ||
graph.lookupOrInsertNode(node); | ||
const dependencies = (this.getServiceDependencies(node.ctor) || []).sort((a, b) => a.parameterIndex - b.parameterIndex); | ||
for (const dependency of dependencies) { | ||
if (this.serviceStore.has(dependency.id)) | ||
continue; | ||
const ServiceCtor2 = this.getServiceCtorById(dependency.id); | ||
const dependencyNode = { ctor: ServiceCtor2, serviceId: dependency.id }; | ||
if (!graph.lookup(dependencyNode)) { | ||
stack.push(dependencyNode); | ||
} | ||
graph.insertEdge(node, dependencyNode); | ||
} | ||
graph.insertEdge(node, dependencyNode); | ||
} | ||
} | ||
while (true) { | ||
const roots = graph.roots(); | ||
if (roots.length === 0) { | ||
if (!graph.isEmpty()) { | ||
throw new CyclicDependencyError(graph); | ||
while (true) { | ||
const roots = graph.roots(); | ||
if (roots.length === 0) { | ||
if (!graph.isEmpty()) { | ||
throw new CyclicDependencyError(graph); | ||
} | ||
break; | ||
} | ||
break; | ||
for (const root of roots) { | ||
const { ctor: ServiceCtor2, serviceId: serviceId2 } = root.data; | ||
const dependencies = this.getServiceDependencies(ServiceCtor2) || []; | ||
const args = dependencies.map(({ id }) => this.getService(id)); | ||
const service2 = new ServiceCtor2(...args); | ||
this.serviceStore.set(serviceId2, service2); | ||
graph.removeNode(root.data); | ||
} | ||
} | ||
for (const root of roots) { | ||
const { ctor: ServiceCtor2, serviceId: serviceId2 } = root.data; | ||
const dependencies = __privateMethod(this, _getServiceDependencies, getServiceDependencies_fn).call(this, ServiceCtor2) || []; | ||
const args = dependencies.map(({ id }) => this.getService(id)); | ||
const service2 = new ServiceCtor2(...args); | ||
__privateGet(this, _serviceStore).set(serviceId2, service2); | ||
graph.removeNode(root.data); | ||
return this.getService(serviceId); | ||
} | ||
getServiceDependencies(Ctor) { | ||
return Reflect.getOwnMetadata(dependencyMetadataKey, Ctor); | ||
} | ||
getServiceCtorById(id) { | ||
if (!serviceCtorStore.has(id)) { | ||
throw new Error(`service ${id} not found!`); | ||
} | ||
return serviceCtorStore.get(id); | ||
} | ||
return this.getService(serviceId); | ||
}; | ||
_getServiceDependencies = new WeakSet(); | ||
getServiceDependencies_fn = function(Ctor) { | ||
return Reflect.getOwnMetadata(dependencyMetadataKey, Ctor); | ||
}; | ||
_getServiceCtorById = new WeakSet(); | ||
getServiceCtorById_fn = function(id) { | ||
if (!serviceCtorStore.has(id)) { | ||
throw new Error(`service ${id} not found!`); | ||
} | ||
return serviceCtorStore.get(id); | ||
}; | ||
var CyclicDependencyError = class extends Error { | ||
@@ -191,0 +165,0 @@ constructor(graph) { |
@@ -0,1 +1,7 @@ | ||
declare type DependenciesValue = Array<{ | ||
id: string; | ||
parameterKey: string; | ||
parameterIndex: number; | ||
}>; | ||
export declare function inject(id: ServiceUniqueId): (Ctor: ServiceCtor, parameterKey: string, parameterIndex: number) => void; | ||
@@ -6,3 +12,3 @@ | ||
declare class InstantiationService { | ||
#private; | ||
private readonly serviceStore; | ||
get services(): Map<ServiceUniqueId, unknown>; | ||
@@ -13,2 +19,5 @@ constructor(); | ||
getService<S = any>(id: ServiceUniqueId): S; | ||
createAndCacheService<S = any>(serviceId: ServiceUniqueId): S; | ||
getServiceDependencies(Ctor: ServiceCtor): DependenciesValue; | ||
getServiceCtorById(id: ServiceUniqueId): ServiceCtor; | ||
} | ||
@@ -15,0 +24,0 @@ export default InstantiationService; |
{ | ||
"name": "@electron-tools/ioc", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"description": "DI implement of IOC", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
47486
470