@anvil-di/anvil 0.0.4 → 0.0.6

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/dist/index.d.ts CHANGED
@@ -9,4 +9,5 @@
9
9
  */
10
10
  export * from "./decorators.js";
11
11
  export * from "./token.js";
12
+ export * from "./lazy.js";
12
13
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC"}
package/dist/index.js CHANGED
@@ -9,4 +9,5 @@
9
9
  */
10
10
  export * from "./decorators.js";
11
11
  export * from "./token.js";
12
+ export * from "./lazy.js";
12
13
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC"}
package/dist/lazy.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ /**
2
+ * A deferred handle to a binding. Inject `Lazy<T>` instead of `T` to delay
3
+ * constructing `T` until the first {@link Lazy.get} call, or to break a
4
+ * dependency cycle. The first result is memoized per handle.
5
+ *
6
+ * The generated dagger constructs these; user code only calls `get()`. An
7
+ * async-provided `T` is already resolved by `createX()`, so `Lazy` defers
8
+ * only synchronous construction.
9
+ */
10
+ export declare class Lazy<T> {
11
+ #private;
12
+ constructor(factory: () => T);
13
+ get(): T;
14
+ }
15
+ //# sourceMappingURL=lazy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lazy.d.ts","sourceRoot":"","sources":["../src/lazy.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,qBAAa,IAAI,CAAC,CAAC;;gBAIL,OAAO,EAAE,MAAM,CAAC;IAI5B,GAAG,IAAI,CAAC;CAOT"}
package/dist/lazy.js ADDED
@@ -0,0 +1,24 @@
1
+ /**
2
+ * A deferred handle to a binding. Inject `Lazy<T>` instead of `T` to delay
3
+ * constructing `T` until the first {@link Lazy.get} call, or to break a
4
+ * dependency cycle. The first result is memoized per handle.
5
+ *
6
+ * The generated dagger constructs these; user code only calls `get()`. An
7
+ * async-provided `T` is already resolved by `createX()`, so `Lazy` defers
8
+ * only synchronous construction.
9
+ */
10
+ export class Lazy {
11
+ #factory;
12
+ #value;
13
+ constructor(factory) {
14
+ this.#factory = factory;
15
+ }
16
+ get() {
17
+ if (this.#factory) {
18
+ this.#value = this.#factory();
19
+ this.#factory = undefined;
20
+ }
21
+ return this.#value;
22
+ }
23
+ }
24
+ //# sourceMappingURL=lazy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lazy.js","sourceRoot":"","sources":["../src/lazy.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,MAAM,OAAO,IAAI;IACf,QAAQ,CAAwB;IAChC,MAAM,CAAgB;IAEtB,YAAY,OAAgB;QAC1B,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC1B,CAAC;IAED,GAAG;QACD,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YAClB,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,QAAQ,EAAE,CAAC;YAC9B,IAAI,CAAC,QAAQ,GAAG,SAAS,CAAC;QAC5B,CAAC;QACD,OAAO,IAAI,CAAC,MAAW,CAAC;IAC1B,CAAC;CACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anvil-di/anvil",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "description": "Runtime stubs and types for the anvil compile-time dependency injection framework.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
package/src/index.ts CHANGED
@@ -10,3 +10,4 @@
10
10
 
11
11
  export * from "./decorators.js";
12
12
  export * from "./token.js";
13
+ export * from "./lazy.js";
@@ -0,0 +1,36 @@
1
+ import { describe, expect, it, vi } from "vitest";
2
+
3
+ import { Lazy } from "./lazy.js";
4
+
5
+ describe("Lazy", () => {
6
+ it("does not call the factory until get()", () => {
7
+ const factory = vi.fn(() => 1);
8
+ new Lazy(factory);
9
+ expect(factory).not.toHaveBeenCalled();
10
+ });
11
+
12
+ it("memoizes the first result", () => {
13
+ const factory = vi.fn(() => ({}));
14
+ const lazy = new Lazy(factory);
15
+ expect(lazy.get()).toBe(lazy.get());
16
+ expect(factory).toHaveBeenCalledTimes(1);
17
+ });
18
+
19
+ it("memoizes an undefined result", () => {
20
+ const factory = vi.fn(() => undefined);
21
+ const lazy = new Lazy(factory);
22
+ lazy.get();
23
+ lazy.get();
24
+ expect(factory).toHaveBeenCalledTimes(1);
25
+ });
26
+
27
+ it("retries after the factory throws", () => {
28
+ let calls = 0;
29
+ const lazy = new Lazy(() => {
30
+ if (++calls === 1) throw new Error("first");
31
+ return calls;
32
+ });
33
+ expect(() => lazy.get()).toThrow("first");
34
+ expect(lazy.get()).toBe(2);
35
+ });
36
+ });
package/src/lazy.ts ADDED
@@ -0,0 +1,25 @@
1
+ /**
2
+ * A deferred handle to a binding. Inject `Lazy<T>` instead of `T` to delay
3
+ * constructing `T` until the first {@link Lazy.get} call, or to break a
4
+ * dependency cycle. The first result is memoized per handle.
5
+ *
6
+ * The generated dagger constructs these; user code only calls `get()`. An
7
+ * async-provided `T` is already resolved by `createX()`, so `Lazy` defers
8
+ * only synchronous construction.
9
+ */
10
+ export class Lazy<T> {
11
+ #factory: (() => T) | undefined;
12
+ #value: T | undefined;
13
+
14
+ constructor(factory: () => T) {
15
+ this.#factory = factory;
16
+ }
17
+
18
+ get(): T {
19
+ if (this.#factory) {
20
+ this.#value = this.#factory();
21
+ this.#factory = undefined;
22
+ }
23
+ return this.#value as T;
24
+ }
25
+ }