UNPKG

9.1 kBTypeScriptView Raw
1declare module '@ember/-internals/container/lib/registry' {
2 import type {
3 Factory,
4 FactoryClass,
5 FullName,
6 InternalFactory,
7 KnownForTypeResult,
8 RegisterOptions,
9 Resolver,
10 } from '@ember/-internals/owner';
11 import type { set } from '@ember/object';
12 import type { ContainerOptions, LazyInjection } from '@ember/-internals/container/lib/container';
13 import Container from '@ember/-internals/container/lib/container';
14 export interface Injection {
15 property: string;
16 specifier: FullName;
17 }
18 export interface ResolverClass
19 extends Factory<Resolver>,
20 Partial<{
21 new (...args: any): Resolver;
22 }> {}
23 export interface RegistryOptions {
24 fallback?: Registry;
25 registrations?: {
26 [key: string]: object;
27 };
28 resolver?: Resolver;
29 }
30 /**
31 A registry used to store factory and option information keyed
32 by type.
33
34 A `Registry` stores the factory and option information needed by a
35 `Container` to instantiate and cache objects.
36
37 The API for `Registry` is still in flux and should not be considered stable.
38
39 @private
40 @class Registry
41 @since 1.11.0
42 */
43 export default class Registry {
44 readonly _failSet: Set<string>;
45 resolver: Resolver | null;
46 readonly fallback: Registry | null;
47 readonly registrations: Record<string, InternalFactory<object> | object>;
48 readonly _normalizeCache: Record<FullName, FullName>;
49 readonly _options: Record<string, RegisterOptions>;
50 readonly _resolveCache: Record<string, InternalFactory<object> | object>;
51 readonly _typeOptions: Record<string, RegisterOptions>;
52 set?: typeof set;
53 constructor(options?: RegistryOptions);
54 /**
55 A backup registry for resolving registrations when no matches can be found.
56
57 @private
58 @property fallback
59 @type Registry
60 */
61 /**
62 An object that has a `resolve` method that resolves a name.
63
64 @private
65 @property resolver
66 @type Resolver
67 */
68 /**
69 @private
70 @property registrations
71 @type InheritingDict
72 */
73 /**
74 @private
75
76 @property _normalizeCache
77 @type InheritingDict
78 */
79 /**
80 @private
81
82 @property _resolveCache
83 @type InheritingDict
84 */
85 /**
86 @private
87
88 @property _options
89 @type InheritingDict
90 */
91 /**
92 @private
93
94 @property _typeOptions
95 @type InheritingDict
96 */
97 /**
98 Creates a container based on this registry.
99
100 @private
101 @method container
102 @param {Object} options
103 @return {Container} created container
104 */
105 container(options?: ContainerOptions): Container;
106 /**
107 Registers a factory for later injection.
108
109 Example:
110
111 ```javascript
112 let registry = new Registry();
113
114 registry.register('model:user', Person, {singleton: false });
115 registry.register('fruit:favorite', Orange);
116 registry.register('communication:main', Email, {singleton: false});
117 ```
118
119 @private
120 @method register
121 @param {String} fullName
122 @param {Function} factory
123 @param {Object} options
124 */
125 register(
126 fullName: FullName,
127 factory: object,
128 options: RegisterOptions & {
129 instantiate: false;
130 }
131 ): void;
132 register<T extends object, C extends FactoryClass | object>(
133 fullName: FullName,
134 factory: InternalFactory<T, C>,
135 options?: RegisterOptions
136 ): void;
137 /**
138 Unregister a fullName
139
140 ```javascript
141 let registry = new Registry();
142 registry.register('model:user', User);
143
144 registry.resolve('model:user').create() instanceof User //=> true
145
146 registry.unregister('model:user')
147 registry.resolve('model:user') === undefined //=> true
148 ```
149
150 @private
151 @method unregister
152 @param {String} fullName
153 */
154 unregister(fullName: FullName): void;
155 /**
156 Given a fullName return the corresponding factory.
157
158 By default `resolve` will retrieve the factory from
159 the registry.
160
161 ```javascript
162 let registry = new Registry();
163 registry.register('api:twitter', Twitter);
164
165 registry.resolve('api:twitter') // => Twitter
166 ```
167
168 Optionally the registry can be provided with a custom resolver.
169 If provided, `resolve` will first provide the custom resolver
170 the opportunity to resolve the fullName, otherwise it will fallback
171 to the registry.
172
173 ```javascript
174 let registry = new Registry();
175 registry.resolver = function(fullName) {
176 // lookup via the module system of choice
177 };
178
179 // the twitter factory is added to the module system
180 registry.resolve('api:twitter') // => Twitter
181 ```
182
183 @private
184 @method resolve
185 @param {String} fullName
186 @return {Function} fullName's factory
187 */
188 resolve(fullName: FullName): InternalFactory<object> | object | undefined;
189 /**
190 A hook that can be used to describe how the resolver will
191 attempt to find the factory.
192
193 For example, the default Ember `.describe` returns the full
194 class name (including namespace) where Ember's resolver expects
195 to find the `fullName`.
196
197 @private
198 @method describe
199 @param {String} fullName
200 @return {string} described fullName
201 */
202 describe(fullName: FullName): string;
203 /**
204 A hook to enable custom fullName normalization behavior
205
206 @private
207 @method normalizeFullName
208 @param {String} fullName
209 @return {string} normalized fullName
210 */
211 normalizeFullName(fullName: FullName): FullName;
212 /**
213 Normalize a fullName based on the application's conventions
214
215 @private
216 @method normalize
217 @param {String} fullName
218 @return {string} normalized fullName
219 */
220 normalize(fullName: FullName): FullName;
221 /**
222 @method makeToString
223
224 @private
225 @param {any} factory
226 @param {string} fullName
227 @return {function} toString function
228 */
229 makeToString(factory: InternalFactory<object>, fullName: FullName): string;
230 /**
231 Given a fullName check if the container is aware of its factory
232 or singleton instance.
233
234 @private
235 @method has
236 @param {String} fullName
237 @param {Object} [options]
238 @param {String} [options.source] the fullname of the request source (used for local lookups)
239 @return {Boolean}
240 */
241 has(fullName: FullName): boolean;
242 /**
243 Allow registering options for all factories of a type.
244
245 ```javascript
246 let registry = new Registry();
247 let container = registry.container();
248
249 // if all of type `connection` must not be singletons
250 registry.optionsForType('connection', { singleton: false });
251
252 registry.register('connection:twitter', TwitterConnection);
253 registry.register('connection:facebook', FacebookConnection);
254
255 let twitter = container.lookup('connection:twitter');
256 let twitter2 = container.lookup('connection:twitter');
257
258 twitter === twitter2; // => false
259
260 let facebook = container.lookup('connection:facebook');
261 let facebook2 = container.lookup('connection:facebook');
262
263 facebook === facebook2; // => false
264 ```
265
266 @private
267 @method optionsForType
268 @param {String} type
269 @param {Object} options
270 */
271 optionsForType(type: string, options: RegisterOptions): void;
272 getOptionsForType(type: string): RegisterOptions | undefined;
273 /**
274 @private
275 @method options
276 @param {String} fullName
277 @param {Object} options
278 */
279 options(fullName: FullName, options: RegisterOptions): void;
280 getOptions(fullName: FullName): RegisterOptions | undefined;
281 getOption<K extends keyof RegisterOptions>(
282 fullName: FullName,
283 optionName: K
284 ): RegisterOptions[K] | undefined;
285 /**
286 @private
287 @method knownForType
288 @param {String} type the type to iterate over
289 */
290 knownForType<T extends string>(type: T): KnownForTypeResult<T>;
291 isValidFullName(fullName: string): fullName is FullName;
292 }
293 export class DebugRegistry extends Registry {
294 normalizeInjectionsHash(hash: { [key: string]: LazyInjection }): Injection[];
295 validateInjections(injections: Injection[]): void;
296 }
297 export function privatize([fullName]: TemplateStringsArray): FullName;
298}