@unional/async-context 8.1.1 → 9.0.1
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 +1 -1
- package/cjs/AsyncContext.d.ts.map +1 -1
- package/cjs/AsyncContext.js +6 -4
- package/cjs/AsyncContext.js.map +1 -1
- package/cjs/errors.d.ts.map +1 -1
- package/cjs/errors.js.map +1 -1
- package/cjs/index.d.ts +2 -2
- package/cjs/index.d.ts.map +1 -1
- package/cjs/index.js +2 -2
- package/cjs/index.js.map +1 -1
- package/cjs/package.json +1 -0
- package/esm/AsyncContext.d.ts.map +1 -1
- package/esm/AsyncContext.js +4 -2
- package/esm/AsyncContext.js.map +1 -1
- package/esm/errors.d.ts.map +1 -1
- package/esm/errors.js.map +1 -1
- package/esm/index.d.ts +2 -2
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +2 -2
- package/esm/index.js.map +1 -1
- package/package.json +27 -12
- package/ts/AsyncContext.ts +66 -62
- package/ts/errors.ts +10 -6
- package/ts/index.ts +2 -2
package/README.md
CHANGED
|
@@ -210,7 +210,7 @@ configure({ ... })
|
|
|
210
210
|
|
|
211
211
|
[bundlephobia-image]: https://img.shields.io/bundlephobia/minzip/@unional/async-context.svg
|
|
212
212
|
[bundlephobia-url]: https://bundlephobia.com/result?p=@unional/async-context
|
|
213
|
-
[codecov-image]: https://codecov.io/gh/unional/async-fp/branch/
|
|
213
|
+
[codecov-image]: https://codecov.io/gh/unional/async-fp/branch/main/graph/badge.svg
|
|
214
214
|
[codecov-url]: https://codecov.io/gh/unional/async-fp
|
|
215
215
|
[downloads-image]: https://img.shields.io/npm/dm/@unional/async-context.svg?style=flat
|
|
216
216
|
[downloads-url]: https://npmjs.org/package/@unional/async-context
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AsyncContext.d.ts","sourceRoot":"","sources":["../ts/AsyncContext.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EA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KAAY,WAAW,CAAC,cAAc,EAAE,iBAAiB,IAAI,CAC5D,OAAO,EAAE,cAAc,KACnB,iBAAiB,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAA;CACnD"}
|
package/cjs/AsyncContext.js
CHANGED
|
@@ -13,7 +13,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
13
13
|
var _AsyncContext_instances, _AsyncContext_resolving, _AsyncContext_resolvers, _AsyncContext_transforming, _AsyncContext_init, _AsyncContext_original, _AsyncContext_buildContext;
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.AsyncContext = void 0;
|
|
16
|
-
const
|
|
16
|
+
const errors_js_1 = require("./errors.js");
|
|
17
17
|
class AsyncContext {
|
|
18
18
|
constructor(init) {
|
|
19
19
|
_AsyncContext_instances.add(this);
|
|
@@ -26,7 +26,7 @@ class AsyncContext {
|
|
|
26
26
|
}
|
|
27
27
|
initialize(init) {
|
|
28
28
|
if (__classPrivateFieldGet(this, _AsyncContext_init, "f"))
|
|
29
|
-
throw new
|
|
29
|
+
throw new errors_js_1.ContextAlreadyInitialized({ ssf: this.initialize });
|
|
30
30
|
__classPrivateFieldSet(this, _AsyncContext_init, init, "f");
|
|
31
31
|
if (__classPrivateFieldGet(this, _AsyncContext_resolvers, "f").length > 0)
|
|
32
32
|
__classPrivateFieldGet(this, _AsyncContext_resolvers, "f").forEach(r => r(__classPrivateFieldGet(this, _AsyncContext_instances, "m", _AsyncContext_buildContext).call(this)));
|
|
@@ -52,10 +52,12 @@ class AsyncContext {
|
|
|
52
52
|
}
|
|
53
53
|
async get() {
|
|
54
54
|
if (__classPrivateFieldGet(this, _AsyncContext_transforming, "f"))
|
|
55
|
-
throw new
|
|
55
|
+
throw new errors_js_1.BlockingGetDetected({ ssf: this.get });
|
|
56
56
|
if (__classPrivateFieldGet(this, _AsyncContext_resolving, "f"))
|
|
57
57
|
return __classPrivateFieldGet(this, _AsyncContext_resolving, "f");
|
|
58
|
-
return __classPrivateFieldSet(this, _AsyncContext_resolving, __classPrivateFieldGet(this, _AsyncContext_init, "f")
|
|
58
|
+
return (__classPrivateFieldSet(this, _AsyncContext_resolving, __classPrivateFieldGet(this, _AsyncContext_init, "f")
|
|
59
|
+
? __classPrivateFieldGet(this, _AsyncContext_instances, "m", _AsyncContext_buildContext).call(this)
|
|
60
|
+
: new Promise(a => __classPrivateFieldGet(this, _AsyncContext_resolvers, "f").push(a)), "f"));
|
|
59
61
|
}
|
|
60
62
|
}
|
|
61
63
|
exports.AsyncContext = AsyncContext;
|
package/cjs/AsyncContext.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AsyncContext.js","sourceRoot":"","sources":["../ts/AsyncContext.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,
|
|
1
|
+
{"version":3,"file":"AsyncContext.js","sourceRoot":"","sources":["../ts/AsyncContext.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,2CAA4E;AAE5E,MAAa,YAAY;IASxB,YAAY,IAA4D;;QALxE,0CAAoC;QACpC,kCAAuD,EAAE,EAAA;QACzD,qCAAgB,KAAK,EAAA;QACrB,qCAAwE;QACxE,yCAA6C;QAE5C,uBAAA,IAAI,sBAAS,IAAI,MAAA,CAAA;IAClB,CAAC;IACD,UAAU,CACT,IAAkD;QAElD,IAAI,uBAAA,IAAI,0BAAM;YAAE,MAAM,IAAI,qCAAyB,CAAC,EAAE,GAAG,EAAE,IAAI,CAAC,UAAU,EAAE,CAAC,CAAA;QAC7E,uBAAA,IAAI,sBAAS,IAAI,MAAA,CAAA;QAEjB,IAAI,uBAAA,IAAI,+BAAW,CAAC,MAAM,GAAG,CAAC;YAAE,uBAAA,IAAI,+BAAW,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,uBAAA,IAAI,2DAAc,MAAlB,IAAI,CAAgB,CAAC,CAAC,CAAA;QACrF,OAAO,IAA8B,CAAA;IACt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,oCAuDC;gRAHA,KAAK;IACJ,OAAO,WAAW,CAAC,uBAAA,IAAI,0BAAO,CAA4B,CAAA;AAC3D,CAAC;AASF,KAAK,UAAU,WAAW,CACzB,IAA2D;IAE3D,OAAO,aAAa,CAAO,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,CAAA;AACjD,CAAC;AAED,SAAS,aAAa,CACrB,OAAgB;IAEhB,OAAO,OAAO,OAAO,KAAK,UAAU,CAAA;AACrC,CAAC;AAED,SAAS,aAAa,CAAI,OAAgB;IACzC,OAAO,OAAO,OAAO,KAAK,UAAU,CAAA;AACrC,CAAC","sourcesContent":["import type { LeftJoin } from 'type-plus'\nimport { BlockingGetDetected, ContextAlreadyInitialized } from './errors.js'\n\nexport class AsyncContext<\n\tInit extends Record<string | symbol, any>,\n\tContext extends Record<string | symbol, any> = Init\n> {\n\t#resolving: Promise<any> | undefined\n\t#resolvers: Array<(value: Promise<Context>) => void> = []\n\t#transforming = false\n\t#init: Init | Promise<Init> | AsyncContext.Initializer<Init> | undefined\n\t#original: AsyncContext<any, any> | undefined\n\tconstructor(init?: Init | Promise<Init> | AsyncContext.Initializer<Init>) {\n\t\tthis.#init = init\n\t}\n\tinitialize<I extends Init = Init>(\n\t\tinit: I | Promise<I> | AsyncContext.Initializer<I>\n\t): AsyncContext<I> {\n\t\tif (this.#init) throw new ContextAlreadyInitialized({ ssf: this.initialize })\n\t\tthis.#init = init\n\n\t\tif (this.#resolvers.length > 0) this.#resolvers.forEach(r => r(this.#buildContext()))\n\t\treturn this as any as AsyncContext<I>\n\t}\n\textend<\n\t\tCurrentContext extends Record<string | symbol, any> = Context,\n\t\tAdditionalContext extends Record<string | symbol, any> = Record<string | symbol, any>\n\t>(\n\t\tcontext:\n\t\t\t| AdditionalContext\n\t\t\t| Promise<AdditionalContext>\n\t\t\t| AsyncContext.Transformer<Context, AdditionalContext>\n\t): AsyncContext<Init, LeftJoin<CurrentContext, AdditionalContext>> {\n\t\tconst newctx = new AsyncContext(async () => {\n\t\t\ttry {\n\t\t\t\tconst value = await this.get()\n\t\t\t\tnewctx.#transforming = true\n\t\t\t\tconst newValue = await (isTransformer<any, any>(context) ? context(value) : context)\n\t\t\t\treturn { ...value, ...newValue }\n\t\t\t} finally {\n\t\t\t\tnewctx.#transforming = false\n\t\t\t}\n\t\t})\n\t\tconst ctx = this.#original ?? this\n\t\tnewctx.#original = ctx\n\t\tnewctx.initialize = newctx.initialize.bind(ctx)\n\t\treturn newctx\n\t}\n\tasync get<C = Context>(): Promise<C> {\n\t\tif (this.#transforming) throw new BlockingGetDetected({ ssf: this.get })\n\t\tif (this.#resolving) return this.#resolving\n\t\treturn (this.#resolving = this.#init\n\t\t\t? this.#buildContext()\n\t\t\t: new Promise<any>(a => this.#resolvers.push(a)))\n\t}\n\tasync #buildContext(): Promise<Context> {\n\t\treturn resolveInit(this.#init!) as any as Promise<Context>\n\t}\n}\nexport namespace AsyncContext {\n\texport type Initializer<Init> = () => Init | Promise<Init>\n\texport type Transformer<CurrentContext, AdditionalContext> = (\n\t\tcontext: CurrentContext\n\t) => AdditionalContext | Promise<AdditionalContext>\n}\n\nasync function resolveInit<Init extends Record<string | symbol, any>>(\n\tinit: Init | Promise<Init> | AsyncContext.Initializer<Init>\n): Promise<Init> {\n\treturn isInitializer<Init>(init) ? init() : init\n}\n\nfunction isTransformer<CurrentContext, AdditionalContext>(\n\tcontext: unknown\n): context is AsyncContext.Transformer<CurrentContext, AdditionalContext> {\n\treturn typeof context === 'function'\n}\n\nfunction isInitializer<T>(context: unknown): context is AsyncContext.Initializer<T> {\n\treturn typeof context === 'function'\n}\n"]}
|
package/cjs/errors.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../ts/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AAEvC,qBAAa,yBAA0B,SAAQ,WAAW;
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../ts/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AAEvC,qBAAa,yBAA0B,SAAQ,WAAW;gBAC7C,OAAO,CAAC,EAAE,WAAW,CAAC,OAAO;CAGzC;AAED,qBAAa,mBAAoB,SAAQ,WAAW;gBACvC,OAAO,CAAC,EAAE,WAAW,CAAC,OAAO;CAOzC"}
|
package/cjs/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../ts/errors.ts"],"names":[],"mappings":";;;AAAA,yCAAuC;AAEvC,MAAa,yBAA0B,SAAQ,uBAAW;
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../ts/errors.ts"],"names":[],"mappings":";;;AAAA,yCAAuC;AAEvC,MAAa,yBAA0B,SAAQ,uBAAW;IACzD,YAAY,OAA6B;QACxC,KAAK,CAAC,wBAAwB,EAAE,uCAAuC,EAAE,OAAO,CAAC,CAAA;IAClF,CAAC;CACD;AAJD,8DAIC;AAED,MAAa,mBAAoB,SAAQ,uBAAW;IACnD,YAAY,OAA6B;QACxC,KAAK,CACJ,wBAAwB,EACxB,gEAAgE,EAChE,OAAO,CACP,CAAA;IACF,CAAC;CACD;AARD,kDAQC","sourcesContent":["import { ModuleError } from 'iso-error'\n\nexport class ContextAlreadyInitialized extends ModuleError {\n\tconstructor(options?: ModuleError.Options) {\n\t\tsuper('@unional/async-context', 'context can only be initialized once.', options)\n\t}\n}\n\nexport class BlockingGetDetected extends ModuleError {\n\tconstructor(options?: ModuleError.Options) {\n\t\tsuper(\n\t\t\t'@unional/async-context',\n\t\t\t`a blocking get() detected. Are you calling it within extend()?`,\n\t\t\toptions\n\t\t)\n\t}\n}\n"]}
|
package/cjs/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from './AsyncContext';
|
|
2
|
-
export * from './errors';
|
|
1
|
+
export * from './AsyncContext.js';
|
|
2
|
+
export * from './errors.js';
|
|
3
3
|
//# sourceMappingURL=index.d.ts.map
|
package/cjs/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../ts/index.ts"],"names":[],"mappings":"AAAA,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../ts/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAA;AACjC,cAAc,aAAa,CAAA"}
|
package/cjs/index.js
CHANGED
|
@@ -14,6 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./AsyncContext"), exports);
|
|
18
|
-
__exportStar(require("./errors"), exports);
|
|
17
|
+
__exportStar(require("./AsyncContext.js"), exports);
|
|
18
|
+
__exportStar(require("./errors.js"), exports);
|
|
19
19
|
//# sourceMappingURL=index.js.map
|
package/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../ts/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../ts/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oDAAiC;AACjC,8CAA2B","sourcesContent":["export * from './AsyncContext.js'\nexport * from './errors.js'\n"]}
|
package/cjs/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type": "commonjs"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AsyncContext.d.ts","sourceRoot":"","sources":["../ts/AsyncContext.ts"],"names":[],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AG,OAAO,CAAC,IAAI,CAAC,CAAA;IAC1D,KAAY,WAAW,CAAC,cAAc,EAAE,iBAAiB,IAAI,CAC5D,OAAO,EAAE,cAAc,KACnB,iBAAiB,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAA;CACnD"}
|
package/esm/AsyncContext.js
CHANGED
|
@@ -10,7 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
12
|
var _AsyncContext_instances, _AsyncContext_resolving, _AsyncContext_resolvers, _AsyncContext_transforming, _AsyncContext_init, _AsyncContext_original, _AsyncContext_buildContext;
|
|
13
|
-
import { BlockingGetDetected, ContextAlreadyInitialized } from './errors';
|
|
13
|
+
import { BlockingGetDetected, ContextAlreadyInitialized } from './errors.js';
|
|
14
14
|
export class AsyncContext {
|
|
15
15
|
constructor(init) {
|
|
16
16
|
_AsyncContext_instances.add(this);
|
|
@@ -51,7 +51,9 @@ export class AsyncContext {
|
|
|
51
51
|
throw new BlockingGetDetected({ ssf: this.get });
|
|
52
52
|
if (__classPrivateFieldGet(this, _AsyncContext_resolving, "f"))
|
|
53
53
|
return __classPrivateFieldGet(this, _AsyncContext_resolving, "f");
|
|
54
|
-
return __classPrivateFieldSet(this, _AsyncContext_resolving, __classPrivateFieldGet(this, _AsyncContext_init, "f")
|
|
54
|
+
return (__classPrivateFieldSet(this, _AsyncContext_resolving, __classPrivateFieldGet(this, _AsyncContext_init, "f")
|
|
55
|
+
? __classPrivateFieldGet(this, _AsyncContext_instances, "m", _AsyncContext_buildContext).call(this)
|
|
56
|
+
: new Promise(a => __classPrivateFieldGet(this, _AsyncContext_resolvers, "f").push(a)), "f"));
|
|
55
57
|
}
|
|
56
58
|
}
|
|
57
59
|
_AsyncContext_resolving = new WeakMap(), _AsyncContext_resolvers = new WeakMap(), _AsyncContext_transforming = new WeakMap(), _AsyncContext_init = new WeakMap(), _AsyncContext_original = new WeakMap(), _AsyncContext_instances = new WeakSet(), _AsyncContext_buildContext = async function _AsyncContext_buildContext() {
|
package/esm/AsyncContext.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AsyncContext.js","sourceRoot":"","sources":["../ts/AsyncContext.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EAAE,mBAAmB,EAAE,yBAAyB,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"AsyncContext.js","sourceRoot":"","sources":["../ts/AsyncContext.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EAAE,mBAAmB,EAAE,yBAAyB,EAAE,MAAM,aAAa,CAAA;AAE5E,MAAM,OAAO,YAAY;IASxB,YAAY,IAA4D;;QALxE,0CAAoC;QACpC,kCAAuD,EAAE,EAAA;QACzD,qCAAgB,KAAK,EAAA;QACrB,qCAAwE;QACxE,yCAA6C;QAE5C,uBAAA,IAAI,sBAAS,IAAI,MAAA,CAAA;IAClB,CAAC;IACD,UAAU,CACT,IAAkD;QAElD,IAAI,uBAAA,IAAI,0BAAM;YAAE,MAAM,IAAI,yBAAyB,CAAC,EAAE,GAAG,EAAE,IAAI,CAAC,UAAU,EAAE,CAAC,CAAA;QAC7E,uBAAA,IAAI,sBAAS,IAAI,MAAA,CAAA;QAEjB,IAAI,uBAAA,IAAI,+BAAW,CAAC,MAAM,GAAG,CAAC;YAAE,uBAAA,IAAI,+BAAW,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,uBAAA,IAAI,2DAAc,MAAlB,IAAI,CAAgB,CAAC,CAAC,CAAA;QACrF,OAAO,IAA8B,CAAA;IACtC,CAAC;IACD,MAAM,CAIL,OAGuD;QAEvD,MAAM,MAAM,GAAG,IAAI,YAAY,CAAC,KAAK,IAAI,EAAE;YAC1C,IAAI;gBACH,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,GAAG,EAAE,CAAA;gBA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E,CAAC,CAAC,CAAC,IAAI,CAAA;AACjD,CAAC;AAED,SAAS,aAAa,CACrB,OAAgB;IAEhB,OAAO,OAAO,OAAO,KAAK,UAAU,CAAA;AACrC,CAAC;AAED,SAAS,aAAa,CAAI,OAAgB;IACzC,OAAO,OAAO,OAAO,KAAK,UAAU,CAAA;AACrC,CAAC","sourcesContent":["import type { LeftJoin } from 'type-plus'\nimport { BlockingGetDetected, ContextAlreadyInitialized } from './errors.js'\n\nexport class AsyncContext<\n\tInit extends Record<string | symbol, any>,\n\tContext extends Record<string | symbol, any> = Init\n> {\n\t#resolving: Promise<any> | undefined\n\t#resolvers: Array<(value: Promise<Context>) => void> = []\n\t#transforming = false\n\t#init: Init | Promise<Init> | AsyncContext.Initializer<Init> | undefined\n\t#original: AsyncContext<any, any> | undefined\n\tconstructor(init?: Init | Promise<Init> | AsyncContext.Initializer<Init>) {\n\t\tthis.#init = init\n\t}\n\tinitialize<I extends Init = Init>(\n\t\tinit: I | Promise<I> | AsyncContext.Initializer<I>\n\t): AsyncContext<I> {\n\t\tif (this.#init) throw new ContextAlreadyInitialized({ ssf: this.initialize })\n\t\tthis.#init = init\n\n\t\tif (this.#resolvers.length > 0) this.#resolvers.forEach(r => r(this.#buildContext()))\n\t\treturn this as any as AsyncContext<I>\n\t}\n\textend<\n\t\tCurrentContext extends Record<string | symbol, any> = Context,\n\t\tAdditionalContext extends Record<string | symbol, any> = Record<string | symbol, any>\n\t>(\n\t\tcontext:\n\t\t\t| AdditionalContext\n\t\t\t| Promise<AdditionalContext>\n\t\t\t| AsyncContext.Transformer<Context, AdditionalContext>\n\t): AsyncContext<Init, LeftJoin<CurrentContext, AdditionalContext>> {\n\t\tconst newctx = new AsyncContext(async () => {\n\t\t\ttry {\n\t\t\t\tconst value = await this.get()\n\t\t\t\tnewctx.#transforming = true\n\t\t\t\tconst newValue = await (isTransformer<any, any>(context) ? context(value) : context)\n\t\t\t\treturn { ...value, ...newValue }\n\t\t\t} finally {\n\t\t\t\tnewctx.#transforming = false\n\t\t\t}\n\t\t})\n\t\tconst ctx = this.#original ?? this\n\t\tnewctx.#original = ctx\n\t\tnewctx.initialize = newctx.initialize.bind(ctx)\n\t\treturn newctx\n\t}\n\tasync get<C = Context>(): Promise<C> {\n\t\tif (this.#transforming) throw new BlockingGetDetected({ ssf: this.get })\n\t\tif (this.#resolving) return this.#resolving\n\t\treturn (this.#resolving = this.#init\n\t\t\t? this.#buildContext()\n\t\t\t: new Promise<any>(a => this.#resolvers.push(a)))\n\t}\n\tasync #buildContext(): Promise<Context> {\n\t\treturn resolveInit(this.#init!) as any as Promise<Context>\n\t}\n}\nexport namespace AsyncContext {\n\texport type Initializer<Init> = () => Init | Promise<Init>\n\texport type Transformer<CurrentContext, AdditionalContext> = (\n\t\tcontext: CurrentContext\n\t) => AdditionalContext | Promise<AdditionalContext>\n}\n\nasync function resolveInit<Init extends Record<string | symbol, any>>(\n\tinit: Init | Promise<Init> | AsyncContext.Initializer<Init>\n): Promise<Init> {\n\treturn isInitializer<Init>(init) ? init() : init\n}\n\nfunction isTransformer<CurrentContext, AdditionalContext>(\n\tcontext: unknown\n): context is AsyncContext.Transformer<CurrentContext, AdditionalContext> {\n\treturn typeof context === 'function'\n}\n\nfunction isInitializer<T>(context: unknown): context is AsyncContext.Initializer<T> {\n\treturn typeof context === 'function'\n}\n"]}
|
package/esm/errors.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../ts/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AAEvC,qBAAa,yBAA0B,SAAQ,WAAW;
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../ts/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AAEvC,qBAAa,yBAA0B,SAAQ,WAAW;gBAC7C,OAAO,CAAC,EAAE,WAAW,CAAC,OAAO;CAGzC;AAED,qBAAa,mBAAoB,SAAQ,WAAW;gBACvC,OAAO,CAAC,EAAE,WAAW,CAAC,OAAO;CAOzC"}
|
package/esm/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../ts/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AAEvC,MAAM,OAAO,yBAA0B,SAAQ,WAAW;
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../ts/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AAEvC,MAAM,OAAO,yBAA0B,SAAQ,WAAW;IACzD,YAAY,OAA6B;QACxC,KAAK,CAAC,wBAAwB,EAAE,uCAAuC,EAAE,OAAO,CAAC,CAAA;IAClF,CAAC;CACD;AAED,MAAM,OAAO,mBAAoB,SAAQ,WAAW;IACnD,YAAY,OAA6B;QACxC,KAAK,CACJ,wBAAwB,EACxB,gEAAgE,EAChE,OAAO,CACP,CAAA;IACF,CAAC;CACD","sourcesContent":["import { ModuleError } from 'iso-error'\n\nexport class ContextAlreadyInitialized extends ModuleError {\n\tconstructor(options?: ModuleError.Options) {\n\t\tsuper('@unional/async-context', 'context can only be initialized once.', options)\n\t}\n}\n\nexport class BlockingGetDetected extends ModuleError {\n\tconstructor(options?: ModuleError.Options) {\n\t\tsuper(\n\t\t\t'@unional/async-context',\n\t\t\t`a blocking get() detected. Are you calling it within extend()?`,\n\t\t\toptions\n\t\t)\n\t}\n}\n"]}
|
package/esm/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from './AsyncContext';
|
|
2
|
-
export * from './errors';
|
|
1
|
+
export * from './AsyncContext.js';
|
|
2
|
+
export * from './errors.js';
|
|
3
3
|
//# sourceMappingURL=index.d.ts.map
|
package/esm/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../ts/index.ts"],"names":[],"mappings":"AAAA,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../ts/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAA;AACjC,cAAc,aAAa,CAAA"}
|
package/esm/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from './AsyncContext';
|
|
2
|
-
export * from './errors';
|
|
1
|
+
export * from './AsyncContext.js';
|
|
2
|
+
export * from './errors.js';
|
|
3
3
|
//# sourceMappingURL=index.js.map
|
package/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../ts/index.ts"],"names":[],"mappings":"AAAA,cAAc,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../ts/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAA;AACjC,cAAc,aAAa,CAAA","sourcesContent":["export * from './AsyncContext.js'\nexport * from './errors.js'\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unional/async-context",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "9.0.1",
|
|
4
4
|
"description": "Asynchronous context for functional style programming",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"async",
|
|
@@ -9,13 +9,14 @@
|
|
|
9
9
|
"functional",
|
|
10
10
|
"typescript"
|
|
11
11
|
],
|
|
12
|
-
"homepage": "https://github.com/unional/async-fp/tree/
|
|
12
|
+
"homepage": "https://github.com/unional/async-fp/tree/main/packages/async-context",
|
|
13
13
|
"bugs": {
|
|
14
14
|
"url": "https://github.com/unional/async-fp/issues"
|
|
15
15
|
},
|
|
16
16
|
"repository": {
|
|
17
17
|
"type": "git",
|
|
18
|
-
"url": "https://github.com/unional/async-fp.git"
|
|
18
|
+
"url": "https://github.com/unional/async-fp.git",
|
|
19
|
+
"directory": "packages/async-context"
|
|
19
20
|
},
|
|
20
21
|
"license": "MIT",
|
|
21
22
|
"author": {
|
|
@@ -23,19 +24,32 @@
|
|
|
23
24
|
"email": "homawong@gmail.com"
|
|
24
25
|
},
|
|
25
26
|
"sideEffects": false,
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
|
|
29
|
-
|
|
27
|
+
"type": "module",
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"import": {
|
|
31
|
+
"types": "./esm/index.d.ts",
|
|
32
|
+
"default": "./esm/index.js"
|
|
33
|
+
},
|
|
34
|
+
"require": {
|
|
35
|
+
"types": "./cjs/index.d.ts",
|
|
36
|
+
"default": "./cjs/index.js"
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"main": "./cjs/index.js",
|
|
41
|
+
"module": "./esm/index.js",
|
|
42
|
+
"jsnext:main": "./esm/index.js",
|
|
43
|
+
"types": "./esm/index.d.ts",
|
|
30
44
|
"files": [
|
|
31
45
|
"cjs",
|
|
32
46
|
"esm",
|
|
33
47
|
"ts",
|
|
34
|
-
"!**/*.spec.*"
|
|
48
|
+
"!**/*.{spec,test,unit,accept,integrate,system}.*"
|
|
35
49
|
],
|
|
36
50
|
"dependencies": {
|
|
37
51
|
"iso-error": "^6.0.0",
|
|
38
|
-
"type-plus": "^6.
|
|
52
|
+
"type-plus": "^6.4.0"
|
|
39
53
|
},
|
|
40
54
|
"devDependencies": {
|
|
41
55
|
"@repobuddy/jest": "^3.1.3",
|
|
@@ -54,11 +68,12 @@
|
|
|
54
68
|
"jest-watch-suspend": "^1.1.2",
|
|
55
69
|
"jest-watch-toggle-config-2": "^2.1.0",
|
|
56
70
|
"jest-watch-typeahead": "^2.2.2",
|
|
71
|
+
"ncp": "^2.0.0",
|
|
57
72
|
"npm-run-all": "^4.1.5",
|
|
58
|
-
"rimraf": "^
|
|
73
|
+
"rimraf": "^5.0.0",
|
|
59
74
|
"size-limit": "^8.0.1",
|
|
60
75
|
"ts-jest": "^29.0.0",
|
|
61
|
-
"typescript": "^5.0.
|
|
76
|
+
"typescript": "^5.0.4"
|
|
62
77
|
},
|
|
63
78
|
"engines": {
|
|
64
79
|
"node": ">= 14.16"
|
|
@@ -76,7 +91,7 @@
|
|
|
76
91
|
],
|
|
77
92
|
"scripts": {
|
|
78
93
|
"build": "run-p build:cjs build:esm",
|
|
79
|
-
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
94
|
+
"build:cjs": "tsc -p tsconfig.cjs.json && ncp ../../.config/package.cjs.json ./cjs/package.json",
|
|
80
95
|
"build:esm": "tsc",
|
|
81
96
|
"clean": "rimraf cjs esm coverage *.tsbuildinfo",
|
|
82
97
|
"coverage": "jest --coverage",
|
package/ts/AsyncContext.ts
CHANGED
|
@@ -1,77 +1,81 @@
|
|
|
1
1
|
import type { LeftJoin } from 'type-plus'
|
|
2
|
-
import { BlockingGetDetected, ContextAlreadyInitialized } from './errors'
|
|
2
|
+
import { BlockingGetDetected, ContextAlreadyInitialized } from './errors.js'
|
|
3
3
|
|
|
4
4
|
export class AsyncContext<
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
Init extends Record<string | symbol, any>,
|
|
6
|
+
Context extends Record<string | symbol, any> = Init
|
|
7
7
|
> {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
8
|
+
#resolving: Promise<any> | undefined
|
|
9
|
+
#resolvers: Array<(value: Promise<Context>) => void> = []
|
|
10
|
+
#transforming = false
|
|
11
|
+
#init: Init | Promise<Init> | AsyncContext.Initializer<Init> | undefined
|
|
12
|
+
#original: AsyncContext<any, any> | undefined
|
|
13
|
+
constructor(init?: Init | Promise<Init> | AsyncContext.Initializer<Init>) {
|
|
14
|
+
this.#init = init
|
|
15
|
+
}
|
|
16
|
+
initialize<I extends Init = Init>(
|
|
17
|
+
init: I | Promise<I> | AsyncContext.Initializer<I>
|
|
18
|
+
): AsyncContext<I> {
|
|
19
|
+
if (this.#init) throw new ContextAlreadyInitialized({ ssf: this.initialize })
|
|
20
|
+
this.#init = init
|
|
19
21
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
22
|
+
if (this.#resolvers.length > 0) this.#resolvers.forEach(r => r(this.#buildContext()))
|
|
23
|
+
return this as any as AsyncContext<I>
|
|
24
|
+
}
|
|
25
|
+
extend<
|
|
26
|
+
CurrentContext extends Record<string | symbol, any> = Context,
|
|
27
|
+
AdditionalContext extends Record<string | symbol, any> = Record<string | symbol, any>
|
|
28
|
+
>(
|
|
29
|
+
context:
|
|
30
|
+
| AdditionalContext
|
|
31
|
+
| Promise<AdditionalContext>
|
|
32
|
+
| AsyncContext.Transformer<Context, AdditionalContext>
|
|
33
|
+
): AsyncContext<Init, LeftJoin<CurrentContext, AdditionalContext>> {
|
|
34
|
+
const newctx = new AsyncContext(async () => {
|
|
35
|
+
try {
|
|
36
|
+
const value = await this.get()
|
|
37
|
+
newctx.#transforming = true
|
|
38
|
+
const newValue = await (isTransformer<any, any>(context) ? context(value) : context)
|
|
39
|
+
return { ...value, ...newValue }
|
|
40
|
+
} finally {
|
|
41
|
+
newctx.#transforming = false
|
|
42
|
+
}
|
|
43
|
+
})
|
|
44
|
+
const ctx = this.#original ?? this
|
|
45
|
+
newctx.#original = ctx
|
|
46
|
+
newctx.initialize = newctx.initialize.bind(ctx)
|
|
47
|
+
return newctx
|
|
48
|
+
}
|
|
49
|
+
async get<C = Context>(): Promise<C> {
|
|
50
|
+
if (this.#transforming) throw new BlockingGetDetected({ ssf: this.get })
|
|
51
|
+
if (this.#resolving) return this.#resolving
|
|
52
|
+
return (this.#resolving = this.#init
|
|
53
|
+
? this.#buildContext()
|
|
54
|
+
: new Promise<any>(a => this.#resolvers.push(a)))
|
|
55
|
+
}
|
|
56
|
+
async #buildContext(): Promise<Context> {
|
|
57
|
+
return resolveInit(this.#init!) as any as Promise<Context>
|
|
58
|
+
}
|
|
53
59
|
}
|
|
54
60
|
export namespace AsyncContext {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
> = (context: CurrentContext) => AdditionalContext | Promise<AdditionalContext>
|
|
61
|
+
export type Initializer<Init> = () => Init | Promise<Init>
|
|
62
|
+
export type Transformer<CurrentContext, AdditionalContext> = (
|
|
63
|
+
context: CurrentContext
|
|
64
|
+
) => AdditionalContext | Promise<AdditionalContext>
|
|
60
65
|
}
|
|
61
66
|
|
|
62
|
-
async function resolveInit<
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
67
|
+
async function resolveInit<Init extends Record<string | symbol, any>>(
|
|
68
|
+
init: Init | Promise<Init> | AsyncContext.Initializer<Init>
|
|
69
|
+
): Promise<Init> {
|
|
70
|
+
return isInitializer<Init>(init) ? init() : init
|
|
66
71
|
}
|
|
67
72
|
|
|
68
|
-
function isTransformer<
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
return typeof context === 'function'
|
|
73
|
+
function isTransformer<CurrentContext, AdditionalContext>(
|
|
74
|
+
context: unknown
|
|
75
|
+
): context is AsyncContext.Transformer<CurrentContext, AdditionalContext> {
|
|
76
|
+
return typeof context === 'function'
|
|
73
77
|
}
|
|
74
78
|
|
|
75
79
|
function isInitializer<T>(context: unknown): context is AsyncContext.Initializer<T> {
|
|
76
|
-
|
|
80
|
+
return typeof context === 'function'
|
|
77
81
|
}
|
package/ts/errors.ts
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
import { ModuleError } from 'iso-error'
|
|
2
2
|
|
|
3
3
|
export class ContextAlreadyInitialized extends ModuleError {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
constructor(options?: ModuleError.Options) {
|
|
5
|
+
super('@unional/async-context', 'context can only be initialized once.', options)
|
|
6
|
+
}
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
export class BlockingGetDetected extends ModuleError {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
constructor(options?: ModuleError.Options) {
|
|
11
|
+
super(
|
|
12
|
+
'@unional/async-context',
|
|
13
|
+
`a blocking get() detected. Are you calling it within extend()?`,
|
|
14
|
+
options
|
|
15
|
+
)
|
|
16
|
+
}
|
|
13
17
|
}
|
package/ts/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './AsyncContext'
|
|
2
|
-
export * from './errors'
|
|
1
|
+
export * from './AsyncContext.js'
|
|
2
|
+
export * from './errors.js'
|