@electron-tools/ioc
Advanced tools
Comparing version 1.0.0 to 1.0.1
export { default } from './instantiation'; | ||
export { service, inject } from './instantiation'; | ||
export { service, inject, InstantiationServiceID } from './instantiation'; | ||
//# sourceMappingURL=index.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.inject = exports.service = exports.default = void 0; | ||
exports.InstantiationServiceID = exports.inject = exports.service = exports.default = void 0; | ||
var instantiation_1 = require("./instantiation"); | ||
@@ -9,2 +9,3 @@ Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(instantiation_1).default; } }); | ||
Object.defineProperty(exports, "inject", { enumerable: true, get: function () { return instantiation_2.inject; } }); | ||
Object.defineProperty(exports, "InstantiationServiceID", { enumerable: true, get: function () { return instantiation_2.InstantiationServiceID; } }); | ||
//# sourceMappingURL=index.js.map |
import 'reflect-metadata'; | ||
declare type ServiceUniqueId = string | Symbol; | ||
export declare const InstantiationServiceID: unique symbol; | ||
export default class InstantiationService { | ||
#private; | ||
get services(): Map<string, unknown>; | ||
get services(): Map<ServiceUniqueId, unknown>; | ||
constructor(); | ||
init(): void; | ||
getService<T = any>(id: string): T; | ||
getService<S = any>(id: ServiceUniqueId): S; | ||
} | ||
@@ -17,5 +19,5 @@ export declare type DependenciesValue = Array<{ | ||
} | ||
export declare function service(id?: string): (Ctor: ServiceCtor) => void; | ||
export declare function inject(id: string): (Ctor: ServiceCtor, parameterKey: string, parameterIndex: number) => void; | ||
export declare function service(id?: ServiceUniqueId): (Ctor: ServiceCtor) => void; | ||
export declare function inject(id: ServiceUniqueId): (Ctor: ServiceCtor, parameterKey: string, parameterIndex: number) => void; | ||
export {}; | ||
//# sourceMappingURL=instantiation.d.ts.map |
"use strict"; | ||
var _InstantiationService_instances, _InstantiationService_serviceStore, _InstantiationService_createAndCacheService, _InstantiationService_getServiceDependencies, _InstantiationService_getServiceCtorById; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.inject = exports.service = void 0; | ||
exports.inject = exports.service = exports.InstantiationServiceID = void 0; | ||
const tslib_1 = require("tslib"); | ||
@@ -9,2 +9,3 @@ require("reflect-metadata"); | ||
const serviceCtorStore = new Map(); | ||
exports.InstantiationServiceID = Symbol.for('instantiationService'); | ||
class InstantiationService { | ||
@@ -14,3 +15,3 @@ constructor() { | ||
_InstantiationService_serviceStore.set(this, new Map()); | ||
tslib_1.__classPrivateFieldGet(this, _InstantiationService_serviceStore, "f").set('instantiationService', this); | ||
tslib_1.__classPrivateFieldGet(this, _InstantiationService_serviceStore, "f").set(exports.InstantiationServiceID, this); | ||
} | ||
@@ -36,3 +37,3 @@ get services() { | ||
throw new Error(`[InstantiationService] service ${serviceId} not found!`); | ||
const graph = new graph_1.default((node) => node.serviceId); | ||
const graph = new graph_1.default((node) => node.serviceId.toString()); | ||
const stack = [{ ctor: ServiceCtor, serviceId }]; | ||
@@ -39,0 +40,0 @@ while (stack.length) { |
{ | ||
"name": "@electron-tools/ioc", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "DI implement of IOC", | ||
@@ -9,7 +9,2 @@ "main": "dist/index.js", | ||
], | ||
"scripts": { | ||
"dev": "tsc --watch --project tsconfig.json", | ||
"build": "tsc --project tsconfig.json", | ||
"test": "jest" | ||
}, | ||
"author": "heychenfq <heychenfq@foxmail.com>", | ||
@@ -22,10 +17,9 @@ "license": "MIT", | ||
"devDependencies": { | ||
"@types/jest": "^27.5.1", | ||
"@types/node": "^17.0.35", | ||
"jest": "^28.1.0", | ||
"ts-jest": "^28.0.2", | ||
"ts-node": "^10.8.0", | ||
"typescript": "^4.6.4" | ||
}, | ||
"types": "./dist/index.d.ts" | ||
} | ||
"types": "./dist/index.d.ts", | ||
"scripts": { | ||
"dev": "tsc -w", | ||
"build": "tsc" | ||
} | ||
} |
@@ -1,2 +0,1 @@ | ||
# Introduction | ||
@@ -8,10 +7,10 @@ | ||
```bash | ||
````bash | ||
# install by npm | ||
$npm install @electron-toolkit/ioc | ||
$npm install @electron-tools/ioc | ||
# install by pnpm | ||
$pnpm add @electron-toolkit/ioc | ||
$pnpm add @electron-tools/ioc | ||
# install by yarn | ||
$yarn add @electron-toolkit/ioc | ||
``` | ||
$yarn add @electron-tools/ioc | ||
```~ | ||
@@ -26,3 +25,3 @@ # Usage | ||
// src/serviceA.ts | ||
import { service } from '@electron-toolkit/ioc'; | ||
import { service } from '@electron-tools/ioc'; | ||
@@ -33,3 +32,3 @@ @service('serviceA') // register ServiceA with unique id 'serviceA' using service decorator exported by this package. | ||
} | ||
``` | ||
```` | ||
@@ -40,3 +39,3 @@ 3. use `inject` decorator inject your service to another service. | ||
// src/serviceB.ts | ||
import { service, inject } from '@electron-toolkit/ioc'; | ||
import { service, inject } from '@electron-tools/ioc'; | ||
@@ -48,3 +47,3 @@ @service('serviceB') // also register ServiceB with unique id 'serviceB' | ||
readonly serviceA: ServiceA, | ||
) | ||
) {} | ||
} | ||
@@ -57,3 +56,3 @@ ``` | ||
// src/index.ts | ||
import IOC from '@electron-toolkit/ioc'; | ||
import IOC from '@electron-tools/ioc'; | ||
import './serviceA.ts'; | ||
@@ -66,3 +65,3 @@ import './serviceB.ts'; | ||
console.log(serviceA instanceof ServiceA); // true | ||
console.log(serviceB instanceof ServiceA); // true | ||
console.log(serviceB instanceof ServiceB); // true | ||
console.log(serviceA === serviceB.a); // true | ||
@@ -74,3 +73,3 @@ ``` | ||
1. Instance all service in one place. | ||
by default. service only be instanced when needed. in the case above. if you only call `ioc.getService('serviceA')`, serviceB will not be instance, cause serviceB is not dependencied by any service, but if you only call `ioc.getService('serviceB')`, serviceA will be instance, and inject into serviceB. this maybe not what you want. you can init all services in one place by call `ioc.init()`. | ||
by default. service only be instanced when needed. in the case above. if you only call `ioc.getService('serviceA')`, serviceB will not be instance, cause serviceB is not dependencied by any service, but if you only call `ioc.getService('serviceB')`, serviceA will be instance, and inject into serviceB. this maybe not what you want. you can init all services in one place by call `ioc.init()`. | ||
@@ -83,7 +82,7 @@ ```typescript | ||
2. Cycle reference. | ||
if there are some cycle reference between your services. such as serviceA dependencied by serviceB, serviceB also dependencied by serviceA, you can resolve this issue by get service later instead of constructor of service. | ||
if there are some cycle reference between your services. such as serviceA dependencied by serviceB, serviceB also dependencied by serviceA, you can resolve this issue by get service later instead of constructor of service. | ||
``` typescript | ||
```typescript | ||
// src/serviceA.ts | ||
import IOC, { service, inject } from '@electron-toolkit/ioc'; | ||
import IOC, { service, inject } from '@electron-tools/ioc'; | ||
@@ -94,3 +93,3 @@ @service('serviceA') // register ServiceA with unique id 'serviceA' using service decorator exported by ioc. | ||
@inject('instantiationService') readonly ioc: IOC, // ioc itself is also a service can be injected. | ||
) | ||
) {} | ||
@@ -97,0 +96,0 @@ someMethod() { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
24046
1
15
258
93