UNPKG

6.09 kBTypeScriptView Raw
1declare module '@ember/-internals/container/lib/container' {
2 import type {
3 InternalFactory,
4 FactoryClass,
5 InternalOwner,
6 RegisterOptions,
7 FactoryManager,
8 FullName,
9 } from '@ember/-internals/owner';
10 import type { DebugRegistry } from '@ember/-internals/container/lib/registry';
11 import type Registry from '@ember/-internals/container/lib/registry';
12 interface LeakTracking {
13 hasContainers(): boolean;
14 reset(): void;
15 }
16 export interface ContainerOptions {
17 owner?: InternalOwner;
18 cache?: {
19 [key: string]: object;
20 };
21 factoryManagerCache?: {
22 [key: string]: InternalFactoryManager<any, any>;
23 };
24 validationCache?: {
25 [key: string]: boolean;
26 };
27 }
28 /**
29 A container used to instantiate and cache objects.
30
31 Every `Container` must be associated with a `Registry`, which is referenced
32 to determine the factory and options that should be used to instantiate
33 objects.
34
35 The public API for `Container` is still in flux and should not be considered
36 stable.
37
38 @private
39 @class Container
40 */
41 export default class Container {
42 static _leakTracking: LeakTracking;
43 readonly owner: InternalOwner | null;
44 readonly registry: Registry & DebugRegistry;
45 cache: {
46 [key: string]: object;
47 };
48 factoryManagerCache: {
49 [key: string]: InternalFactoryManager<object>;
50 };
51 readonly validationCache: {
52 [key: string]: boolean;
53 };
54 isDestroyed: boolean;
55 isDestroying: boolean;
56 constructor(registry: Registry, options?: ContainerOptions);
57 /**
58 @private
59 @property registry
60 @type Registry
61 @since 1.11.0
62 */
63 /**
64 @private
65 @property cache
66 @type InheritingDict
67 */
68 /**
69 @private
70 @property validationCache
71 @type InheritingDict
72 */
73 /**
74 Given a fullName return a corresponding instance.
75 The default behavior is for lookup to return a singleton instance.
76 The singleton is scoped to the container, allowing multiple containers
77 to all have their own locally scoped singletons.
78 ```javascript
79 let registry = new Registry();
80 let container = registry.container();
81 registry.register('api:twitter', Twitter);
82 let twitter = container.lookup('api:twitter');
83 twitter instanceof Twitter; // => true
84 // by default the container will return singletons
85 let twitter2 = container.lookup('api:twitter');
86 twitter2 instanceof Twitter; // => true
87 twitter === twitter2; //=> true
88 ```
89 If singletons are not wanted, an optional flag can be provided at lookup.
90 ```javascript
91 let registry = new Registry();
92 let container = registry.container();
93 registry.register('api:twitter', Twitter);
94 let twitter = container.lookup('api:twitter', { singleton: false });
95 let twitter2 = container.lookup('api:twitter', { singleton: false });
96 twitter === twitter2; //=> false
97 ```
98 @private
99 @method lookup
100 @param {String} fullName
101 @param {RegisterOptions} [options]
102 @return {any}
103 */
104 lookup(
105 fullName: string,
106 options?: RegisterOptions
107 ): InternalFactory<object> | object | undefined;
108 /**
109 A depth first traversal, destroying the container, its descendant containers and all
110 their managed objects.
111 @private
112 @method destroy
113 */
114 destroy(): void;
115 finalizeDestroy(): void;
116 /**
117 Clear either the entire cache or just the cache for a particular key.
118
119 @private
120 @method reset
121 @param {String} fullName optional key to reset; if missing, resets everything
122 */
123 reset(fullName: FullName): void;
124 /**
125 Returns an object that can be used to provide an owner to a
126 manually created instance.
127 @private
128 @method ownerInjection
129 @returns { Object }
130 */
131 ownerInjection(): {};
132 /**
133 Given a fullName, return the corresponding factory. The consumer of the factory
134 is responsible for the destruction of any factory instances, as there is no
135 way for the container to ensure instances are destroyed when it itself is
136 destroyed.
137 @public
138 @method factoryFor
139 @param {String} fullName
140 @return {any}
141 */
142 factoryFor(fullName: FullName): InternalFactoryManager<object> | undefined;
143 }
144 export interface LazyInjection {
145 namespace: string | undefined;
146 source: string | undefined;
147 specifier: string;
148 }
149 interface DebugFactory<T extends object, C extends FactoryClass | object = FactoryClass>
150 extends InternalFactory<T, C> {
151 _onLookup?: (fullName: string) => void;
152 _lazyInjections?: () => {
153 [key: string]: LazyInjection;
154 };
155 _initFactory?: (factoryManager: InternalFactoryManager<T, C>) => void;
156 }
157 export const INIT_FACTORY: unique symbol;
158 export function getFactoryFor(
159 obj: object
160 ): InternalFactoryManager<object, FactoryClass | object> | undefined;
161 export function setFactoryFor<T extends object, C extends FactoryClass | object>(
162 obj: object,
163 factory: InternalFactoryManager<T, C>
164 ): void;
165 export class InternalFactoryManager<
166 T extends object,
167 C extends FactoryClass | object = FactoryClass
168 > implements FactoryManager<T>
169 {
170 readonly container: Container;
171 readonly owner: InternalOwner | null;
172 readonly class: DebugFactory<T, C>;
173 readonly fullName: FullName;
174 readonly normalizedName: string;
175 private madeToString;
176 injections:
177 | {
178 [key: string]: unknown;
179 }
180 | undefined;
181 constructor(
182 container: Container,
183 factory: InternalFactory<T, C>,
184 fullName: FullName,
185 normalizedName: string
186 );
187 toString(): string;
188 create(options?: Partial<T>): T;
189 }
190 export {};
191}