Socket
Socket
Sign inDemoInstall

injection

Package Overview
Dependencies
20
Maintainers
2
Versions
73
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.7.7 to 1.8.0

17

CHANGELOG.md
1.8.0 / 2019-12-16
==================
* 解决循环引用问题以及提升测试覆盖率 (#33)
* remove 8
* add 12 and remove 9
* cnpm -> npm
* fix test case
* add co dep
* modified
* modified test case
* add test case
* refactor singleton init mode
* add test case
* add circular dependency support
* 循环依赖性处理
1.7.7 / 2019-10-08

@@ -3,0 +20,0 @@ ==================

2

dist/factory/applicationContext.d.ts

@@ -14,4 +14,6 @@ /**

export declare class ObjectDefinitionRegistry extends Map implements IObjectDefinitionRegistry {
private singletonIds;
readonly identifiers: any[];
readonly count: number;
getSingletonDefinitionIds(): ObjectIdentifier[];
getDefinitionByName(name: string): IObjectDefinition[];

@@ -18,0 +20,0 @@ registerDefinition(identifier: ObjectIdentifier, definition: IObjectDefinition): void;

@@ -16,2 +16,6 @@ "use strict";

class ObjectDefinitionRegistry extends Map {
constructor() {
super(...arguments);
this.singletonIds = [];
}
get identifiers() {

@@ -29,2 +33,5 @@ const ids = [];

}
getSingletonDefinitionIds() {
return this.singletonIds;
}
getDefinitionByName(name) {

@@ -41,2 +48,5 @@ const definitions = [];

registerDefinition(identifier, definition) {
if (definition.isSingletonScope()) {
this.singletonIds.push(identifier);
}
this.set(identifier, definition);

@@ -134,3 +144,7 @@ }

}
return this.refreshAsync();
await this.refreshAsync();
const ids = this.registry.getSingletonDefinitionIds();
for (const id of ids) {
await this.getAsync(id);
}
}

@@ -137,0 +151,0 @@ async refreshAsync() {

20

dist/factory/common/managedResolverFactory.d.ts

@@ -1,5 +0,1 @@

/**
* 管理对象解析构建
*/
import { EventEmitter } from 'events';
import { IApplicationContext, IManagedInstance, IManagedResolver, IObjectDefinition } from '../../interfaces';

@@ -19,3 +15,3 @@ /**

*/
export declare class ManagedResolverFactory extends EventEmitter {
export declare class ManagedResolverFactory {
private resolvers;

@@ -55,7 +51,2 @@ private _props;

/**
* 判断是否需要等待单例异步初始化
* @param definition 单例定义
*/
private compareAndSetCreating;
/**
* 触发单例初始化结束事件

@@ -65,3 +56,10 @@ * @param definition 单例定义

*/
private removeCreating;
private removeCreateStatus;
private isCreating;
private compareAndSetCreateStatus;
/**
* 创建对象定义的代理访问逻辑
* @param definition 对象定义
*/
private createProxyReference;
}

@@ -6,3 +6,2 @@ "use strict";

*/
const events_1 = require("events");
const _ = require("../../utils/lodashWrap");

@@ -14,4 +13,2 @@ const constants_1 = require("./constants");

const errorFactory_1 = require("../../utils/errorFactory");
const awaitFirst = require('await-first');
const SINGLETON_CREATED = '_single_created';
/**

@@ -247,5 +244,4 @@ * 所有解析器基类

*/
class ManagedResolverFactory extends events_1.EventEmitter {
class ManagedResolverFactory {
constructor(context) {
super();
this.resolvers = {};

@@ -318,2 +314,8 @@ this._props = null;

}
// 如果非 null 表示已经创建 proxy
let inst = this.createProxyReference(definition);
if (inst) {
return inst;
}
this.compareAndSetCreateStatus(definition);
// 预先初始化依赖

@@ -340,3 +342,3 @@ if (definition.hasDependsOn()) {

}
const inst = definition.creator.doConstruct(Clzz, constructorArgs);
inst = definition.creator.doConstruct(Clzz, constructorArgs);
// binding ctx object

@@ -362,2 +364,3 @@ if (definition.isRequestScope() && definition.constructor.name === 'ObjectDefinition') {

}
this.removeCreateStatus(definition, true);
throw error;

@@ -383,2 +386,3 @@ }

}
this.removeCreateStatus(definition, true);
return inst;

@@ -396,7 +400,8 @@ }

}
let inst = await this.compareAndSetCreating(definition);
// 如果非 null 表示已经创建成功
// 如果非 null 表示已经创建 proxy
let inst = this.createProxyReference(definition);
if (inst) {
return inst;
}
this.compareAndSetCreateStatus(definition);
// 预先初始化依赖

