UNPKG

@visactor/vrender-core

Version:

```typescript import { xxx } from '@visactor/vrender-core'; ```

91 lines (84 loc) 3.76 kB
class LegacyBindingSyntax { constructor(record) { this.record = record; } to(constructor) { return this.record.implementationType = "constructor", this.record.implementationConstructor = constructor, this.record.cached = !1, this.record.cachedValue = void 0, this; } toSelf() { return this.to(this.record.serviceIdentifier); } toDynamicValue(factory) { return this.record.implementationType = "dynamic", this.record.implementationFactory = factory, this.record.cached = !1, this.record.cachedValue = void 0, this; } toConstantValue(value) { return this.record.implementationType = "constant", this.record.implementationValue = value, this.record.cached = !0, this.record.cachedValue = value, this; } toService(serviceIdentifier) { return this.record.implementationType = "service", this.record.linkedServiceIdentifier = serviceIdentifier, this.record.cached = !1, this.record.cachedValue = void 0, this; } inSingletonScope() { return this.record.scope = "singleton", this; } whenTargetNamed(name) { return this.record.named = name, this; } } class LegacyBindingContext { constructor() { this.bindings = new Map, this.bind = serviceIdentifier => { const record = { serviceIdentifier: serviceIdentifier, scope: "transient", cached: !1 }, records = this.bindings.get(serviceIdentifier); return records ? records.push(record) : this.bindings.set(serviceIdentifier, [ record ]), new LegacyBindingSyntax(record); }, this.rebind = serviceIdentifier => (this.bindings.delete(serviceIdentifier), this.bind(serviceIdentifier)), this.isBound = serviceIdentifier => { var _a, _b; return (null !== (_b = null === (_a = this.bindings.get(serviceIdentifier)) || void 0 === _a ? void 0 : _a.length) && void 0 !== _b ? _b : 0) > 0; }, this.getAll = serviceIdentifier => { const records = this.bindings.get(serviceIdentifier); return (null == records ? void 0 : records.length) ? records.map((record => this.resolveBinding(record))).filter((value => void 0 !== value)) : []; }, this.getNamed = (serviceIdentifier, name) => { const records = this.bindings.get(serviceIdentifier); if (!(null == records ? void 0 : records.length)) return; const record = records.find((item => item.named === name)); return record ? this.resolveBinding(record) : void 0; }; } resolveBinding(record) { var _a; if ("singleton" === record.scope && record.cached) return record.cachedValue; let value; switch (record.implementationType) { case "constant": value = record.implementationValue; break; case "constructor": value = record.implementationConstructor ? new record.implementationConstructor : void 0; break; case "dynamic": value = null === (_a = record.implementationFactory) || void 0 === _a ? void 0 : _a.call(record, { container: this }); break; case "service": record.linkedServiceIdentifier && ([value] = this.getAll(record.linkedServiceIdentifier)); break; default: value = void 0; } return "singleton" === record.scope && (record.cached = !0, record.cachedValue = value), value; } } export function createLegacyBindingContext() { return new LegacyBindingContext; } //# sourceMappingURL=binding-context.js.map