wellcrafted 0.42.0 → 0.43.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.
@@ -0,0 +1,36 @@
1
+ //#region src/function.d.ts
2
+ /**
3
+ * Run-at-most-once wrapper. Wrap `fn` so the first call invokes it and caches the result;
4
+ * every later call is a no-op that returns that same cached result. Arguments passed after
5
+ * the first call are ignored.
6
+ *
7
+ * Canonical use: an idempotent `[Symbol.dispose]` whose teardown is reachable from more than
8
+ * one path and must not run twice. `once` makes that guarantee declarative instead of a
9
+ * hand-rolled `let disposed` flag.
10
+ *
11
+ * This is for the pure "this function body runs at most once" case. A boolean that is ALSO
12
+ * read by other methods to short-circuit a dead object is a liveness flag, not a once-guard;
13
+ * keep that boolean, `once` does not replace it.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * import { once } from "wellcrafted/function";
18
+ *
19
+ * const init = once(() => expensiveSetup());
20
+ * init(); // runs expensiveSetup()
21
+ * init(); // returns the cached result, does not run again
22
+ * ```
23
+ *
24
+ * @example Idempotent disposal
25
+ * ```ts
26
+ * import { once } from "wellcrafted/function";
27
+ *
28
+ * const dispose = once(() => closeConnection());
29
+ * // safe to call from multiple teardown paths; closeConnection() runs at most once
30
+ * ```
31
+ */
32
+ declare function once<TArgs extends readonly unknown[], TReturn>(fn: (...args: TArgs) => TReturn): (...args: TArgs) => TReturn;
33
+ //# sourceMappingURL=function.d.ts.map
34
+ //#endregion
35
+ export { once };
36
+ //# sourceMappingURL=function.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"function.d.ts","names":[],"sources":["../src/function.ts"],"sourcesContent":[],"mappings":";;AA8BA;;;;;;AAE8B;;;;;;;;;;;;;;;;;;;;;;;iBAFd,8DACD,UAAU,oBACZ,UAAU"}
@@ -0,0 +1,46 @@
1
+ //#region src/function.ts
2
+ /**
3
+ * Run-at-most-once wrapper. Wrap `fn` so the first call invokes it and caches the result;
4
+ * every later call is a no-op that returns that same cached result. Arguments passed after
5
+ * the first call are ignored.
6
+ *
7
+ * Canonical use: an idempotent `[Symbol.dispose]` whose teardown is reachable from more than
8
+ * one path and must not run twice. `once` makes that guarantee declarative instead of a
9
+ * hand-rolled `let disposed` flag.
10
+ *
11
+ * This is for the pure "this function body runs at most once" case. A boolean that is ALSO
12
+ * read by other methods to short-circuit a dead object is a liveness flag, not a once-guard;
13
+ * keep that boolean, `once` does not replace it.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * import { once } from "wellcrafted/function";
18
+ *
19
+ * const init = once(() => expensiveSetup());
20
+ * init(); // runs expensiveSetup()
21
+ * init(); // returns the cached result, does not run again
22
+ * ```
23
+ *
24
+ * @example Idempotent disposal
25
+ * ```ts
26
+ * import { once } from "wellcrafted/function";
27
+ *
28
+ * const dispose = once(() => closeConnection());
29
+ * // safe to call from multiple teardown paths; closeConnection() runs at most once
30
+ * ```
31
+ */
32
+ function once(fn) {
33
+ let called = false;
34
+ let result;
35
+ return (...args) => {
36
+ if (!called) {
37
+ called = true;
38
+ result = fn(...args);
39
+ }
40
+ return result;
41
+ };
42
+ }
43
+
44
+ //#endregion
45
+ export { once };
46
+ //# sourceMappingURL=function.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"function.js","names":["fn: (...args: TArgs) => TReturn","result: TReturn"],"sources":["../src/function.ts"],"sourcesContent":["/**\n * Run-at-most-once wrapper. Wrap `fn` so the first call invokes it and caches the result;\n * every later call is a no-op that returns that same cached result. Arguments passed after\n * the first call are ignored.\n *\n * Canonical use: an idempotent `[Symbol.dispose]` whose teardown is reachable from more than\n * one path and must not run twice. `once` makes that guarantee declarative instead of a\n * hand-rolled `let disposed` flag.\n *\n * This is for the pure \"this function body runs at most once\" case. A boolean that is ALSO\n * read by other methods to short-circuit a dead object is a liveness flag, not a once-guard;\n * keep that boolean, `once` does not replace it.\n *\n * @example\n * ```ts\n * import { once } from \"wellcrafted/function\";\n *\n * const init = once(() => expensiveSetup());\n * init(); // runs expensiveSetup()\n * init(); // returns the cached result, does not run again\n * ```\n *\n * @example Idempotent disposal\n * ```ts\n * import { once } from \"wellcrafted/function\";\n *\n * const dispose = once(() => closeConnection());\n * // safe to call from multiple teardown paths; closeConnection() runs at most once\n * ```\n */\nexport function once<TArgs extends readonly unknown[], TReturn>(\n\tfn: (...args: TArgs) => TReturn,\n): (...args: TArgs) => TReturn {\n\tlet called = false;\n\tlet result: TReturn;\n\treturn (...args: TArgs): TReturn => {\n\t\tif (!called) {\n\t\t\tcalled = true;\n\t\t\tresult = fn(...args);\n\t\t}\n\t\treturn result;\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8BA,SAAgB,KACfA,IAC8B;CAC9B,IAAI,SAAS;CACb,IAAIC;AACJ,QAAO,CAAC,GAAG,SAAyB;AACnC,OAAK,QAAQ;AACZ,YAAS;AACT,YAAS,GAAG,GAAG,KAAK;EACpB;AACD,SAAO;CACP;AACD"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wellcrafted",
3
- "version": "0.42.0",
3
+ "version": "0.43.0",
4
4
  "description": "Delightful TypeScript patterns for elegant, type-safe applications",
5
5
  "type": "module",
6
6
  "files": [
@@ -29,6 +29,10 @@
29
29
  "types": "./dist/brand.d.ts",
30
30
  "import": "./dist/brand.js"
31
31
  },
32
+ "./function": {
33
+ "types": "./dist/function.d.ts",
34
+ "import": "./dist/function.js"
35
+ },
32
36
  "./query": {
33
37
  "types": "./dist/query/index.d.ts",
34
38
  "import": "./dist/query/index.js"