UNPKG

3.94 kBPlain TextView Raw
1import { Dict } from './dict';
2import { Factory, FactoryDefinition } from './factory';
3
4export interface RegistrationOptions {
5 singleton?: boolean;
6 instantiate?: boolean;
7}
8
9export interface Injection {
10 property: string,
11 source: string
12}
13
14export interface RegistryWriter {
15 register(specifier: string, factory: any, options?: RegistrationOptions): void;
16 unregister(specifier: string): void;
17 registerOption(specifier: string, option: string, value: any): void;
18 unregisterOption(specifier: string, option: string): void;
19 registerInjection(specifier: string, property: string, source: string): void;
20}
21
22export interface RegistryReader {
23 registration(specifier: string): any;
24 registeredOption(specifier: string, option: string): any;
25 registeredOptions(specifier: string): any;
26 registeredInjections(specifier: string): Injection[];
27}
28
29export interface RegistryOptions {
30 fallback?: RegistryReader;
31}
32
33export interface RegistryAccessor extends RegistryReader, RegistryWriter {}
34
35export default class Registry implements RegistryAccessor {
36 private _registrations: Dict<FactoryDefinition<any>>;
37 private _registeredOptions: Dict<any>;
38 private _registeredInjections: Dict<Injection[]>;
39 private _fallback: RegistryReader;
40
41 constructor(options?: RegistryOptions) {
42 this._registrations = {};
43 this._registeredOptions = {};
44 this._registeredInjections = {};
45 if (options && options.fallback) {
46 this._fallback = options.fallback;
47 }
48 }
49
50 register(specifier: string, factoryDefinition: FactoryDefinition<any>, options?: RegistrationOptions): void {
51 this._registrations[specifier] = factoryDefinition;
52 if (options) {
53 this._registeredOptions[specifier] = options;
54 }
55 }
56
57 registration(specifier: string): FactoryDefinition<any> {
58 let registration = this._registrations[specifier];
59 if (registration === undefined && this._fallback) {
60 registration = this._fallback.registration(specifier);
61 }
62 return registration;
63 }
64
65 unregister(specifier: string): void {
66 delete this._registrations[specifier];
67 delete this._registeredOptions[specifier];
68 delete this._registeredInjections[specifier];
69 }
70
71 registerOption(specifier: string, option: string, value: any): void {
72 let options = this._registeredOptions[specifier];
73
74 if (!options) {
75 options = {};
76 this._registeredOptions[specifier] = options;
77 }
78
79 options[option] = value;
80 }
81
82 registeredOption(specifier: string, option: string): any {
83 let result: Boolean;
84 let options = this.registeredOptions(specifier);
85
86 if (options) {
87 result = options[option];
88 }
89
90 if (result === undefined && this._fallback !== undefined) {
91 result = this._fallback.registeredOption(specifier, option);
92 }
93
94 return result;
95 }
96
97 registeredOptions(specifier: string): any {
98 let options = this._registeredOptions[specifier];
99 if (options === undefined) {
100 let type = specifier.split(':')[0];
101 options = this._registeredOptions[type];
102 }
103 return options;
104 }
105
106 unregisterOption(specifier: string, option: string): void {
107 let options = this._registeredOptions[specifier];
108
109 if (options) {
110 delete options[option];
111 }
112 }
113
114 registerInjection(specifier: string, property: string, source: string): void {
115 let injections = this._registeredInjections[specifier];
116 if (injections === undefined) {
117 this._registeredInjections[specifier] = injections = [];
118 }
119 injections.push({
120 property,
121 source
122 });
123 }
124
125 registeredInjections(specifier: string): Injection[] {
126 let type = specifier.split(':')[0];
127 let injections: Injection[] = this._fallback ? this._fallback.registeredInjections(specifier) : [];
128 Array.prototype.push.apply(injections, this._registeredInjections[type]);
129 Array.prototype.push.apply(injections, this._registeredInjections[specifier]);
130 return injections;
131 }
132}