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.34 kB
JavaScript
import { InjectionToken } from './InjectionToken';
export class GroupAliasToken extends InjectionToken {
token;
_getArgsFn;
_isLazy;
constructor(token, { getArgsFn = () => [], isLazy = false } = {}) {
super();
this.token = token;
this._getArgsFn = getArgsFn;
this._isLazy = isLazy;
}
select(fn) {
return (s) => fn(this.resolve(s));
}
resolve(s, { args = [], lazy } = {}) {
return s.resolveByAlias(this.token, {
args: this._getArgsFn(s, { args }),
lazy: this._isLazy || lazy,
});
}
bindTo(r) {
r.bindToAlias(this.token);
}
args(...newArgs) {
const parentFn = this._getArgsFn;
return new GroupAliasToken(this.token, {
getArgsFn: (s, opts) => [...parentFn(s, opts), ...newArgs],
isLazy: this._isLazy,
});
}
argsFn(fn) {
const parentFn = this._getArgsFn;
return new GroupAliasToken(this.token, {
getArgsFn: (s, opts) => [...parentFn(s, opts), ...fn(s)],
isLazy: this._isLazy,
});
}
lazy() {
return new GroupAliasToken(this.token, {
getArgsFn: this._getArgsFn,
isLazy: true,
});
}
}
export const toGroupAlias = (token) => new GroupAliasToken(token);