@dits/dits
Advanced tools
Comparing version 0.1.5 to 0.1.6
@@ -19,2 +19,5 @@ "use strict"; | ||
return (constructor) => { | ||
if (!scope) { | ||
throw new Error(`Failed to invoke ${constructor.name} – component scope must be defined`); | ||
} | ||
// could look this up via metadata override (from some new decorator) | ||
@@ -21,0 +24,0 @@ const name = constructor.name; |
@@ -1,18 +0,19 @@ | ||
import { ComponentDeclaration } from "./components"; | ||
import { ComponentRegistry, ComponentDeclaration } from "./components"; | ||
import { HandlerRegistry } from "../dispatch/handlers"; | ||
import { DispatchEvent, EventConstructor, HandlerDeclaration } from "../dispatch/dispatch"; | ||
export default class Container { | ||
name: string; | ||
parent?: Container | undefined; | ||
static ZONE_PROPERTY: string; | ||
private components; | ||
components: ComponentRegistry; | ||
handlers: HandlerRegistry; | ||
private singletons; | ||
constructor(parent?: Container | undefined); | ||
constructor(name: string, parent?: Container | undefined); | ||
static fromZone(): Container; | ||
get<T>(key: { | ||
new (...args: any[]): unknown; | ||
}): T | undefined; | ||
getOrThrow<T>(key: { | ||
get<T, C extends { | ||
new (...args: any[]): T; | ||
}, errMessage?: string): T; | ||
}>(key: C): T | undefined; | ||
getOrThrow<T, C extends { | ||
new (...args: any[]): T; | ||
}>(key: C, errMessage?: string): T; | ||
provide<T>(key: { | ||
@@ -27,5 +28,6 @@ new (...args: any[]): unknown; | ||
initialize(scope: string, ...scopes: string[]): Promise<void>; | ||
private getAncestry; | ||
unwrap(): Map<new (...args: any[]) => unknown, unknown>; | ||
createChild(): Container; | ||
createChild(name: string): Container; | ||
} | ||
//# sourceMappingURL=container.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// import { CONTAINER_PROPERTY } from '../zones/zones' | ||
const tslog_1 = require("tslog"); | ||
const components_1 = require("./components"); | ||
const handlers_1 = require("../dispatch/handlers"); | ||
const log = new tslog_1.Logger({ name: __filename }); | ||
// /** | ||
@@ -11,2 +13,3 @@ // * Keeps track of classes and their instances | ||
class Container { | ||
name; | ||
parent; | ||
@@ -17,3 +20,4 @@ static ZONE_PROPERTY = '_ditsContainer'; | ||
singletons = new Map(); | ||
constructor(parent) { | ||
constructor(name, parent) { | ||
this.name = name; | ||
this.parent = parent; | ||
@@ -67,25 +71,30 @@ } | ||
for (const s of [scope, ...scopes]) { | ||
const graph = await this.components.populate(s, this); | ||
for (const [key, value] of graph.entries()) { | ||
// const wrap = SmartProxy.getSmartProxy(value) | ||
// const sym = Symbol.for('handlers') | ||
// const handlers: HandlerDeclaration<any>[] = Metadata.retrieveMetadata(key, sym) | ||
// if (handlers) { | ||
// console.log(handlers) | ||
// } | ||
const handlers = Reflect.getMetadata(handlers_1.HANDLER_KEY, key); | ||
if (handlers) { | ||
handlers.forEach(h => { | ||
this.handlers.register(h.type, h); | ||
}); | ||
for (const container of this.getAncestry()) { | ||
const graph = await container.components.populate(s, this); | ||
for (const [key, value] of graph.entries()) { | ||
const handlers = Reflect.getMetadata(handlers_1.HANDLER_KEY, key); | ||
if (handlers) { | ||
handlers.forEach(h => { | ||
this.handlers.register(h.type, h); | ||
}); | ||
} | ||
this.provide(key, value); | ||
} | ||
this.provide(key, value); | ||
} | ||
} | ||
} | ||
getAncestry(asc = true) { | ||
const ancestry = []; | ||
let target = this; | ||
while (target) { | ||
asc ? ancestry.unshift(target) : ancestry.push(target); | ||
target = target.parent; | ||
} | ||
return ancestry; | ||
} | ||
unwrap() { | ||
return this.singletons; | ||
} | ||
createChild() { | ||
return new Container(this); | ||
createChild(name) { | ||
return new Container(name, this); | ||
} | ||
@@ -92,0 +101,0 @@ } |
@@ -19,3 +19,3 @@ import Container from './container'; | ||
zone?: Zone; | ||
static fromZone(): any; | ||
static fromZone(): Service; | ||
constructor(config: dits.config.Configuration, container?: Container); | ||
@@ -25,4 +25,4 @@ get principal(): dits.security.Principal; | ||
initialize(config: ServiceConfig): Promise<any>; | ||
fork(name: string, properties?: any): Promise<Zone>; | ||
fork(name: string, properties?: any): Zone; | ||
} | ||
//# sourceMappingURL=service.d.ts.map |
@@ -23,3 +23,4 @@ "use strict"; | ||
static fromZone() { | ||
return Zone.current.get(Service.ZONE_KEY); | ||
const container = container_1.default.fromZone(); | ||
return container.getOrThrow(Service, 'Could not locate Service from Zone\'s container.'); | ||
} | ||
@@ -29,2 +30,3 @@ constructor(config, container = zones_1.root) { | ||
this.container = container; | ||
container.provide(Service, this, true); | ||
} | ||
@@ -39,13 +41,15 @@ get principal() { | ||
handler = handler || configOrHandler; | ||
this.zone = Zone.current.fork({ | ||
name: config?.zone?.name || 'app', | ||
properties: { | ||
...(config?.zone?.properties || {}), | ||
[Service.ZONE_KEY]: this | ||
} | ||
}); | ||
// this.zone = Zone.current.fork({ | ||
// name: config?.zone?.name || 'app', | ||
// properties: { | ||
// ...(config?.zone?.properties || {}), | ||
// [Container.ZONE_PROPERTY]: this.container, | ||
// [Service.ZONE_KEY]: this | ||
// } | ||
// }) | ||
this.zone = Zone.root; | ||
return this.zone.run(handler, this, [new InitContext(this)]); | ||
} | ||
async fork(name, properties) { | ||
const container = this.container.createChild(); | ||
fork(name, properties) { | ||
const container = this.container.createChild(name); | ||
return this.zone.fork({ | ||
@@ -52,0 +56,0 @@ name, |
@@ -32,3 +32,3 @@ "use strict"; | ||
const dependencies = paramTypes; | ||
const metadata = Metadata_1.default.retrieveMetadata(target, propertyKey); | ||
const metadata = Metadata_1.default.retrieveMetadata(target, propertyKey) || {}; | ||
// allow customization of what type of event is being registered for the handler | ||
@@ -53,3 +53,3 @@ const registerType = Reflect.getMetadata(exports.REGISTER_AS_META_KEY, target, propertyKey) || eventType; | ||
// container.handler(registerType, decl) | ||
const handlers = Reflect.getMetadata(exports.HANDLER_KEY, target) || []; | ||
const handlers = Reflect.getMetadata(exports.HANDLER_KEY, target.constructor) || []; | ||
handlers.push(decl); | ||
@@ -56,0 +56,0 @@ Reflect.defineMetadata(exports.HANDLER_KEY, handlers, target.constructor); |
import ZoneHook from './zones/zones'; | ||
import Container from './di/container'; | ||
import Service from './di/service'; | ||
export { Service, Container, ZoneHook }; | ||
import SmartProxy from './di/proxy'; | ||
export { Service, Container, ZoneHook, SmartProxy }; | ||
export { Component, ComponentRegistry } from './di/components'; | ||
@@ -6,0 +7,0 @@ export { DispatchEvent, DispatchPredicate, HandlerDeclaration } from './dispatch/dispatch'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Logger = exports.ANONYMOUS = exports.Authenticator = exports.SecurityContext = exports.HandlerRegistry = exports.Handler = exports.DispatchEvent = exports.ComponentRegistry = exports.Component = exports.ZoneHook = exports.Container = exports.Service = void 0; | ||
exports.Logger = exports.ANONYMOUS = exports.Authenticator = exports.SecurityContext = exports.HandlerRegistry = exports.Handler = exports.DispatchEvent = exports.ComponentRegistry = exports.Component = exports.SmartProxy = exports.ZoneHook = exports.Container = exports.Service = void 0; | ||
const tslib_1 = require("tslib"); | ||
@@ -12,2 +12,4 @@ const zones_1 = (0, tslib_1.__importDefault)(require("./zones/zones")); | ||
exports.Service = service_1.default; | ||
const proxy_1 = (0, tslib_1.__importDefault)(require("./di/proxy")); | ||
exports.SmartProxy = proxy_1.default; | ||
var components_1 = require("./di/components"); | ||
@@ -14,0 +16,0 @@ Object.defineProperty(exports, "Component", { enumerable: true, get: function () { return components_1.Component; } }); |
@@ -10,2 +10,3 @@ interface DoneCallback { | ||
export default class DitsTestHarness { | ||
private idx; | ||
init(): void; | ||
@@ -12,0 +13,0 @@ shutdown(): void; |
@@ -8,2 +8,3 @@ "use strict"; | ||
class DitsTestHarness { | ||
idx = 0; | ||
init() { | ||
@@ -20,3 +21,3 @@ zones_1.default.enable(); | ||
const parent = container_1.default.fromZone(); | ||
const child = parent.createChild(); | ||
const child = parent.createChild(`harness-${this.idx++}`); | ||
const testZone = Zone.current.fork({ | ||
@@ -23,0 +24,0 @@ name: 'test-' + (0, uuid_1.v4)(), |
@@ -5,4 +5,4 @@ "use strict"; | ||
const tslog_1 = require("tslog"); | ||
const Logger = (fileName) => new tslog_1.Logger({ name: __filename }); | ||
const Logger = (fileName) => new tslog_1.Logger({ name: fileName }); | ||
exports.Logger = Logger; | ||
//# sourceMappingURL=logging.js.map |
{ | ||
"name": "@dits/dits", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"main": "lib/index.js", | ||
@@ -5,0 +5,0 @@ "types": "lib/index.d.ts", |
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
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
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
107279
1522