@@ -426,3 +431,3 @@ if (definition.hasDependsOn()) {

if (!inst) {
this.removeCreating(definition, false);
this.removeCreateStatus(definition, false);
throw new Error(`${definition.id} config no valid path`);

@@ -450,3 +455,3 @@ }

}
this.removeCreating(definition, false);
this.removeCreateStatus(definition, false);
throw error;

@@ -467,3 +472,2 @@ }

this.singletonCache.set(definition.id, inst);
this.removeCreating(definition, true);
}

@@ -474,2 +478,3 @@ // for request scope

}
this.removeCreateStatus(definition, true);
return inst;

@@ -494,30 +499,51 @@ }

/**
* 判断是否需要等待单例异步初始化
* 触发单例初始化结束事件
* @param definition 单例定义
* @param success 成功 or 失败
*/
async compareAndSetCreating(definition) {
if (definition.isSingletonScope() && definition.id) {
if (this.creating.has(definition.id)) {
const e = await awaitFirst(this, `${definition.id}${SINGLETON_CREATED}`);
// 初始化成功
if (e.args[0]) {
return this.singletonCache.get(definition.id);
}
return null;
}
removeCreateStatus(definition, success) {
// 如果map中存在表示需要设置状态
if (this.creating.has(definition.id)) {
this.creating.set(definition.id, false);
}
return true;
}
isCreating(definition) {
return this.creating.has(definition.id) && this.creating.get(definition.id);
}
compareAndSetCreateStatus(definition) {
if (!this.creating.has(definition.id) || !this.creating.get(definition.id)) {
this.creating.set(definition.id, true);
}
return null;
}
/**
* 触发单例初始化结束事件
* @param definition 单例定义
* @param success 成功 or 失败
* 创建对象定义的代理访问逻辑
* @param definition 对象定义
*/
removeCreating(definition, success) {
if (definition.isSingletonScope() && this.creating.has(definition.id)) {
this.creating.set(definition.id, false);
this.emit(`${definition.id}${SINGLETON_CREATED}`, success);
createProxyReference(definition) {
if (this.isCreating(definition)) {
// 创建代理对象
return new Proxy({}, {
get: (obj, prop) => {
let target;
if (definition.isRequestScope()) {
target = this.context.registry.getObject(definition.id);
}
else if (definition.isSingletonScope()) {
target = this.singletonCache.get(definition.id);
}
else {
target = this.context.get(definition.id);
}
if (target) {
if (typeof target[prop] === 'function') {
return target[prop].bind(target);
}
return target[prop];
}
return undefined;
}
});
}
return true;
return null;
}

@@ -524,0 +550,0 @@ }

@@ -73,2 +73,3 @@ export declare type ObjectIdentifier = string;

registerDefinition(identifier: ObjectIdentifier, definition: IObjectDefinition): any;
getSingletonDefinitionIds(): ObjectIdentifier[];
getDefinition(identifier: ObjectIdentifier): IObjectDefinition;

@@ -75,0 +76,0 @@ getDefinitionByPath(path: string): IObjectDefinition;

{
"name": "injection",
"version": "1.7.7",
"version": "1.8.0",
"description": "A New IoC Container For All Node.js Application",

@@ -53,3 +53,2 @@ "main": "dist/index",

"dependencies": {
"await-first": "^1.0.0",
"camelcase": "^5.0.0",

@@ -56,0 +55,0 @@ "co": "^4.6.0",

@@ -31,2 +31,4 @@ /**

export class ObjectDefinitionRegistry extends Map implements IObjectDefinitionRegistry {
private singletonIds = [];
get identifiers() {

@@ -46,2 +48,6 @@ const ids = [];

getSingletonDefinitionIds(): ObjectIdentifier[] {
return this.singletonIds;
}
getDefinitionByName(name: string): IObjectDefinition[] {

@@ -59,2 +65,5 @@ const definitions = [];

registerDefinition(identifier: ObjectIdentifier, definition: IObjectDefinition) {
if (definition.isSingletonScope()) {
this.singletonIds.push(identifier);
}
this.set(identifier, definition);

@@ -169,3 +178,7 @@ }

}
return this.refreshAsync();
await this.refreshAsync();
const ids = this.registry.getSingletonDefinitionIds();
for (const id of ids) {
await this.getAsync(id);
}
}

@@ -172,0 +185,0 @@

/**
* 管理对象解析构建
*/
import { EventEmitter } from 'events';
import * as _ from '../../utils/lodashWrap';

@@ -31,4 +30,2 @@ import { KEYS, VALUE_TYPE } from './constants';

const awaitFirst = require('await-first');
const SINGLETON_CREATED = '_single_created';
/**

@@ -296,3 +293,3 @@ * 所有解析器基类

*/
export class ManagedResolverFactory extends EventEmitter {
export class ManagedResolverFactory {
private resolvers = {};

@@ -307,3 +304,2 @@ private _props = null;

constructor(context: IApplicationContext) {
super();

@@ -378,3 +374,9 @@ this.context = context;

}
// 如果非 null 表示已经创建 proxy
let inst = this.createProxyReference(definition);
if (inst) {
return inst;
}
this.compareAndSetCreateStatus(definition);
// 预先初始化依赖

@@ -404,3 +406,3 @@ if (definition.hasDependsOn()) {

const inst = definition.creator.doConstruct(Clzz, constructorArgs);
inst = definition.creator.doConstruct(Clzz, constructorArgs);

@@ -427,2 +429,3 @@ // binding ctx object

}
this.removeCreateStatus(definition, true);
throw error;

@@ -453,2 +456,3 @@ }

}
this.removeCreateStatus(definition, true);

@@ -469,4 +473,4 @@ return inst;

let inst = await this.compareAndSetCreating(definition);
// 如果非 null 表示已经创建成功
// 如果非 null 表示已经创建 proxy
let inst = this.createProxyReference(definition);
if (inst) {

@@ -476,2 +480,3 @@ return inst;

this.compareAndSetCreateStatus(definition);
// 预先初始化依赖

@@ -503,3 +508,3 @@ if (definition.hasDependsOn()) {

if (!inst) {
this.removeCreating(definition, false);
this.removeCreateStatus(definition, false);
throw new Error(`${definition.id} config no valid path`);

@@ -528,3 +533,3 @@ }

}
this.removeCreating(definition, false);
this.removeCreateStatus(definition, false);
throw error;

@@ -549,3 +554,2 @@ }

this.singletonCache.set(definition.id, inst);
this.removeCreating(definition, true);
}

@@ -557,2 +561,3 @@

}
this.removeCreateStatus(definition, true);

@@ -581,31 +586,54 @@ return inst;

/**
* 判断是否需要等待单例异步初始化
* 触发单例初始化结束事件
* @param definition 单例定义
* @param success 成功 or 失败
*/
private async compareAndSetCreating(definition: IObjectDefinition): Promise<any> {
if (definition.isSingletonScope() && definition.id) {
if (this.creating.has(definition.id)) {
const e = await awaitFirst(this, `${definition.id}${SINGLETON_CREATED}`);
// 初始化成功
if (e.args[0]) {
return this.singletonCache.get(definition.id);
}
return null;
}
private removeCreateStatus(definition: IObjectDefinition, success: boolean): boolean {
// 如果map中存在表示需要设置状态
if (this.creating.has(definition.id)) {
this.creating.set(definition.id, false);
}
return true;
}
private isCreating(definition: IObjectDefinition) {
return this.creating.has(definition.id) && this.creating.get(definition.id);
}
private compareAndSetCreateStatus(definition: IObjectDefinition) {
if (!this.creating.has(definition.id) || !this.creating.get(definition.id)) {
this.creating.set(definition.id, true);
}
return null;
}
/**
* 触发单例初始化结束事件
* @param definition 单例定义
* @param success 成功 or 失败
* 创建对象定义的代理访问逻辑
* @param definition 对象定义
*/
private removeCreating(definition: IObjectDefinition, success: boolean): boolean {
if (definition.isSingletonScope() && this.creating.has(definition.id)) {
this.creating.set(definition.id, false);
this.emit(`${definition.id}${SINGLETON_CREATED}`, success);
private createProxyReference(definition: IObjectDefinition): any {
if (this.isCreating(definition)) {
// 创建代理对象
return new Proxy({}, {
get: (obj, prop) => {
let target;
if (definition.isRequestScope()) {
target = this.context.registry.getObject(definition.id);
} else if (definition.isSingletonScope()) {
target = this.singletonCache.get(definition.id);
} else {
target = this.context.get(definition.id);
}
if (target) {
if (typeof target[prop] === 'function') {
return target[prop].bind(target);
}
return target[prop];
}
return undefined;
}
});
}
return true;
return null;
}
}

@@ -76,2 +76,3 @@ export type ObjectIdentifier = string;

registerDefinition(identifier: ObjectIdentifier, definition: IObjectDefinition);
getSingletonDefinitionIds(): ObjectIdentifier[];
getDefinition(identifier: ObjectIdentifier): IObjectDefinition;

@@ -78,0 +79,0 @@ getDefinitionByPath(path: string): IObjectDefinition;

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc