getbox 1.1.0 → 1.2.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/README.md +17 -0
- package/dist/index.cjs +24 -4
- package/dist/index.d.cts +15 -1
- package/dist/index.d.mts +15 -1
- package/dist/index.mjs +23 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -172,6 +172,23 @@ const ApiUrl = factory((box: Box) => {
|
|
|
172
172
|
const apiUrl = box.get(ApiUrl); // "https://example.com/api"
|
|
173
173
|
```
|
|
174
174
|
|
|
175
|
+
## Transient factories
|
|
176
|
+
|
|
177
|
+
Use the `transient` helper to create a factory whose result is never cached. The factory is called on every resolution, even when retrieved via `box.get()`.
|
|
178
|
+
|
|
179
|
+
```ts
|
|
180
|
+
import { Box, transient } from "getbox";
|
|
181
|
+
|
|
182
|
+
const RequestId = transient(() => crypto.randomUUID());
|
|
183
|
+
|
|
184
|
+
const box = new Box();
|
|
185
|
+
|
|
186
|
+
const id1 = box.get(RequestId);
|
|
187
|
+
const id2 = box.get(RequestId);
|
|
188
|
+
|
|
189
|
+
console.log(id1 === id2); // false
|
|
190
|
+
```
|
|
191
|
+
|
|
175
192
|
## Resolving multiple constructors
|
|
176
193
|
|
|
177
194
|
Use `box.all.get()` to resolve multiple constructors at once. Pass an object to get an object of instances, or an array to get an array of instances.
|
package/dist/index.cjs
CHANGED
|
@@ -16,7 +16,26 @@
|
|
|
16
16
|
function factory(init) {
|
|
17
17
|
return { init };
|
|
18
18
|
}
|
|
19
|
-
const
|
|
19
|
+
const noCacheSymbol = Symbol("Box constant");
|
|
20
|
+
/**
|
|
21
|
+
* Creates a {@link Constructor} from a factory function whose result is never cached.
|
|
22
|
+
* The factory is called on every resolution, always returning a fresh value
|
|
23
|
+
* even when retrieved via {@link Box.get}.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* const RequestId = transient(() => crypto.randomUUID());
|
|
28
|
+
* const id1 = box.get(RequestId);
|
|
29
|
+
* const id2 = box.get(RequestId);
|
|
30
|
+
* console.log(id1 === id2); // false
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
function transient(init) {
|
|
34
|
+
return {
|
|
35
|
+
init,
|
|
36
|
+
[noCacheSymbol]: true
|
|
37
|
+
};
|
|
38
|
+
}
|
|
20
39
|
/**
|
|
21
40
|
* Creates a {@link Constructor} that always resolves to the given constant value.
|
|
22
41
|
* Constant values are never cached since they are already fixed.
|
|
@@ -30,7 +49,7 @@ const constantSymbol = Symbol("Box constant");
|
|
|
30
49
|
function constant(value) {
|
|
31
50
|
return {
|
|
32
51
|
init: () => value,
|
|
33
|
-
[
|
|
52
|
+
[noCacheSymbol]: true
|
|
34
53
|
};
|
|
35
54
|
}
|
|
36
55
|
/**
|
|
@@ -79,7 +98,7 @@ var Box = class {
|
|
|
79
98
|
get(constructor) {
|
|
80
99
|
if (this.cache.has(constructor)) return this.cache.get(constructor);
|
|
81
100
|
const value = this.new(constructor);
|
|
82
|
-
if (!(
|
|
101
|
+
if (!(noCacheSymbol in constructor && constructor[noCacheSymbol])) this.cache.set(constructor, value);
|
|
83
102
|
return value;
|
|
84
103
|
}
|
|
85
104
|
/** Resolves multiple constructors at once. */
|
|
@@ -183,4 +202,5 @@ var Construct = class {
|
|
|
183
202
|
exports.Box = Box;
|
|
184
203
|
exports.Construct = Construct;
|
|
185
204
|
exports.constant = constant;
|
|
186
|
-
exports.factory = factory;
|
|
205
|
+
exports.factory = factory;
|
|
206
|
+
exports.transient = transient;
|
package/dist/index.d.cts
CHANGED
|
@@ -25,6 +25,20 @@ type ConstructorInstanceType<T> = T extends Constructor<infer U> ? U : never;
|
|
|
25
25
|
* ```
|
|
26
26
|
*/
|
|
27
27
|
declare function factory<T>(init: (box: Box) => T): Constructor<T>;
|
|
28
|
+
/**
|
|
29
|
+
* Creates a {@link Constructor} from a factory function whose result is never cached.
|
|
30
|
+
* The factory is called on every resolution, always returning a fresh value
|
|
31
|
+
* even when retrieved via {@link Box.get}.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* const RequestId = transient(() => crypto.randomUUID());
|
|
36
|
+
* const id1 = box.get(RequestId);
|
|
37
|
+
* const id2 = box.get(RequestId);
|
|
38
|
+
* console.log(id1 === id2); // false
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
declare function transient<T>(init: (box: Box) => T): Constructor<T>;
|
|
28
42
|
/**
|
|
29
43
|
* Creates a {@link Constructor} that always resolves to the given constant value.
|
|
30
44
|
* Constant values are never cached since they are already fixed.
|
|
@@ -166,4 +180,4 @@ type ClassConstructor<T> = {
|
|
|
166
180
|
/** Maps each constructor parameter to its corresponding {@link Constructor} type. */
|
|
167
181
|
type ClassConstructorArgs<T extends ClassConstructor<any>, Args = ConstructorParameters<T>> = { [K in keyof Args]: Constructor<Args[K]> };
|
|
168
182
|
//#endregion
|
|
169
|
-
export { Box, Construct, Constructor, ConstructorInstanceType, constant, factory };
|
|
183
|
+
export { Box, Construct, Constructor, ConstructorInstanceType, constant, factory, transient };
|
package/dist/index.d.mts
CHANGED
|
@@ -25,6 +25,20 @@ type ConstructorInstanceType<T> = T extends Constructor<infer U> ? U : never;
|
|
|
25
25
|
* ```
|
|
26
26
|
*/
|
|
27
27
|
declare function factory<T>(init: (box: Box) => T): Constructor<T>;
|
|
28
|
+
/**
|
|
29
|
+
* Creates a {@link Constructor} from a factory function whose result is never cached.
|
|
30
|
+
* The factory is called on every resolution, always returning a fresh value
|
|
31
|
+
* even when retrieved via {@link Box.get}.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* const RequestId = transient(() => crypto.randomUUID());
|
|
36
|
+
* const id1 = box.get(RequestId);
|
|
37
|
+
* const id2 = box.get(RequestId);
|
|
38
|
+
* console.log(id1 === id2); // false
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
declare function transient<T>(init: (box: Box) => T): Constructor<T>;
|
|
28
42
|
/**
|
|
29
43
|
* Creates a {@link Constructor} that always resolves to the given constant value.
|
|
30
44
|
* Constant values are never cached since they are already fixed.
|
|
@@ -166,4 +180,4 @@ type ClassConstructor<T> = {
|
|
|
166
180
|
/** Maps each constructor parameter to its corresponding {@link Constructor} type. */
|
|
167
181
|
type ClassConstructorArgs<T extends ClassConstructor<any>, Args = ConstructorParameters<T>> = { [K in keyof Args]: Constructor<Args[K]> };
|
|
168
182
|
//#endregion
|
|
169
|
-
export { Box, Construct, Constructor, ConstructorInstanceType, constant, factory };
|
|
183
|
+
export { Box, Construct, Constructor, ConstructorInstanceType, constant, factory, transient };
|
package/dist/index.mjs
CHANGED
|
@@ -15,7 +15,26 @@
|
|
|
15
15
|
function factory(init) {
|
|
16
16
|
return { init };
|
|
17
17
|
}
|
|
18
|
-
const
|
|
18
|
+
const noCacheSymbol = Symbol("Box constant");
|
|
19
|
+
/**
|
|
20
|
+
* Creates a {@link Constructor} from a factory function whose result is never cached.
|
|
21
|
+
* The factory is called on every resolution, always returning a fresh value
|
|
22
|
+
* even when retrieved via {@link Box.get}.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* const RequestId = transient(() => crypto.randomUUID());
|
|
27
|
+
* const id1 = box.get(RequestId);
|
|
28
|
+
* const id2 = box.get(RequestId);
|
|
29
|
+
* console.log(id1 === id2); // false
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
function transient(init) {
|
|
33
|
+
return {
|
|
34
|
+
init,
|
|
35
|
+
[noCacheSymbol]: true
|
|
36
|
+
};
|
|
37
|
+
}
|
|
19
38
|
/**
|
|
20
39
|
* Creates a {@link Constructor} that always resolves to the given constant value.
|
|
21
40
|
* Constant values are never cached since they are already fixed.
|
|
@@ -29,7 +48,7 @@ const constantSymbol = Symbol("Box constant");
|
|
|
29
48
|
function constant(value) {
|
|
30
49
|
return {
|
|
31
50
|
init: () => value,
|
|
32
|
-
[
|
|
51
|
+
[noCacheSymbol]: true
|
|
33
52
|
};
|
|
34
53
|
}
|
|
35
54
|
/**
|
|
@@ -78,7 +97,7 @@ var Box = class {
|
|
|
78
97
|
get(constructor) {
|
|
79
98
|
if (this.cache.has(constructor)) return this.cache.get(constructor);
|
|
80
99
|
const value = this.new(constructor);
|
|
81
|
-
if (!(
|
|
100
|
+
if (!(noCacheSymbol in constructor && constructor[noCacheSymbol])) this.cache.set(constructor, value);
|
|
82
101
|
return value;
|
|
83
102
|
}
|
|
84
103
|
/** Resolves multiple constructors at once. */
|
|
@@ -179,4 +198,4 @@ var Construct = class {
|
|
|
179
198
|
};
|
|
180
199
|
|
|
181
200
|
//#endregion
|
|
182
|
-
export { Box, Construct, constant, factory };
|
|
201
|
+
export { Box, Construct, constant, factory, transient };
|