@unional/async-context 3.1.1 → 8.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/cjs/AsyncContext.d.ts +1 -6
- package/cjs/AsyncContext.d.ts.map +1 -1
- package/cjs/AsyncContext.js +41 -44
- package/cjs/AsyncContext.js.map +1 -1
- package/cjs/errors.d.ts +3 -0
- package/cjs/errors.d.ts.map +1 -1
- package/cjs/errors.js +7 -1
- package/cjs/errors.js.map +1 -1
- package/esm/AsyncContext.d.ts +1 -6
- package/esm/AsyncContext.d.ts.map +1 -1
- package/esm/AsyncContext.js +41 -45
- package/esm/AsyncContext.js.map +1 -1
- package/esm/errors.d.ts +3 -0
- package/esm/errors.d.ts.map +1 -1
- package/esm/errors.js +5 -0
- package/esm/errors.js.map +1 -1
- package/package.json +1 -1
- package/ts/AsyncContext.ts +31 -20
- package/ts/errors.ts +6 -0
package/cjs/AsyncContext.d.ts
CHANGED
|
@@ -1,14 +1,9 @@
|
|
|
1
1
|
import { LeftJoin } from 'type-plus';
|
|
2
2
|
export declare class AsyncContext<Init extends Record<string | symbol, any>, Context extends Record<string | symbol, any> = Init> {
|
|
3
3
|
#private;
|
|
4
|
-
|
|
5
|
-
private transformers;
|
|
6
|
-
private resolving;
|
|
7
|
-
private resolvers;
|
|
8
|
-
constructor(init?: Init | Promise<Init> | AsyncContext.Initializer<Init> | undefined);
|
|
4
|
+
constructor(init?: Init | Promise<Init> | AsyncContext.Initializer<Init>);
|
|
9
5
|
initialize<I extends Init = Init>(init: I | Promise<I> | AsyncContext.Initializer<I>): AsyncContext<I>;
|
|
10
6
|
extend<CurrentContext extends Record<string | symbol, any> = Context, AdditionalContext extends Record<string | symbol, any> = Record<string | symbol, any>>(context: AdditionalContext | Promise<AdditionalContext> | AsyncContext.Transformer<CurrentContext, AdditionalContext>): AsyncContext<Init, LeftJoin<CurrentContext, AdditionalContext>>;
|
|
11
|
-
clone(): AsyncContext<Context, Context>;
|
|
12
7
|
get<C = Context>(): Promise<C>;
|
|
13
8
|
}
|
|
14
9
|
export declare namespace AsyncContext {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AsyncContext.d.ts","sourceRoot":"","sources":["../ts/AsyncContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA;AAGpC,qBAAa,YAAY,CACvB,IAAI,SAAS,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,GAAG,CAAC,EACzC,OAAO,SAAS,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,GAAG,CAAC,GAAG,IAAI;;
|
|
1
|
+
{"version":3,"file":"AsyncContext.d.ts","sourceRoot":"","sources":["../ts/AsyncContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA;AAGpC,qBAAa,YAAY,CACvB,IAAI,SAAS,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,GAAG,CAAC,EACzC,OAAO,SAAS,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,GAAG,CAAC,GAAG,IAAI;;gBAOvC,IAAI,CAAC,EAAE,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,GAAG,YAAY,CAAC,WAAW,CAAC,IAAI,CAAC;IAGxE,UAAU,CAAC,CAAC,SAAS,IAAI,GAAG,IAAI,EAAE,IAAI,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,GAAG,YAAY,CAAC,WAAW,CAAC,CAAC,CAAC,GAAG,YAAY,CAAC,CAAC,CAAC;IAOtG,MAAM,CACJ,cAAc,SAAS,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,GAAG,CAAC,GAAG,OAAO,EAC7D,iBAAiB,SAAS,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,GAAG,CAAC,EAErF,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAAC,iBAAiB,CAAC,GAAG,YAAY,CAAC,WAAW,CAAC,cAAc,EAAE,iBAAiB,CAAC,GACpH,YAAY,CAAC,IAAI,EAAE,QAAQ,CAAC,cAAc,EAAE,iBAAiB,CAAC,CAAC;IAiB5D,GAAG,CAAC,CAAC,GAAG,OAAO,KAAK,OAAO,CAAC,CAAC,CAAC;CAQrC;AACD,yBAAiB,YAAY,CAAC;IAC5B,KAAY,WAAW,CAAC,IAAI,IAAI,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAC1D,KAAY,WAAW,CACrB,cAAc,EACd,iBAAiB,IACb,CAAC,OAAO,EAAE,cAAc,KAAK,iBAAiB,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAA;CAClF"}
|
package/cjs/AsyncContext.js
CHANGED
|
@@ -1,69 +1,66 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
+
};
|
|
2
8
|
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
9
|
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
10
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
5
11
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
12
|
};
|
|
7
|
-
var _AsyncContext_instances, _AsyncContext_buildContext;
|
|
13
|
+
var _AsyncContext_instances, _AsyncContext_resolving, _AsyncContext_resolvers, _AsyncContext_transforming, _AsyncContext_init, _AsyncContext_original, _AsyncContext_buildContext;
|
|
8
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
15
|
exports.AsyncContext = void 0;
|
|
10
16
|
const errors_1 = require("./errors");
|
|
11
17
|
class AsyncContext {
|
|
12
18
|
constructor(init) {
|
|
13
19
|
_AsyncContext_instances.add(this);
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
Object.defineProperty(this, "transformers", {
|
|
21
|
-
enumerable: true,
|
|
22
|
-
configurable: true,
|
|
23
|
-
writable: true,
|
|
24
|
-
value: []
|
|
25
|
-
});
|
|
26
|
-
Object.defineProperty(this, "resolving", {
|
|
27
|
-
enumerable: true,
|
|
28
|
-
configurable: true,
|
|
29
|
-
writable: true,
|
|
30
|
-
value: void 0
|
|
31
|
-
});
|
|
32
|
-
Object.defineProperty(this, "resolvers", {
|
|
33
|
-
enumerable: true,
|
|
34
|
-
configurable: true,
|
|
35
|
-
writable: true,
|
|
36
|
-
value: []
|
|
37
|
-
});
|
|
20
|
+
_AsyncContext_resolving.set(this, void 0);
|
|
21
|
+
_AsyncContext_resolvers.set(this, []);
|
|
22
|
+
_AsyncContext_transforming.set(this, false);
|
|
23
|
+
_AsyncContext_init.set(this, void 0);
|
|
24
|
+
_AsyncContext_original.set(this, void 0);
|
|
25
|
+
__classPrivateFieldSet(this, _AsyncContext_init, init, "f");
|
|
38
26
|
}
|
|
39
27
|
initialize(init) {
|
|
40
|
-
if (this
|
|
41
|
-
throw new errors_1.ContextAlreadyInitialized();
|
|
42
|
-
this
|
|
43
|
-
if (this.
|
|
44
|
-
this.
|
|
28
|
+
if (__classPrivateFieldGet(this, _AsyncContext_init, "f"))
|
|
29
|
+
throw new errors_1.ContextAlreadyInitialized({ ssf: this.initialize });
|
|
30
|
+
__classPrivateFieldSet(this, _AsyncContext_init, init, "f");
|
|
31
|
+
if (__classPrivateFieldGet(this, _AsyncContext_resolvers, "f").length > 0)
|
|
32
|
+
__classPrivateFieldGet(this, _AsyncContext_resolvers, "f").forEach(r => r(__classPrivateFieldGet(this, _AsyncContext_instances, "m", _AsyncContext_buildContext).call(this)));
|
|
45
33
|
return this;
|
|
46
34
|
}
|
|
47
35
|
extend(context) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
36
|
+
var _a;
|
|
37
|
+
const newctx = new AsyncContext(async () => {
|
|
38
|
+
try {
|
|
39
|
+
const value = await this.get();
|
|
40
|
+
__classPrivateFieldSet(newctx, _AsyncContext_transforming, true, "f");
|
|
41
|
+
const newValue = await (isTransformer(context) ? context(value) : context);
|
|
42
|
+
return { ...value, ...newValue };
|
|
43
|
+
}
|
|
44
|
+
finally {
|
|
45
|
+
__classPrivateFieldSet(newctx, _AsyncContext_transforming, false, "f");
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
const ctx = (_a = __classPrivateFieldGet(this, _AsyncContext_original, "f")) !== null && _a !== void 0 ? _a : this;
|
|
49
|
+
__classPrivateFieldSet(newctx, _AsyncContext_original, ctx, "f");
|
|
50
|
+
newctx.initialize = newctx.initialize.bind(ctx);
|
|
51
|
+
return newctx;
|
|
54
52
|
}
|
|
55
53
|
async get() {
|
|
56
|
-
if (this
|
|
57
|
-
|
|
58
|
-
|
|
54
|
+
if (__classPrivateFieldGet(this, _AsyncContext_transforming, "f"))
|
|
55
|
+
throw new errors_1.BlockingGetDetected({ ssf: this.get });
|
|
56
|
+
if (__classPrivateFieldGet(this, _AsyncContext_resolving, "f"))
|
|
57
|
+
return __classPrivateFieldGet(this, _AsyncContext_resolving, "f");
|
|
58
|
+
return __classPrivateFieldSet(this, _AsyncContext_resolving, __classPrivateFieldGet(this, _AsyncContext_init, "f") ? __classPrivateFieldGet(this, _AsyncContext_instances, "m", _AsyncContext_buildContext).call(this) : new Promise(a => __classPrivateFieldGet(this, _AsyncContext_resolvers, "f").push(a)), "f");
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
61
|
exports.AsyncContext = AsyncContext;
|
|
62
|
-
_AsyncContext_instances = new WeakSet(), _AsyncContext_buildContext = async function _AsyncContext_buildContext() {
|
|
63
|
-
return
|
|
64
|
-
const c = await p;
|
|
65
|
-
return { ...c, ...await t(c) };
|
|
66
|
-
}, resolveInit(this.init));
|
|
62
|
+
_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() {
|
|
63
|
+
return resolveInit(__classPrivateFieldGet(this, _AsyncContext_init, "f"));
|
|
67
64
|
};
|
|
68
65
|
async function resolveInit(init) {
|
|
69
66
|
return isInitializer(init) ? init() : init;
|
package/cjs/AsyncContext.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AsyncContext.js","sourceRoot":"","sources":["../ts/AsyncContext.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"AsyncContext.js","sourceRoot":"","sources":["../ts/AsyncContext.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,qCAAyE;AAEzE,MAAa,YAAY;IASvB,YAAY,IAA4D;;QALxE,0CAAoC;QACpC,kCAAyD,EAAE,EAAA;QAC3D,qCAAgB,KAAK,EAAA;QACrB,qCAAwE;QACxE,yCAA6C;QAE3C,uBAAA,IAAI,sBAAS,IAAI,MAAA,CAAA;IACnB,CAAC;IACD,UAAU,CAAwB,IAAkD;QAClF,IAAI,uBAAA,IAAI,0BAAM;YAAE,MAAM,IAAI,kCAAyB,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,IAAuB,CAAA;IAChC,CAAC;IACD,MAAM,CAIJ,OAAqH;;QAErH,MAAM,MAAM,GAAG,IAAI,YAAY,CAAC,KAAK,IAAI,EAAE;YACzC,IAAI;gBACF,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,GAAG,EAAE,CAAA;gBAC9B,uBAAA,MAAM,8BAAiB,IAAI,MAAA,CAAA;gBAC3B,MAAM,QAAQ,GAAG,MAAM,CAAC,aAAa,CAAW,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAA;gBACpF,OAAO,EAAE,GAAG,KAAK,EAAE,GAAG,QAAQ,EAAE,CAAA;aACjC;oBACO;gBACN,uBAAA,MAAM,8BAAiB,KAAK,MAAA,CAAA;aAC7B;QACH,CAAC,CAAC,CAAA;QACF,MAAM,GAAG,GAAG,MAAA,uBAAA,IAAI,8BAAU,mCAAI,IAAI,CAAA;QAClC,uBAAA,MAAM,0BAAa,GAAG,MAAA,CAAA;QACtB,MAAM,CAAC,UAAU,GAAG,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;QAC/C,OAAO,MAAM,CAAA;IACf,CAAC;IACD,KAAK,CAAC,GAAG;QACP,IAAI,uBAAA,IAAI,kCAAc;YAAE,MAAM,IAAI,4BAAmB,CAAC,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,CAAC,CAAA;QACxE,IAAI,uBAAA,IAAI,+BAAW;YAAE,OAAO,uBAAA,IAAI,+BAAW,CAAA;QAC3C,OAAO,uBAAA,IAAI,2BAAc,uBAAA,IAAI,0BAAM,CAAC,CAAC,CAAC,uBAAA,IAAI,2DAAc,MAAlB,IAAI,CAAgB,CAAC,CAAC,CAAC,IAAI,OAAO,CAAM,CAAC,CAAC,EAAE,CAAC,uBAAA,IAAI,+BAAW,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAA,CAAA;IAC7G,CAAC;CAIF;AAjDD,oCAiDC;gRAHC,KAAK;IACH,OAAO,WAAW,CAAC,uBAAA,IAAI,0BAAO,CAAqB,CAAA;AACrD,CAAC;AAUH,KAAK,UAAU,WAAW,CAEtB,IAA2D;IAC7D,OAAO,aAAa,CAAO,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,CAAA;AAClD,CAAC;AAED,SAAS,aAAa,CAGpB,OAAgB;IAChB,OAAO,OAAO,OAAO,KAAK,UAAU,CAAA;AACtC,CAAC;AAED,SAAS,aAAa,CAAI,OAAgB;IACxC,OAAO,OAAO,OAAO,KAAK,UAAU,CAAA;AACtC,CAAC","sourcesContent":["import { LeftJoin } from 'type-plus'\nimport { BlockingGetDetected, ContextAlreadyInitialized } from './errors'\n\nexport class AsyncContext<\n Init extends Record<string | symbol, any>,\n Context extends Record<string | symbol, any> = Init\n > {\n #resolving: Promise<any> | undefined\n #resolvers: Array<((value: Promise<Context>) => void)> = []\n #transforming = false\n #init: Init | Promise<Init> | AsyncContext.Initializer<Init> | undefined\n #original: AsyncContext<any, any> | undefined\n constructor(init?: Init | Promise<Init> | AsyncContext.Initializer<Init>) {\n this.#init = init\n }\n initialize<I extends Init = Init>(init: I | Promise<I> | AsyncContext.Initializer<I>): AsyncContext<I> {\n if (this.#init) throw new ContextAlreadyInitialized({ ssf: this.initialize })\n this.#init = init\n\n if (this.#resolvers.length > 0) this.#resolvers.forEach(r => r(this.#buildContext()))\n return this as AsyncContext<I>\n }\n extend<\n CurrentContext extends Record<string | symbol, any> = Context,\n AdditionalContext extends Record<string | symbol, any> = Record<string | symbol, any>\n >(\n context: AdditionalContext | Promise<AdditionalContext> | AsyncContext.Transformer<CurrentContext, AdditionalContext>\n ): AsyncContext<Init, LeftJoin<CurrentContext, AdditionalContext>> {\n const newctx = new AsyncContext(async () => {\n try {\n const value = await this.get()\n newctx.#transforming = true\n const newValue = await (isTransformer<any, any>(context) ? context(value) : context)\n return { ...value, ...newValue }\n }\n finally {\n newctx.#transforming = false\n }\n })\n const ctx = this.#original ?? this\n newctx.#original = ctx\n newctx.initialize = newctx.initialize.bind(ctx)\n return newctx\n }\n async get<C = Context>(): Promise<C> {\n if (this.#transforming) throw new BlockingGetDetected({ ssf: this.get })\n if (this.#resolving) return this.#resolving\n return this.#resolving = this.#init ? this.#buildContext() : new Promise<any>(a => this.#resolvers.push(a))\n }\n async #buildContext(): Promise<Context> {\n return resolveInit(this.#init!) as Promise<Context>\n }\n}\nexport namespace AsyncContext {\n export type Initializer<Init> = () => Init | Promise<Init>\n export type Transformer<\n CurrentContext,\n AdditionalContext\n > = (context: CurrentContext) => AdditionalContext | Promise<AdditionalContext>\n}\n\nasync function resolveInit<\n Init extends Record<string | symbol, any>,\n >(init: Init | Promise<Init> | AsyncContext.Initializer<Init>): Promise<Init> {\n return isInitializer<Init>(init) ? init() : init\n}\n\nfunction isTransformer<\n CurrentContext,\n AdditionalContext\n>(context: unknown): context is AsyncContext.Transformer<CurrentContext, AdditionalContext> {\n return typeof context === 'function'\n}\n\nfunction isInitializer<T>(context: unknown): context is AsyncContext.Initializer<T> {\n return typeof context === 'function'\n}\n"]}
|
package/cjs/errors.d.ts
CHANGED
|
@@ -2,4 +2,7 @@ import { IsoError, ModuleError } from 'iso-error';
|
|
|
2
2
|
export declare class ContextAlreadyInitialized extends ModuleError {
|
|
3
3
|
constructor(options?: IsoError.Options);
|
|
4
4
|
}
|
|
5
|
+
export declare class BlockingGetDetected extends ModuleError {
|
|
6
|
+
constructor(options?: IsoError.Options);
|
|
7
|
+
}
|
|
5
8
|
//# sourceMappingURL=errors.d.ts.map
|
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,QAAQ,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AAEjD,qBAAa,yBAA0B,SAAQ,WAAW;gBAC5C,OAAO,CAAC,EAAE,QAAQ,CAAC,OAAO;CAGvC"}
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../ts/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AAEjD,qBAAa,yBAA0B,SAAQ,WAAW;gBAC5C,OAAO,CAAC,EAAE,QAAQ,CAAC,OAAO;CAGvC;AAED,qBAAa,mBAAoB,SAAQ,WAAW;gBACtC,OAAO,CAAC,EAAE,QAAQ,CAAC,OAAO;CAGvC"}
|
package/cjs/errors.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ContextAlreadyInitialized = void 0;
|
|
3
|
+
exports.BlockingGetDetected = exports.ContextAlreadyInitialized = void 0;
|
|
4
4
|
const iso_error_1 = require("iso-error");
|
|
5
5
|
class ContextAlreadyInitialized extends iso_error_1.ModuleError {
|
|
6
6
|
constructor(options) {
|
|
@@ -8,4 +8,10 @@ class ContextAlreadyInitialized extends iso_error_1.ModuleError {
|
|
|
8
8
|
}
|
|
9
9
|
}
|
|
10
10
|
exports.ContextAlreadyInitialized = ContextAlreadyInitialized;
|
|
11
|
+
class BlockingGetDetected extends iso_error_1.ModuleError {
|
|
12
|
+
constructor(options) {
|
|
13
|
+
super('@unional/async-context', `a blocking get() detected. Are you calling it within extend()?`, options);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.BlockingGetDetected = BlockingGetDetected;
|
|
11
17
|
//# sourceMappingURL=errors.js.map
|
package/cjs/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../ts/errors.ts"],"names":[],"mappings":";;;AAAA,yCAAiD;AAEjD,MAAa,yBAA0B,SAAQ,uBAAW;IACxD,YAAY,OAA0B;QACpC,KAAK,CAAC,wBAAwB,EAAE,uCAAuC,EAAE,OAAO,CAAC,CAAA;IACnF,CAAC;CACF;AAJD,8DAIC","sourcesContent":["import { IsoError, ModuleError } from 'iso-error'\n\nexport class ContextAlreadyInitialized extends ModuleError {\n constructor(options?: IsoError.Options) {\n super('@unional/async-context', 'context can only be initialized once.', options)\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../ts/errors.ts"],"names":[],"mappings":";;;AAAA,yCAAiD;AAEjD,MAAa,yBAA0B,SAAQ,uBAAW;IACxD,YAAY,OAA0B;QACpC,KAAK,CAAC,wBAAwB,EAAE,uCAAuC,EAAE,OAAO,CAAC,CAAA;IACnF,CAAC;CACF;AAJD,8DAIC;AAED,MAAa,mBAAoB,SAAQ,uBAAW;IAClD,YAAY,OAA0B;QACpC,KAAK,CAAC,wBAAwB,EAAE,gEAAgE,EAAE,OAAO,CAAC,CAAA;IAC5G,CAAC;CACF;AAJD,kDAIC","sourcesContent":["import { IsoError, ModuleError } from 'iso-error'\n\nexport class ContextAlreadyInitialized extends ModuleError {\n constructor(options?: IsoError.Options) {\n super('@unional/async-context', 'context can only be initialized once.', options)\n }\n}\n\nexport class BlockingGetDetected extends ModuleError {\n constructor(options?: IsoError.Options) {\n super('@unional/async-context', `a blocking get() detected. Are you calling it within extend()?`, options)\n }\n}\n"]}
|
package/esm/AsyncContext.d.ts
CHANGED
|
@@ -1,14 +1,9 @@
|
|
|
1
1
|
import { LeftJoin } from 'type-plus';
|
|
2
2
|
export declare class AsyncContext<Init extends Record<string | symbol, any>, Context extends Record<string | symbol, any> = Init> {
|
|
3
3
|
#private;
|
|
4
|
-
|
|
5
|
-
private transformers;
|
|
6
|
-
private resolving;
|
|
7
|
-
private resolvers;
|
|
8
|
-
constructor(init?: Init | Promise<Init> | AsyncContext.Initializer<Init> | undefined);
|
|
4
|
+
constructor(init?: Init | Promise<Init> | AsyncContext.Initializer<Init>);
|
|
9
5
|
initialize<I extends Init = Init>(init: I | Promise<I> | AsyncContext.Initializer<I>): AsyncContext<I>;
|
|
10
6
|
extend<CurrentContext extends Record<string | symbol, any> = Context, AdditionalContext extends Record<string | symbol, any> = Record<string | symbol, any>>(context: AdditionalContext | Promise<AdditionalContext> | AsyncContext.Transformer<CurrentContext, AdditionalContext>): AsyncContext<Init, LeftJoin<CurrentContext, AdditionalContext>>;
|
|
11
|
-
clone(): AsyncContext<Context, Context>;
|
|
12
7
|
get<C = Context>(): Promise<C>;
|
|
13
8
|
}
|
|
14
9
|
export declare namespace AsyncContext {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AsyncContext.d.ts","sourceRoot":"","sources":["../ts/AsyncContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA;AAGpC,qBAAa,YAAY,CACvB,IAAI,SAAS,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,GAAG,CAAC,EACzC,OAAO,SAAS,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,GAAG,CAAC,GAAG,IAAI;;
|
|
1
|
+
{"version":3,"file":"AsyncContext.d.ts","sourceRoot":"","sources":["../ts/AsyncContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA;AAGpC,qBAAa,YAAY,CACvB,IAAI,SAAS,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,GAAG,CAAC,EACzC,OAAO,SAAS,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,GAAG,CAAC,GAAG,IAAI;;gBAOvC,IAAI,CAAC,EAAE,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,GAAG,YAAY,CAAC,WAAW,CAAC,IAAI,CAAC;IAGxE,UAAU,CAAC,CAAC,SAAS,IAAI,GAAG,IAAI,EAAE,IAAI,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,GAAG,YAAY,CAAC,WAAW,CAAC,CAAC,CAAC,GAAG,YAAY,CAAC,CAAC,CAAC;IAOtG,MAAM,CACJ,cAAc,SAAS,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,GAAG,CAAC,GAAG,OAAO,EAC7D,iBAAiB,SAAS,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,GAAG,CAAC,EAErF,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAAC,iBAAiB,CAAC,GAAG,YAAY,CAAC,WAAW,CAAC,cAAc,EAAE,iBAAiB,CAAC,GACpH,YAAY,CAAC,IAAI,EAAE,QAAQ,CAAC,cAAc,EAAE,iBAAiB,CAAC,CAAC;IAiB5D,GAAG,CAAC,CAAC,GAAG,OAAO,KAAK,OAAO,CAAC,CAAC,CAAC;CAQrC;AACD,yBAAiB,YAAY,CAAC;IAC5B,KAAY,WAAW,CAAC,IAAI,IAAI,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAC1D,KAAY,WAAW,CACrB,cAAc,EACd,iBAAiB,IACb,CAAC,OAAO,EAAE,cAAc,KAAK,iBAAiB,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAA;CAClF"}
|
package/esm/AsyncContext.js
CHANGED
|
@@ -1,65 +1,61 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
1
7
|
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
2
8
|
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3
9
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
4
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
11
|
};
|
|
6
|
-
var _AsyncContext_instances, _AsyncContext_buildContext;
|
|
7
|
-
import { ContextAlreadyInitialized } from './errors';
|
|
12
|
+
var _AsyncContext_instances, _AsyncContext_resolving, _AsyncContext_resolvers, _AsyncContext_transforming, _AsyncContext_init, _AsyncContext_original, _AsyncContext_buildContext;
|
|
13
|
+
import { BlockingGetDetected, ContextAlreadyInitialized } from './errors';
|
|
8
14
|
export class AsyncContext {
|
|
9
15
|
constructor(init) {
|
|
10
16
|
_AsyncContext_instances.add(this);
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
Object.defineProperty(this, "transformers", {
|
|
18
|
-
enumerable: true,
|
|
19
|
-
configurable: true,
|
|
20
|
-
writable: true,
|
|
21
|
-
value: []
|
|
22
|
-
});
|
|
23
|
-
Object.defineProperty(this, "resolving", {
|
|
24
|
-
enumerable: true,
|
|
25
|
-
configurable: true,
|
|
26
|
-
writable: true,
|
|
27
|
-
value: void 0
|
|
28
|
-
});
|
|
29
|
-
Object.defineProperty(this, "resolvers", {
|
|
30
|
-
enumerable: true,
|
|
31
|
-
configurable: true,
|
|
32
|
-
writable: true,
|
|
33
|
-
value: []
|
|
34
|
-
});
|
|
17
|
+
_AsyncContext_resolving.set(this, void 0);
|
|
18
|
+
_AsyncContext_resolvers.set(this, []);
|
|
19
|
+
_AsyncContext_transforming.set(this, false);
|
|
20
|
+
_AsyncContext_init.set(this, void 0);
|
|
21
|
+
_AsyncContext_original.set(this, void 0);
|
|
22
|
+
__classPrivateFieldSet(this, _AsyncContext_init, init, "f");
|
|
35
23
|
}
|
|
36
24
|
initialize(init) {
|
|
37
|
-
if (this
|
|
38
|
-
throw new ContextAlreadyInitialized();
|
|
39
|
-
this
|
|
40
|
-
if (this.
|
|
41
|
-
this.
|
|
25
|
+
if (__classPrivateFieldGet(this, _AsyncContext_init, "f"))
|
|
26
|
+
throw new ContextAlreadyInitialized({ ssf: this.initialize });
|
|
27
|
+
__classPrivateFieldSet(this, _AsyncContext_init, init, "f");
|
|
28
|
+
if (__classPrivateFieldGet(this, _AsyncContext_resolvers, "f").length > 0)
|
|
29
|
+
__classPrivateFieldGet(this, _AsyncContext_resolvers, "f").forEach(r => r(__classPrivateFieldGet(this, _AsyncContext_instances, "m", _AsyncContext_buildContext).call(this)));
|
|
42
30
|
return this;
|
|
43
31
|
}
|
|
44
32
|
extend(context) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
33
|
+
const newctx = new AsyncContext(async () => {
|
|
34
|
+
try {
|
|
35
|
+
const value = await this.get();
|
|
36
|
+
__classPrivateFieldSet(newctx, _AsyncContext_transforming, true, "f");
|
|
37
|
+
const newValue = await (isTransformer(context) ? context(value) : context);
|
|
38
|
+
return { ...value, ...newValue };
|
|
39
|
+
}
|
|
40
|
+
finally {
|
|
41
|
+
__classPrivateFieldSet(newctx, _AsyncContext_transforming, false, "f");
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
const ctx = __classPrivateFieldGet(this, _AsyncContext_original, "f") ?? this;
|
|
45
|
+
__classPrivateFieldSet(newctx, _AsyncContext_original, ctx, "f");
|
|
46
|
+
newctx.initialize = newctx.initialize.bind(ctx);
|
|
47
|
+
return newctx;
|
|
51
48
|
}
|
|
52
49
|
async get() {
|
|
53
|
-
if (this
|
|
54
|
-
|
|
55
|
-
|
|
50
|
+
if (__classPrivateFieldGet(this, _AsyncContext_transforming, "f"))
|
|
51
|
+
throw new BlockingGetDetected({ ssf: this.get });
|
|
52
|
+
if (__classPrivateFieldGet(this, _AsyncContext_resolving, "f"))
|
|
53
|
+
return __classPrivateFieldGet(this, _AsyncContext_resolving, "f");
|
|
54
|
+
return __classPrivateFieldSet(this, _AsyncContext_resolving, __classPrivateFieldGet(this, _AsyncContext_init, "f") ? __classPrivateFieldGet(this, _AsyncContext_instances, "m", _AsyncContext_buildContext).call(this) : new Promise(a => __classPrivateFieldGet(this, _AsyncContext_resolvers, "f").push(a)), "f");
|
|
56
55
|
}
|
|
57
56
|
}
|
|
58
|
-
_AsyncContext_instances = new WeakSet(), _AsyncContext_buildContext = async function _AsyncContext_buildContext() {
|
|
59
|
-
return
|
|
60
|
-
const c = await p;
|
|
61
|
-
return { ...c, ...await t(c) };
|
|
62
|
-
}, resolveInit(this.init));
|
|
57
|
+
_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() {
|
|
58
|
+
return resolveInit(__classPrivateFieldGet(this, _AsyncContext_init, "f"));
|
|
63
59
|
};
|
|
64
60
|
async function resolveInit(init) {
|
|
65
61
|
return isInitializer(init) ? init() : init;
|
package/esm/AsyncContext.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AsyncContext.js","sourceRoot":"","sources":["../ts/AsyncContext.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"AsyncContext.js","sourceRoot":"","sources":["../ts/AsyncContext.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EAAE,mBAAmB,EAAE,yBAAyB,EAAE,MAAM,UAAU,CAAA;AAEzE,MAAM,OAAO,YAAY;IASvB,YAAY,IAA4D;;QALxE,0CAAoC;QACpC,kCAAyD,EAAE,EAAA;QAC3D,qCAAgB,KAAK,EAAA;QACrB,qCAAwE;QACxE,yCAA6C;QAE3C,uBAAA,IAAI,sBAAS,IAAI,MAAA,CAAA;IACnB,CAAC;IACD,UAAU,CAAwB,IAAkD;QAClF,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,IAAuB,CAAA;IAChC,CAAC;IACD,MAAM,CAIJ,OAAqH;QAErH,MAAM,MAAM,GAAG,IAAI,YAAY,CAAC,KAAK,IAAI,EAAE;YACzC,IAAI;gBACF,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,GAAG,EAAE,CAAA;gBAC9B,uBAAA,MAAM,8BAAiB,IAAI,MAAA,CAAA;gBAC3B,MAAM,QAAQ,GAAG,MAAM,CAAC,aAAa,CAAW,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAA;gBACpF,OAAO,EAAE,GAAG,KAAK,EAAE,GAAG,QAAQ,EAAE,CAAA;aACjC;oBACO;gBACN,uBAAA,MAAM,8BAAiB,KAAK,MAAA,CAAA;aAC7B;QACH,CAAC,CAAC,CAAA;QACF,MAAM,GAAG,GAAG,uBAAA,IAAI,8BAAU,IAAI,IAAI,CAAA;QAClC,uBAAA,MAAM,0BAAa,GAAG,MAAA,CAAA;QACtB,MAAM,CAAC,UAAU,GAAG,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;QAC/C,OAAO,MAAM,CAAA;IACf,CAAC;IACD,KAAK,CAAC,GAAG;QACP,IAAI,uBAAA,IAAI,kCAAc;YAAE,MAAM,IAAI,mBAAmB,CAAC,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,CAAC,CAAA;QACxE,IAAI,uBAAA,IAAI,+BAAW;YAAE,OAAO,uBAAA,IAAI,+BAAW,CAAA;QAC3C,OAAO,uBAAA,IAAI,2BAAc,uBAAA,IAAI,0BAAM,CAAC,CAAC,CAAC,uBAAA,IAAI,2DAAc,MAAlB,IAAI,CAAgB,CAAC,CAAC,CAAC,IAAI,OAAO,CAAM,CAAC,CAAC,EAAE,CAAC,uBAAA,IAAI,+BAAW,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAA,CAAA;IAC7G,CAAC;CAIF;gRAHC,KAAK;IACH,OAAO,WAAW,CAAC,uBAAA,IAAI,0BAAO,CAAqB,CAAA;AACrD,CAAC;AAUH,KAAK,UAAU,WAAW,CAEtB,IAA2D;IAC7D,OAAO,aAAa,CAAO,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,CAAA;AAClD,CAAC;AAED,SAAS,aAAa,CAGpB,OAAgB;IAChB,OAAO,OAAO,OAAO,KAAK,UAAU,CAAA;AACtC,CAAC;AAED,SAAS,aAAa,CAAI,OAAgB;IACxC,OAAO,OAAO,OAAO,KAAK,UAAU,CAAA;AACtC,CAAC","sourcesContent":["import { LeftJoin } from 'type-plus'\nimport { BlockingGetDetected, ContextAlreadyInitialized } from './errors'\n\nexport class AsyncContext<\n Init extends Record<string | symbol, any>,\n Context extends Record<string | symbol, any> = Init\n > {\n #resolving: Promise<any> | undefined\n #resolvers: Array<((value: Promise<Context>) => void)> = []\n #transforming = false\n #init: Init | Promise<Init> | AsyncContext.Initializer<Init> | undefined\n #original: AsyncContext<any, any> | undefined\n constructor(init?: Init | Promise<Init> | AsyncContext.Initializer<Init>) {\n this.#init = init\n }\n initialize<I extends Init = Init>(init: I | Promise<I> | AsyncContext.Initializer<I>): AsyncContext<I> {\n if (this.#init) throw new ContextAlreadyInitialized({ ssf: this.initialize })\n this.#init = init\n\n if (this.#resolvers.length > 0) this.#resolvers.forEach(r => r(this.#buildContext()))\n return this as AsyncContext<I>\n }\n extend<\n CurrentContext extends Record<string | symbol, any> = Context,\n AdditionalContext extends Record<string | symbol, any> = Record<string | symbol, any>\n >(\n context: AdditionalContext | Promise<AdditionalContext> | AsyncContext.Transformer<CurrentContext, AdditionalContext>\n ): AsyncContext<Init, LeftJoin<CurrentContext, AdditionalContext>> {\n const newctx = new AsyncContext(async () => {\n try {\n const value = await this.get()\n newctx.#transforming = true\n const newValue = await (isTransformer<any, any>(context) ? context(value) : context)\n return { ...value, ...newValue }\n }\n finally {\n newctx.#transforming = false\n }\n })\n const ctx = this.#original ?? this\n newctx.#original = ctx\n newctx.initialize = newctx.initialize.bind(ctx)\n return newctx\n }\n async get<C = Context>(): Promise<C> {\n if (this.#transforming) throw new BlockingGetDetected({ ssf: this.get })\n if (this.#resolving) return this.#resolving\n return this.#resolving = this.#init ? this.#buildContext() : new Promise<any>(a => this.#resolvers.push(a))\n }\n async #buildContext(): Promise<Context> {\n return resolveInit(this.#init!) as Promise<Context>\n }\n}\nexport namespace AsyncContext {\n export type Initializer<Init> = () => Init | Promise<Init>\n export type Transformer<\n CurrentContext,\n AdditionalContext\n > = (context: CurrentContext) => AdditionalContext | Promise<AdditionalContext>\n}\n\nasync function resolveInit<\n Init extends Record<string | symbol, any>,\n >(init: Init | Promise<Init> | AsyncContext.Initializer<Init>): Promise<Init> {\n return isInitializer<Init>(init) ? init() : init\n}\n\nfunction isTransformer<\n CurrentContext,\n AdditionalContext\n>(context: unknown): context is AsyncContext.Transformer<CurrentContext, AdditionalContext> {\n return typeof context === 'function'\n}\n\nfunction isInitializer<T>(context: unknown): context is AsyncContext.Initializer<T> {\n return typeof context === 'function'\n}\n"]}
|
package/esm/errors.d.ts
CHANGED
|
@@ -2,4 +2,7 @@ import { IsoError, ModuleError } from 'iso-error';
|
|
|
2
2
|
export declare class ContextAlreadyInitialized extends ModuleError {
|
|
3
3
|
constructor(options?: IsoError.Options);
|
|
4
4
|
}
|
|
5
|
+
export declare class BlockingGetDetected extends ModuleError {
|
|
6
|
+
constructor(options?: IsoError.Options);
|
|
7
|
+
}
|
|
5
8
|
//# sourceMappingURL=errors.d.ts.map
|
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,QAAQ,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AAEjD,qBAAa,yBAA0B,SAAQ,WAAW;gBAC5C,OAAO,CAAC,EAAE,QAAQ,CAAC,OAAO;CAGvC"}
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../ts/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AAEjD,qBAAa,yBAA0B,SAAQ,WAAW;gBAC5C,OAAO,CAAC,EAAE,QAAQ,CAAC,OAAO;CAGvC;AAED,qBAAa,mBAAoB,SAAQ,WAAW;gBACtC,OAAO,CAAC,EAAE,QAAQ,CAAC,OAAO;CAGvC"}
|
package/esm/errors.js
CHANGED
|
@@ -4,4 +4,9 @@ export class ContextAlreadyInitialized extends ModuleError {
|
|
|
4
4
|
super('@unional/async-context', 'context can only be initialized once.', options);
|
|
5
5
|
}
|
|
6
6
|
}
|
|
7
|
+
export class BlockingGetDetected extends ModuleError {
|
|
8
|
+
constructor(options) {
|
|
9
|
+
super('@unional/async-context', `a blocking get() detected. Are you calling it within extend()?`, options);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
7
12
|
//# sourceMappingURL=errors.js.map
|
package/esm/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../ts/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,WAAW,EAAE,MAAM,WAAW,CAAA;AAEjD,MAAM,OAAO,yBAA0B,SAAQ,WAAW;IACxD,YAAY,OAA0B;QACpC,KAAK,CAAC,wBAAwB,EAAE,uCAAuC,EAAE,OAAO,CAAC,CAAA;IACnF,CAAC;CACF","sourcesContent":["import { IsoError, ModuleError } from 'iso-error'\n\nexport class ContextAlreadyInitialized extends ModuleError {\n constructor(options?: IsoError.Options) {\n super('@unional/async-context', 'context can only be initialized once.', options)\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../ts/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,WAAW,EAAE,MAAM,WAAW,CAAA;AAEjD,MAAM,OAAO,yBAA0B,SAAQ,WAAW;IACxD,YAAY,OAA0B;QACpC,KAAK,CAAC,wBAAwB,EAAE,uCAAuC,EAAE,OAAO,CAAC,CAAA;IACnF,CAAC;CACF;AAED,MAAM,OAAO,mBAAoB,SAAQ,WAAW;IAClD,YAAY,OAA0B;QACpC,KAAK,CAAC,wBAAwB,EAAE,gEAAgE,EAAE,OAAO,CAAC,CAAA;IAC5G,CAAC;CACF","sourcesContent":["import { IsoError, ModuleError } from 'iso-error'\n\nexport class ContextAlreadyInitialized extends ModuleError {\n constructor(options?: IsoError.Options) {\n super('@unional/async-context', 'context can only be initialized once.', options)\n }\n}\n\nexport class BlockingGetDetected extends ModuleError {\n constructor(options?: IsoError.Options) {\n super('@unional/async-context', `a blocking get() detected. Are you calling it within extend()?`, options)\n }\n}\n"]}
|
package/package.json
CHANGED
package/ts/AsyncContext.ts
CHANGED
|
@@ -1,19 +1,23 @@
|
|
|
1
1
|
import { LeftJoin } from 'type-plus'
|
|
2
|
-
import { ContextAlreadyInitialized } from './errors'
|
|
2
|
+
import { BlockingGetDetected, ContextAlreadyInitialized } from './errors'
|
|
3
3
|
|
|
4
4
|
export class AsyncContext<
|
|
5
5
|
Init extends Record<string | symbol, any>,
|
|
6
6
|
Context extends Record<string | symbol, any> = Init
|
|
7
7
|
> {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
+
}
|
|
12
16
|
initialize<I extends Init = Init>(init: I | Promise<I> | AsyncContext.Initializer<I>): AsyncContext<I> {
|
|
13
|
-
if (this
|
|
14
|
-
this
|
|
17
|
+
if (this.#init) throw new ContextAlreadyInitialized({ ssf: this.initialize })
|
|
18
|
+
this.#init = init
|
|
15
19
|
|
|
16
|
-
if (this
|
|
20
|
+
if (this.#resolvers.length > 0) this.#resolvers.forEach(r => r(this.#buildContext()))
|
|
17
21
|
return this as AsyncContext<I>
|
|
18
22
|
}
|
|
19
23
|
extend<
|
|
@@ -22,22 +26,29 @@ export class AsyncContext<
|
|
|
22
26
|
>(
|
|
23
27
|
context: AdditionalContext | Promise<AdditionalContext> | AsyncContext.Transformer<CurrentContext, AdditionalContext>
|
|
24
28
|
): AsyncContext<Init, LeftJoin<CurrentContext, AdditionalContext>> {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
const newctx = new AsyncContext(async () => {
|
|
30
|
+
try {
|
|
31
|
+
const value = await this.get()
|
|
32
|
+
newctx.#transforming = true
|
|
33
|
+
const newValue = await (isTransformer<any, any>(context) ? context(value) : context)
|
|
34
|
+
return { ...value, ...newValue }
|
|
35
|
+
}
|
|
36
|
+
finally {
|
|
37
|
+
newctx.#transforming = false
|
|
38
|
+
}
|
|
39
|
+
})
|
|
40
|
+
const ctx = this.#original ?? this
|
|
41
|
+
newctx.#original = ctx
|
|
42
|
+
newctx.initialize = newctx.initialize.bind(ctx)
|
|
43
|
+
return newctx
|
|
31
44
|
}
|
|
32
45
|
async get<C = Context>(): Promise<C> {
|
|
33
|
-
if (this
|
|
34
|
-
|
|
46
|
+
if (this.#transforming) throw new BlockingGetDetected({ ssf: this.get })
|
|
47
|
+
if (this.#resolving) return this.#resolving
|
|
48
|
+
return this.#resolving = this.#init ? this.#buildContext() : new Promise<any>(a => this.#resolvers.push(a))
|
|
35
49
|
}
|
|
36
50
|
async #buildContext(): Promise<Context> {
|
|
37
|
-
return this
|
|
38
|
-
const c = await p
|
|
39
|
-
return { ...c, ...await t(c) as any }
|
|
40
|
-
}, resolveInit(this.init!)) as Promise<Context>
|
|
51
|
+
return resolveInit(this.#init!) as Promise<Context>
|
|
41
52
|
}
|
|
42
53
|
}
|
|
43
54
|
export namespace AsyncContext {
|
package/ts/errors.ts
CHANGED
|
@@ -5,3 +5,9 @@ export class ContextAlreadyInitialized extends ModuleError {
|
|
|
5
5
|
super('@unional/async-context', 'context can only be initialized once.', options)
|
|
6
6
|
}
|
|
7
7
|
}
|
|
8
|
+
|
|
9
|
+
export class BlockingGetDetected extends ModuleError {
|
|
10
|
+
constructor(options?: IsoError.Options) {
|
|
11
|
+
super('@unional/async-context', `a blocking get() detected. Are you calling it within extend()?`, options)
|
|
12
|
+
}
|
|
13
|
+
}
|