UNPKG

ts-ioc-container

Version:

Fast, lightweight TypeScript dependency injection container with a clean API, scoped lifecycles, decorators, tokens, hooks, lazy injection, customizable providers, and no global container objects.

46 lines (45 loc) 1.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ClassToken = void 0; const InjectionToken_1 = require("./InjectionToken"); class ClassToken extends InjectionToken_1.InjectionToken { target; _getArgsFn; _isLazy; constructor(target, { getArgsFn = () => [], isLazy = false } = {}) { super(); this.target = target; this._getArgsFn = getArgsFn; this._isLazy = isLazy; } select(fn) { return (s) => fn(this.resolve(s)); } resolve(s, { args = [], lazy } = {}) { return s.resolve(this.target, { args: this._getArgsFn(s, { args }), lazy: this._isLazy || lazy, }); } args(...newArgs) { const parentFn = this._getArgsFn; return new ClassToken(this.target, { getArgsFn: (s, opts) => [...parentFn(s, opts), ...newArgs], isLazy: this._isLazy, }); } argsFn(fn) { const parentFn = this._getArgsFn; return new ClassToken(this.target, { getArgsFn: (s, opts) => [...parentFn(s, opts), ...fn(s)], isLazy: this._isLazy, }); } lazy() { return new ClassToken(this.target, { getArgsFn: this._getArgsFn, isLazy: true, }); } } exports.ClassToken = ClassToken;