ts-ioc-container
Version:
Typescript IoC container
39 lines (38 loc) • 1.22 kB
JavaScript
import { Registration } from './registration/Registration';
import { Provider } from './provider/Provider';
export const isDepKey = (key) => {
return typeof key === 'object' && key !== null && 'key' in key;
};
export const depKey = (key) => {
const scopePredicates = [];
const mappers = [];
return {
key,
asKey: (registration) => {
let reg = registration.pipe(...mappers).bindToKey(key);
if (scopePredicates.length > 0) {
reg = registration.when(...scopePredicates);
}
return reg;
},
register: (fn) => {
let registration = new Registration(() => new Provider(fn), key).pipe(...mappers);
if (scopePredicates.length > 0) {
registration = registration.when(...scopePredicates);
}
return registration;
},
resolve: (s) => s.resolveOne(key),
pipe(...values) {
mappers.push(...values);
return this;
},
when(...predicates) {
scopePredicates.push(...predicates);
return this;
},
asAlias(r) {
return r.bindToAlias(key);
},
};
};