getbox 1.3.1 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CONTEXT.md +17 -32
- package/README.md +102 -162
- package/dist/context.cjs +9 -25
- package/dist/context.d.cts +15 -21
- package/dist/context.d.mts +15 -21
- package/dist/context.mjs +8 -22
- package/dist/index.cjs +71 -140
- package/dist/index.d.cts +41 -102
- package/dist/index.d.mts +41 -102
- package/dist/index.mjs +69 -137
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
|
|
2
2
|
//#region src/index.ts
|
|
3
|
+
const cacheSymbol = Symbol("Box.cache");
|
|
4
|
+
const isClass = (fn) => fn.prototype !== void 0 && Function.prototype.toString.call(fn).startsWith("class");
|
|
3
5
|
/**
|
|
4
6
|
* Creates a {@link Constructor} from a factory function.
|
|
5
7
|
* The factory receives the box as an argument, allowing it to resolve other dependencies.
|
|
@@ -14,31 +16,36 @@
|
|
|
14
16
|
* ```
|
|
15
17
|
*/
|
|
16
18
|
function factory(init) {
|
|
17
|
-
return {
|
|
19
|
+
return {
|
|
20
|
+
[cacheSymbol]: true,
|
|
21
|
+
init
|
|
22
|
+
};
|
|
18
23
|
}
|
|
19
|
-
const noCacheSymbol = Symbol("Box.noCache");
|
|
20
24
|
/**
|
|
21
|
-
* Creates a {@link Constructor}
|
|
22
|
-
* The factory is called on every resolution, always returning a fresh value
|
|
23
|
-
* even when retrieved via {@link Box.get}.
|
|
25
|
+
* Creates a {@link Constructor} that computes a value from the box without caching the result.
|
|
24
26
|
*
|
|
25
27
|
* @example
|
|
26
28
|
* ```ts
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
29
|
+
* class Config {
|
|
30
|
+
* baseUrl = "https://example.com";
|
|
31
|
+
* }
|
|
32
|
+
*
|
|
33
|
+
* const RequestContext = computed((box) => ({
|
|
34
|
+
* baseUrl: box.get(Config).baseUrl,
|
|
35
|
+
* timestamp: Date.now(),
|
|
36
|
+
* }));
|
|
37
|
+
*
|
|
38
|
+
* const ctx1 = box.get(RequestContext);
|
|
39
|
+
* const ctx2 = box.get(RequestContext);
|
|
40
|
+
* console.log(ctx1 === ctx2); // false
|
|
31
41
|
* ```
|
|
32
42
|
*/
|
|
33
|
-
function
|
|
34
|
-
return {
|
|
35
|
-
init,
|
|
36
|
-
[noCacheSymbol]: true
|
|
37
|
-
};
|
|
43
|
+
function computed(init) {
|
|
44
|
+
return { init };
|
|
38
45
|
}
|
|
39
46
|
/**
|
|
40
47
|
* Creates a {@link Constructor} that always resolves to the given constant value.
|
|
41
|
-
* Constant values are
|
|
48
|
+
* Constant values are already fixed and do not need caching.
|
|
42
49
|
*
|
|
43
50
|
* @example
|
|
44
51
|
* ```ts
|
|
@@ -47,19 +54,15 @@ function transient(init) {
|
|
|
47
54
|
* ```
|
|
48
55
|
*/
|
|
49
56
|
function constant(value) {
|
|
50
|
-
return {
|
|
51
|
-
init: () => value,
|
|
52
|
-
[noCacheSymbol]: true
|
|
53
|
-
};
|
|
57
|
+
return { init: () => value };
|
|
54
58
|
}
|
|
55
59
|
/**
|
|
56
60
|
* Dependency injection container that resolves and caches instances from
|
|
57
61
|
* a {@link Constructor}.
|
|
58
62
|
*
|
|
59
|
-
* A constructor is
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
* create constructors from functions and values respectively.
|
|
63
|
+
* A constructor is a class with a `static init` function, a class with a
|
|
64
|
+
* no-argument constructor, or a function-based constructor created by
|
|
65
|
+
* {@link factory}, {@link computed}, or {@link constant}.
|
|
63
66
|
*
|
|
64
67
|
* @example
|
|
65
68
|
* ```ts
|
|
@@ -69,9 +72,7 @@ function constant(value) {
|
|
|
69
72
|
*
|
|
70
73
|
* class UserService {
|
|
71
74
|
* constructor(private db: Database) {}
|
|
72
|
-
* static init
|
|
73
|
-
* return new UserService(box.get(Database));
|
|
74
|
-
* }
|
|
75
|
+
* static init = Box.init(UserService).get(Database);
|
|
75
76
|
* }
|
|
76
77
|
*
|
|
77
78
|
* const box = new Box();
|
|
@@ -79,44 +80,38 @@ function constant(value) {
|
|
|
79
80
|
* const db = box.get(Database);
|
|
80
81
|
*
|
|
81
82
|
* console.log(service.db === db); // true (cached)
|
|
82
|
-
* console.log(box.new(Database) === db); // false (transient)
|
|
83
83
|
* ```
|
|
84
84
|
*/
|
|
85
85
|
var Box = class {
|
|
86
86
|
cache = /* @__PURE__ */ new Map();
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Resolves an instance from the cache, or creates and caches a new one.
|
|
96
|
-
* Subsequent calls with the same constructor return the cached instance.
|
|
97
|
-
*/
|
|
98
|
-
get(constructor) {
|
|
99
|
-
if (this.cache.has(constructor)) return this.cache.get(constructor);
|
|
100
|
-
const value = this.new(constructor);
|
|
101
|
-
if (!(noCacheSymbol in constructor && constructor[noCacheSymbol])) this.cache.set(constructor, value);
|
|
102
|
-
return value;
|
|
87
|
+
new(arg) {
|
|
88
|
+
if (Array.isArray(arg)) return arg.map((c) => this.new(c));
|
|
89
|
+
if (typeof arg === "function") return "init" in arg ? arg.init(this) : new arg();
|
|
90
|
+
if ("init" in arg && !isClass(arg.init)) return arg.init(this);
|
|
91
|
+
const result = {};
|
|
92
|
+
for (const [key, constructor] of Object.entries(arg)) result[key] = this.new(constructor);
|
|
93
|
+
return result;
|
|
103
94
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
95
|
+
get(arg) {
|
|
96
|
+
if (Array.isArray(arg)) return arg.map((c) => this.get(c));
|
|
97
|
+
if (typeof arg === "function") {
|
|
98
|
+
if (this.cache.has(arg)) return this.cache.get(arg);
|
|
99
|
+
const value = "init" in arg ? arg.init(this) : new arg();
|
|
100
|
+
this.cache.set(arg, value);
|
|
101
|
+
return value;
|
|
102
|
+
}
|
|
103
|
+
if ("init" in arg && !isClass(arg.init)) {
|
|
104
|
+
if (this.cache.has(arg)) return this.cache.get(arg);
|
|
105
|
+
const value = arg.init(this);
|
|
106
|
+
if (cacheSymbol in arg) this.cache.set(arg, value);
|
|
107
|
+
return value;
|
|
108
|
+
}
|
|
109
|
+
const result = {};
|
|
110
|
+
for (const [key, constructor] of Object.entries(arg)) result[key] = this.get(constructor);
|
|
111
|
+
return result;
|
|
116
112
|
}
|
|
117
113
|
/**
|
|
118
|
-
* Returns a
|
|
119
|
-
* The result can be assigned directly to `static init`.
|
|
114
|
+
* Returns a `static init` function builder.
|
|
120
115
|
*
|
|
121
116
|
* @example
|
|
122
117
|
* ```ts
|
|
@@ -127,7 +122,7 @@ var Box = class {
|
|
|
127
122
|
* ```
|
|
128
123
|
*/
|
|
129
124
|
static init(constructor) {
|
|
130
|
-
return new
|
|
125
|
+
return new StaticInit(constructor);
|
|
131
126
|
}
|
|
132
127
|
/**
|
|
133
128
|
* Registers a mock value in the box's cache for a given constructor.
|
|
@@ -139,107 +134,45 @@ var Box = class {
|
|
|
139
134
|
/**
|
|
140
135
|
* Removes the instance from the box's cache for a given constructor.
|
|
141
136
|
* Removes all instances if no constructor is provided.
|
|
142
|
-
*/
|
|
143
|
-
static clear(box, constructor) {
|
|
144
|
-
if (!constructor) return box.cache.clear();
|
|
145
|
-
box.cache.delete(constructor);
|
|
146
|
-
}
|
|
147
|
-
};
|
|
148
|
-
/** Resolves multiple constructors at once from a {@link Box}. */
|
|
149
|
-
var BoxAll = class {
|
|
150
|
-
constructor(box) {
|
|
151
|
-
this.box = box;
|
|
152
|
-
}
|
|
153
|
-
get(constructors) {
|
|
154
|
-
if (Array.isArray(constructors)) return constructors.map((c) => this.box.get(c));
|
|
155
|
-
const result = {};
|
|
156
|
-
for (const [key, constructor] of Object.entries(constructors)) result[key] = this.box.get(constructor);
|
|
157
|
-
return result;
|
|
158
|
-
}
|
|
159
|
-
new(constructors) {
|
|
160
|
-
if (Array.isArray(constructors)) return constructors.map((c) => this.box.new(c));
|
|
161
|
-
const result = {};
|
|
162
|
-
for (const [key, constructor] of Object.entries(constructors)) result[key] = this.box.new(constructor);
|
|
163
|
-
return result;
|
|
164
|
-
}
|
|
165
|
-
};
|
|
166
|
-
/** Builder for creating class instances with constructor dependencies resolved from a {@link Box}. */
|
|
167
|
-
var Construct = class {
|
|
168
|
-
constructor(box, construct) {
|
|
169
|
-
this.box = box;
|
|
170
|
-
this.construct = construct;
|
|
171
|
-
}
|
|
172
|
-
/**
|
|
173
|
-
* Resolves each dependency as a new transient instance via {@link Box.new},
|
|
174
|
-
* meaning dependencies are not cached or shared.
|
|
175
|
-
*
|
|
176
|
-
* The returned instance is cached or transient depending on whether the
|
|
177
|
-
* class is retrieved via {@link Box.get} or {@link Box.new}.
|
|
178
|
-
*
|
|
179
|
-
* @example
|
|
180
|
-
* ```ts
|
|
181
|
-
* class UserService {
|
|
182
|
-
* constructor(private db: Database, private logger: Logger) {}
|
|
183
|
-
* static init(box: Box) {
|
|
184
|
-
* return box.for(UserService).new(Database, LoggerFactory);
|
|
185
|
-
* }
|
|
186
|
-
* }
|
|
187
|
-
* ```
|
|
188
|
-
*/
|
|
189
|
-
new(...args) {
|
|
190
|
-
const instances = args.map((arg) => this.box.new(arg));
|
|
191
|
-
return new this.construct(...instances);
|
|
192
|
-
}
|
|
193
|
-
/**
|
|
194
|
-
* Resolves each dependency as a cached instance via {@link Box.get},
|
|
195
|
-
* meaning dependencies are shared across the box.
|
|
196
|
-
*
|
|
197
|
-
* The returned instance is cached or transient depending on whether the
|
|
198
|
-
* class is retrieved via {@link Box.get} or {@link Box.new}.
|
|
199
137
|
*
|
|
200
|
-
*
|
|
201
|
-
* ```ts
|
|
202
|
-
* class UserService {
|
|
203
|
-
* constructor(private db: Database, private logger: Logger) {}
|
|
204
|
-
* static init(box: Box) {
|
|
205
|
-
* return box.for(UserService).get(Database, LoggerFactory);
|
|
206
|
-
* }
|
|
207
|
-
* }
|
|
208
|
-
* ```
|
|
138
|
+
* Returns true if an instance existed and has been removed from the box's cache.
|
|
209
139
|
*/
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
140
|
+
static clear(box, constructor) {
|
|
141
|
+
if (!constructor) {
|
|
142
|
+
const size = box.cache.size;
|
|
143
|
+
box.cache.clear();
|
|
144
|
+
return size > 0;
|
|
145
|
+
}
|
|
146
|
+
return box.cache.delete(constructor);
|
|
213
147
|
}
|
|
214
148
|
};
|
|
215
149
|
/**
|
|
216
150
|
* Builder for creating a `static init` function with constructor dependencies
|
|
217
151
|
* resolved from a {@link Box}.
|
|
218
152
|
*/
|
|
219
|
-
var
|
|
153
|
+
var StaticInit = class {
|
|
220
154
|
constructor(construct) {
|
|
221
155
|
this.construct = construct;
|
|
222
156
|
}
|
|
223
157
|
/**
|
|
224
|
-
* Resolves each dependency as a new
|
|
158
|
+
* Resolves each dependency as a new instance via {@link Box.new},
|
|
225
159
|
* meaning dependencies are not cached or shared.
|
|
226
160
|
* Returns a function compatible with `static init` that can be assigned directly.
|
|
227
161
|
*
|
|
228
|
-
* The returned instance is cached or
|
|
162
|
+
* The returned instance is cached or new depending on whether the
|
|
229
163
|
* class is retrieved via {@link Box.get} or {@link Box.new}.
|
|
230
164
|
*
|
|
231
165
|
* @example
|
|
232
166
|
* ```ts
|
|
233
167
|
* class UserService {
|
|
234
168
|
* constructor(private db: Database, private logger: Logger) {}
|
|
235
|
-
* static init = Box.init(UserService).
|
|
169
|
+
* static init = Box.init(UserService).new(Database, LoggerFactory);
|
|
236
170
|
* }
|
|
237
171
|
* ```
|
|
238
172
|
*/
|
|
239
173
|
new(...args) {
|
|
240
174
|
return (box) => {
|
|
241
|
-
|
|
242
|
-
return new this.construct(...instances);
|
|
175
|
+
return new this.construct(...box.new(args));
|
|
243
176
|
};
|
|
244
177
|
}
|
|
245
178
|
/**
|
|
@@ -247,7 +180,7 @@ var StaticConstruct = class {
|
|
|
247
180
|
* meaning dependencies are shared across the box.
|
|
248
181
|
* Returns a function compatible with `static init` that can be assigned directly.
|
|
249
182
|
*
|
|
250
|
-
* The returned instance is cached or
|
|
183
|
+
* The returned instance is cached or new depending on whether the
|
|
251
184
|
* class is retrieved via {@link Box.get} or {@link Box.new}.
|
|
252
185
|
*
|
|
253
186
|
* @example
|
|
@@ -260,16 +193,14 @@ var StaticConstruct = class {
|
|
|
260
193
|
*/
|
|
261
194
|
get(...args) {
|
|
262
195
|
return (box) => {
|
|
263
|
-
|
|
264
|
-
return new this.construct(...instances);
|
|
196
|
+
return new this.construct(...box.get(args));
|
|
265
197
|
};
|
|
266
198
|
}
|
|
267
199
|
};
|
|
268
200
|
|
|
269
201
|
//#endregion
|
|
270
202
|
exports.Box = Box;
|
|
271
|
-
exports.
|
|
272
|
-
exports.
|
|
203
|
+
exports.StaticInit = StaticInit;
|
|
204
|
+
exports.computed = computed;
|
|
273
205
|
exports.constant = constant;
|
|
274
|
-
exports.factory = factory;
|
|
275
|
-
exports.transient = transient;
|
|
206
|
+
exports.factory = factory;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
//#region src/index.d.ts
|
|
2
2
|
/**
|
|
3
|
-
* A type that can be resolved by a {@link Box}. Either
|
|
4
|
-
* `init
|
|
5
|
-
*
|
|
3
|
+
* A type that can be resolved by a {@link Box}. Either a class with a
|
|
4
|
+
* `static init` function that returns an instance, a class with a
|
|
5
|
+
* no-argument constructor, or a function-based constructor created by
|
|
6
|
+
* {@link factory}, {@link computed}, or {@link constant}.
|
|
6
7
|
*/
|
|
7
8
|
type Constructor<T> = {
|
|
8
9
|
init(box: Box): T;
|
|
@@ -30,22 +31,28 @@ type ConstructorInstanceType<T> = T extends {
|
|
|
30
31
|
*/
|
|
31
32
|
declare function factory<T>(init: (box: Box) => T): Constructor<T>;
|
|
32
33
|
/**
|
|
33
|
-
* Creates a {@link Constructor}
|
|
34
|
-
* The factory is called on every resolution, always returning a fresh value
|
|
35
|
-
* even when retrieved via {@link Box.get}.
|
|
34
|
+
* Creates a {@link Constructor} that computes a value from the box without caching the result.
|
|
36
35
|
*
|
|
37
36
|
* @example
|
|
38
37
|
* ```ts
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
38
|
+
* class Config {
|
|
39
|
+
* baseUrl = "https://example.com";
|
|
40
|
+
* }
|
|
41
|
+
*
|
|
42
|
+
* const RequestContext = computed((box) => ({
|
|
43
|
+
* baseUrl: box.get(Config).baseUrl,
|
|
44
|
+
* timestamp: Date.now(),
|
|
45
|
+
* }));
|
|
46
|
+
*
|
|
47
|
+
* const ctx1 = box.get(RequestContext);
|
|
48
|
+
* const ctx2 = box.get(RequestContext);
|
|
49
|
+
* console.log(ctx1 === ctx2); // false
|
|
43
50
|
* ```
|
|
44
51
|
*/
|
|
45
|
-
declare function
|
|
52
|
+
declare function computed<T>(init: (box: Box) => T): Constructor<T>;
|
|
46
53
|
/**
|
|
47
54
|
* Creates a {@link Constructor} that always resolves to the given constant value.
|
|
48
|
-
* Constant values are
|
|
55
|
+
* Constant values are already fixed and do not need caching.
|
|
49
56
|
*
|
|
50
57
|
* @example
|
|
51
58
|
* ```ts
|
|
@@ -58,10 +65,9 @@ declare function constant<const T>(value: T): Constructor<T>;
|
|
|
58
65
|
* Dependency injection container that resolves and caches instances from
|
|
59
66
|
* a {@link Constructor}.
|
|
60
67
|
*
|
|
61
|
-
* A constructor is
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
* create constructors from functions and values respectively.
|
|
68
|
+
* A constructor is a class with a `static init` function, a class with a
|
|
69
|
+
* no-argument constructor, or a function-based constructor created by
|
|
70
|
+
* {@link factory}, {@link computed}, or {@link constant}.
|
|
65
71
|
*
|
|
66
72
|
* @example
|
|
67
73
|
* ```ts
|
|
@@ -71,9 +77,7 @@ declare function constant<const T>(value: T): Constructor<T>;
|
|
|
71
77
|
*
|
|
72
78
|
* class UserService {
|
|
73
79
|
* constructor(private db: Database) {}
|
|
74
|
-
* static init
|
|
75
|
-
* return new UserService(box.get(Database));
|
|
76
|
-
* }
|
|
80
|
+
* static init = Box.init(UserService).get(Database);
|
|
77
81
|
* }
|
|
78
82
|
*
|
|
79
83
|
* const box = new Box();
|
|
@@ -81,35 +85,27 @@ declare function constant<const T>(value: T): Constructor<T>;
|
|
|
81
85
|
* const db = box.get(Database);
|
|
82
86
|
*
|
|
83
87
|
* console.log(service.db === db); // true (cached)
|
|
84
|
-
* console.log(box.new(Database) === db); // false (transient)
|
|
85
88
|
* ```
|
|
86
89
|
*/
|
|
87
90
|
declare class Box {
|
|
88
|
-
|
|
91
|
+
protected cache: Map<Constructor<any>, any>;
|
|
89
92
|
/**
|
|
90
|
-
* Creates a new
|
|
91
|
-
*
|
|
93
|
+
* Creates a new instance without caching.
|
|
94
|
+
* Accepts a single constructor, an array of constructors, or an object map of constructors.
|
|
92
95
|
*/
|
|
93
96
|
new<T extends Constructor<any>>(constructor: T): ConstructorInstanceType<T>;
|
|
97
|
+
new<const T extends Constructor<any>[]>(constructors: T): { [K in keyof T]: ConstructorInstanceType<T[K]> };
|
|
98
|
+
new<T extends Record<string, Constructor<any>>>(constructors: T): { [K in keyof T]: ConstructorInstanceType<T[K]> };
|
|
94
99
|
/**
|
|
95
100
|
* Resolves an instance from the cache, or creates and caches a new one.
|
|
96
101
|
* Subsequent calls with the same constructor return the cached instance.
|
|
102
|
+
* Accepts a single constructor, an array of constructors, or an object map of constructors.
|
|
97
103
|
*/
|
|
98
104
|
get<T extends Constructor<any>>(constructor: T): ConstructorInstanceType<T>;
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Returns a {@link Construct} builder for creating class instances by
|
|
103
|
-
* resolving constructors for each constructor parameter.
|
|
104
|
-
*
|
|
105
|
-
* Intended to be used inside a class's `static init` method. The instance
|
|
106
|
-
* returned by the builder is never cached itself, but can be cached when
|
|
107
|
-
* the class is retrieved via {@link Box.get} or kept transient via {@link Box.new}.
|
|
108
|
-
*/
|
|
109
|
-
for<T extends ClassConstructor<any>>(constructor: T): Construct<T>;
|
|
105
|
+
get<const T extends Constructor<any>[]>(constructors: T): { [K in keyof T]: ConstructorInstanceType<T[K]> };
|
|
106
|
+
get<T extends Record<string, Constructor<any>>>(constructors: T): { [K in keyof T]: ConstructorInstanceType<T[K]> };
|
|
110
107
|
/**
|
|
111
|
-
* Returns a
|
|
112
|
-
* The result can be assigned directly to `static init`.
|
|
108
|
+
* Returns a `static init` function builder.
|
|
113
109
|
*
|
|
114
110
|
* @example
|
|
115
111
|
* ```ts
|
|
@@ -119,7 +115,7 @@ declare class Box {
|
|
|
119
115
|
* }
|
|
120
116
|
* ```
|
|
121
117
|
*/
|
|
122
|
-
static init<T extends ClassConstructor<any>>(constructor: T):
|
|
118
|
+
static init<T extends ClassConstructor<any>>(constructor: T): StaticInit<T>;
|
|
123
119
|
/**
|
|
124
120
|
* Registers a mock value in the box's cache for a given constructor.
|
|
125
121
|
* Useful for replacing dependencies in tests.
|
|
@@ -128,88 +124,31 @@ declare class Box {
|
|
|
128
124
|
/**
|
|
129
125
|
* Removes the instance from the box's cache for a given constructor.
|
|
130
126
|
* Removes all instances if no constructor is provided.
|
|
131
|
-
*/
|
|
132
|
-
static clear<T>(box: Box, constructor?: Constructor<T>): void;
|
|
133
|
-
}
|
|
134
|
-
/** Resolves multiple constructors at once from a {@link Box}. */
|
|
135
|
-
declare class BoxAll {
|
|
136
|
-
private box;
|
|
137
|
-
constructor(box: Box);
|
|
138
|
-
/**
|
|
139
|
-
* Resolves each constructor as a cached instance via {@link Box.get}.
|
|
140
|
-
* Accepts an array or object of constructors and returns instances in the same shape.
|
|
141
|
-
*/
|
|
142
|
-
get<const T extends Constructor<any>[]>(constructors: T): { [K in keyof T]: ConstructorInstanceType<T[K]> };
|
|
143
|
-
get<T extends Record<string, Constructor<any>>>(constructors: T): { [K in keyof T]: ConstructorInstanceType<T[K]> };
|
|
144
|
-
/**
|
|
145
|
-
* Resolves each constructor as a new transient instance via {@link Box.new}.
|
|
146
|
-
* Accepts an array or object of constructors and returns instances in the same shape.
|
|
147
|
-
*/
|
|
148
|
-
new<const T extends Constructor<any>[]>(constructors: T): { [K in keyof T]: ConstructorInstanceType<T[K]> };
|
|
149
|
-
new<T extends Record<string, Constructor<any>>>(constructors: T): { [K in keyof T]: ConstructorInstanceType<T[K]> };
|
|
150
|
-
}
|
|
151
|
-
/** Builder for creating class instances with constructor dependencies resolved from a {@link Box}. */
|
|
152
|
-
declare class Construct<T extends ClassConstructor<any>> {
|
|
153
|
-
private box;
|
|
154
|
-
private construct;
|
|
155
|
-
constructor(box: Box, construct: T);
|
|
156
|
-
/**
|
|
157
|
-
* Resolves each dependency as a new transient instance via {@link Box.new},
|
|
158
|
-
* meaning dependencies are not cached or shared.
|
|
159
|
-
*
|
|
160
|
-
* The returned instance is cached or transient depending on whether the
|
|
161
|
-
* class is retrieved via {@link Box.get} or {@link Box.new}.
|
|
162
127
|
*
|
|
163
|
-
*
|
|
164
|
-
* ```ts
|
|
165
|
-
* class UserService {
|
|
166
|
-
* constructor(private db: Database, private logger: Logger) {}
|
|
167
|
-
* static init(box: Box) {
|
|
168
|
-
* return box.for(UserService).new(Database, LoggerFactory);
|
|
169
|
-
* }
|
|
170
|
-
* }
|
|
171
|
-
* ```
|
|
128
|
+
* Returns true if an instance existed and has been removed from the box's cache.
|
|
172
129
|
*/
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* Resolves each dependency as a cached instance via {@link Box.get},
|
|
176
|
-
* meaning dependencies are shared across the box.
|
|
177
|
-
*
|
|
178
|
-
* The returned instance is cached or transient depending on whether the
|
|
179
|
-
* class is retrieved via {@link Box.get} or {@link Box.new}.
|
|
180
|
-
*
|
|
181
|
-
* @example
|
|
182
|
-
* ```ts
|
|
183
|
-
* class UserService {
|
|
184
|
-
* constructor(private db: Database, private logger: Logger) {}
|
|
185
|
-
* static init(box: Box) {
|
|
186
|
-
* return box.for(UserService).get(Database, LoggerFactory);
|
|
187
|
-
* }
|
|
188
|
-
* }
|
|
189
|
-
* ```
|
|
190
|
-
*/
|
|
191
|
-
get(...args: ClassConstructorArgs<T>): InstanceType<T>;
|
|
130
|
+
static clear<T>(box: Box, constructor?: Constructor<T>): boolean;
|
|
192
131
|
}
|
|
193
132
|
/**
|
|
194
133
|
* Builder for creating a `static init` function with constructor dependencies
|
|
195
134
|
* resolved from a {@link Box}.
|
|
196
135
|
*/
|
|
197
|
-
declare class
|
|
136
|
+
declare class StaticInit<T extends ClassConstructor<any>> {
|
|
198
137
|
private construct;
|
|
199
138
|
constructor(construct: T);
|
|
200
139
|
/**
|
|
201
|
-
* Resolves each dependency as a new
|
|
140
|
+
* Resolves each dependency as a new instance via {@link Box.new},
|
|
202
141
|
* meaning dependencies are not cached or shared.
|
|
203
142
|
* Returns a function compatible with `static init` that can be assigned directly.
|
|
204
143
|
*
|
|
205
|
-
* The returned instance is cached or
|
|
144
|
+
* The returned instance is cached or new depending on whether the
|
|
206
145
|
* class is retrieved via {@link Box.get} or {@link Box.new}.
|
|
207
146
|
*
|
|
208
147
|
* @example
|
|
209
148
|
* ```ts
|
|
210
149
|
* class UserService {
|
|
211
150
|
* constructor(private db: Database, private logger: Logger) {}
|
|
212
|
-
* static init = Box.init(UserService).
|
|
151
|
+
* static init = Box.init(UserService).new(Database, LoggerFactory);
|
|
213
152
|
* }
|
|
214
153
|
* ```
|
|
215
154
|
*/
|
|
@@ -219,7 +158,7 @@ declare class StaticConstruct<T extends ClassConstructor<any>> {
|
|
|
219
158
|
* meaning dependencies are shared across the box.
|
|
220
159
|
* Returns a function compatible with `static init` that can be assigned directly.
|
|
221
160
|
*
|
|
222
|
-
* The returned instance is cached or
|
|
161
|
+
* The returned instance is cached or new depending on whether the
|
|
223
162
|
* class is retrieved via {@link Box.get} or {@link Box.new}.
|
|
224
163
|
*
|
|
225
164
|
* @example
|
|
@@ -239,4 +178,4 @@ type ClassConstructor<T> = {
|
|
|
239
178
|
/** Maps each constructor parameter to its corresponding {@link Constructor} type. */
|
|
240
179
|
type ClassConstructorArgs<T extends ClassConstructor<any>, Args = ConstructorParameters<T>> = { [K in keyof Args]: Constructor<Args[K]> };
|
|
241
180
|
//#endregion
|
|
242
|
-
export { Box,
|
|
181
|
+
export { Box, Constructor, ConstructorInstanceType, StaticInit, computed, constant, factory };
|