@unional/async-context 3.2.0 → 8.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.
@@ -4,7 +4,6 @@ export declare class AsyncContext<Init extends Record<string | symbol, any>, Con
4
4
  constructor(init?: Init | Promise<Init> | AsyncContext.Initializer<Init>);
5
5
  initialize<I extends Init = Init>(init: I | Promise<I> | AsyncContext.Initializer<I>): AsyncContext<I>;
6
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>>;
7
- clone(): AsyncContext<Context, Context>;
8
7
  get<C = Context>(): Promise<C>;
9
8
  }
10
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;;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,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,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,IACf,CAAC,OAAO,EAAE,cAAc,KAAK,iBAAiB,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAA;CAChF"}
@@ -10,35 +10,45 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
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");
11
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
12
  };
13
- var _AsyncContext_instances, _AsyncContext_transformers, _AsyncContext_resolving, _AsyncContext_resolvers, _AsyncContext_transforming, _AsyncContext_init, _AsyncContext_buildContext;
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
16
  const errors_1 = require("./errors");
17
17
  class AsyncContext {
18
18
  constructor(init) {
19
19
  _AsyncContext_instances.add(this);
20
- _AsyncContext_transformers.set(this, []);
21
20
  _AsyncContext_resolving.set(this, void 0);
22
21
  _AsyncContext_resolvers.set(this, []);
23
22
  _AsyncContext_transforming.set(this, false);
24
23
  _AsyncContext_init.set(this, void 0);
24
+ _AsyncContext_original.set(this, void 0);
25
25
  __classPrivateFieldSet(this, _AsyncContext_init, init, "f");
26
26
  }
27
27
  initialize(init) {
28
28
  if (__classPrivateFieldGet(this, _AsyncContext_init, "f"))
29
- throw new errors_1.ContextAlreadyInitialized();
29
+ throw new errors_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)));
33
33
  return this;
34
34
  }
35
35
  extend(context) {
36
- __classPrivateFieldGet(this, _AsyncContext_transformers, "f").push(isTransformer(context) ? context : () => context);
37
- __classPrivateFieldSet(this, _AsyncContext_resolving, undefined, "f");
38
- return this;
39
- }
40
- clone() {
41
- return new AsyncContext(() => this.get());
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;
42
52
  }
43
53
  async get() {
44
54
  if (__classPrivateFieldGet(this, _AsyncContext_transforming, "f"))
@@ -49,17 +59,8 @@ class AsyncContext {
49
59
  }
50
60
  }
51
61
  exports.AsyncContext = AsyncContext;
52
- _AsyncContext_transformers = new WeakMap(), _AsyncContext_resolving = new WeakMap(), _AsyncContext_resolvers = new WeakMap(), _AsyncContext_transforming = new WeakMap(), _AsyncContext_init = new WeakMap(), _AsyncContext_instances = new WeakSet(), _AsyncContext_buildContext = async function _AsyncContext_buildContext() {
53
- return __classPrivateFieldGet(this, _AsyncContext_transformers, "f").reduce(async (p, t) => {
54
- try {
55
- __classPrivateFieldSet(this, _AsyncContext_transforming, true, "f");
56
- const c = await p;
57
- return { ...c, ...await t(c) };
58
- }
59
- finally {
60
- __classPrivateFieldSet(this, _AsyncContext_transforming, false, "f");
61
- }
62
- }, resolveInit(__classPrivateFieldGet(this, _AsyncContext_init, "f")));
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"));
63
64
  };
64
65
  async function resolveInit(init) {
65
66
  return isInitializer(init) ? init() : init;
@@ -1 +1 @@
1
- {"version":3,"file":"AsyncContext.js","sourceRoot":"","sources":["../ts/AsyncContext.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,qCAAyE;AAEzE,MAAa,YAAY;IASvB,YAAY,IAA4D;;QALxE,qCAA2D,EAAE,EAAA;QAC7D,0CAAoC;QACpC,kCAAyD,EAAE,EAAA;QAC3D,qCAAgB,KAAK,EAAA;QACrB,qCAAwE;QAEtE,uBAAA,IAAI,sBAAS,IAAI,MAAA,CAAA;IACnB,CAAC;IACD,UAAU,CAAwB,IAAkD;QAClF,IAAI,uBAAA,IAAI,0BAAM;YAAE,MAAM,IAAI,kCAAyB,EAAE,CAAA;Q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;QAC9C,IAAI;YACF,uBAAA,IAAI,8BAAiB,IAAI,MAAA,CAAA;YACzB,MAAM,CAAC,GAAG,MAAM,CAAC,CAAA;YACjB,OAAO,EAAE,GAAG,CAAC,EAAE,GAAG,MAAM,CAAC,CAAC,CAAC,CAAQ,EAAE,CAAA;SACtC;gBACO;YACN,uBAAA,IAAI,8BAAiB,KAAK,MAAA,CAAA;SAC3B;IACH,CAAC,EAAE,WAAW,CAAC,uBAAA,IAAI,0BAAO,CAAC,CAAqB,CAAA;AAClD,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 #transformers: Array<AsyncContext.Transformer<any, any>> = []\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 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()\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 this.#transformers.push(isTransformer(context) ? context : () => context)\n this.#resolving = undefined\n return this as AsyncContext<Init, LeftJoin<CurrentContext, AdditionalContext>>\n }\n clone() {\n return new AsyncContext(() => this.get())\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 this.#transformers.reduce(async (p, t) => {\n try {\n this.#transforming = true\n const c = await p\n return { ...c, ...await t(c) as any }\n }\n finally {\n this.#transforming = false\n }\n }, 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"]}
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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AI,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,CAA4B,CAAA;AAC5D,CAAC;AAUH,KAAK,UAAU,WAAW,CAExB,IAA2D;IAC3D,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 any 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 any 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
@@ -1,8 +1,8 @@
1
- import { IsoError, ModuleError } from 'iso-error';
1
+ import { ModuleError } from 'iso-error';
2
2
  export declare class ContextAlreadyInitialized extends ModuleError {
3
- constructor(options?: IsoError.Options);
3
+ constructor(options?: ModuleError.Options);
4
4
  }
5
5
  export declare class BlockingGetDetected extends ModuleError {
6
- constructor(options?: IsoError.Options);
6
+ constructor(options?: ModuleError.Options);
7
7
  }
8
8
  //# sourceMappingURL=errors.d.ts.map
@@ -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;AAED,qBAAa,mBAAoB,SAAQ,WAAW;gBACtC,OAAO,CAAC,EAAE,QAAQ,CAAC,OAAO;CAGvC"}
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;gBAC5C,OAAO,CAAC,EAAE,WAAW,CAAC,OAAO;CAG1C;AAED,qBAAa,mBAAoB,SAAQ,WAAW;gBACtC,OAAO,CAAC,EAAE,WAAW,CAAC,OAAO;CAG1C"}
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;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"]}
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../ts/errors.ts"],"names":[],"mappings":";;;AAAA,yCAAuC;AAEvC,MAAa,yBAA0B,SAAQ,uBAAW;IACxD,YAAY,OAA6B;QACvC,KAAK,CAAC,wBAAwB,EAAE,uCAAuC,EAAE,OAAO,CAAC,CAAA;IACnF,CAAC;CACF;AAJD,8DAIC;AAED,MAAa,mBAAoB,SAAQ,uBAAW;IAClD,YAAY,OAA6B;QACvC,KAAK,CAAC,wBAAwB,EAAE,gEAAgE,EAAE,OAAO,CAAC,CAAA;IAC5G,CAAC;CACF;AAJD,kDAIC","sourcesContent":["import { ModuleError } from 'iso-error'\n\nexport class ContextAlreadyInitialized extends ModuleError {\n constructor(options?: ModuleError.Options) {\n super('@unional/async-context', 'context can only be initialized once.', options)\n }\n}\n\nexport class BlockingGetDetected extends ModuleError {\n constructor(options?: ModuleError.Options) {\n super('@unional/async-context', `a blocking get() detected. Are you calling it within extend()?`, options)\n }\n}\n"]}
@@ -4,7 +4,6 @@ export declare class AsyncContext<Init extends Record<string | symbol, any>, Con
4
4
  constructor(init?: Init | Promise<Init> | AsyncContext.Initializer<Init>);
5
5
  initialize<I extends Init = Init>(init: I | Promise<I> | AsyncContext.Initializer<I>): AsyncContext<I>;
6
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>>;
7
- clone(): AsyncContext<Context, Context>;
8
7
  get<C = Context>(): Promise<C>;
9
8
  }
10
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;;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;IAKlE,KAAK;IAGC,GAAG,CAAC,CAAC,GAAG,OAAO,KAAK,OAAO,CAAC,CAAC,CAAC;CAiBrC;AACD,yBAAiB,YAAY,CAAC;IAC5B,KAAY,WAAW,CAAC,IAAI,IAAI,MAAM,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;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,IACf,CAAC,OAAO,EAAE,cAAc,KAAK,iBAAiB,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAA;CAChF"}
@@ -9,33 +9,42 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
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");
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _AsyncContext_instances, _AsyncContext_transformers, _AsyncContext_resolving, _AsyncContext_resolvers, _AsyncContext_transforming, _AsyncContext_init, _AsyncContext_buildContext;
12
+ var _AsyncContext_instances, _AsyncContext_resolving, _AsyncContext_resolvers, _AsyncContext_transforming, _AsyncContext_init, _AsyncContext_original, _AsyncContext_buildContext;
13
13
  import { BlockingGetDetected, ContextAlreadyInitialized } from './errors';
14
14
  export class AsyncContext {
15
15
  constructor(init) {
16
16
  _AsyncContext_instances.add(this);
17
- _AsyncContext_transformers.set(this, []);
18
17
  _AsyncContext_resolving.set(this, void 0);
19
18
  _AsyncContext_resolvers.set(this, []);
20
19
  _AsyncContext_transforming.set(this, false);
21
20
  _AsyncContext_init.set(this, void 0);
21
+ _AsyncContext_original.set(this, void 0);
22
22
  __classPrivateFieldSet(this, _AsyncContext_init, init, "f");
23
23
  }
24
24
  initialize(init) {
25
25
  if (__classPrivateFieldGet(this, _AsyncContext_init, "f"))
26
- throw new ContextAlreadyInitialized();
26
+ throw new ContextAlreadyInitialized({ ssf: this.initialize });
27
27
  __classPrivateFieldSet(this, _AsyncContext_init, init, "f");
28
28
  if (__classPrivateFieldGet(this, _AsyncContext_resolvers, "f").length > 0)
29
29
  __classPrivateFieldGet(this, _AsyncContext_resolvers, "f").forEach(r => r(__classPrivateFieldGet(this, _AsyncContext_instances, "m", _AsyncContext_buildContext).call(this)));
30
30
  return this;
31
31
  }
32
32
  extend(context) {
33
- __classPrivateFieldGet(this, _AsyncContext_transformers, "f").push(isTransformer(context) ? context : () => context);
34
- __classPrivateFieldSet(this, _AsyncContext_resolving, undefined, "f");
35
- return this;
36
- }
37
- clone() {
38
- return new AsyncContext(() => this.get());
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;
39
48
  }
40
49
  async get() {
41
50
  if (__classPrivateFieldGet(this, _AsyncContext_transforming, "f"))
@@ -45,17 +54,8 @@ export class AsyncContext {
45
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");
46
55
  }
47
56
  }
48
- _AsyncContext_transformers = new WeakMap(), _AsyncContext_resolving = new WeakMap(), _AsyncContext_resolvers = new WeakMap(), _AsyncContext_transforming = new WeakMap(), _AsyncContext_init = new WeakMap(), _AsyncContext_instances = new WeakSet(), _AsyncContext_buildContext = async function _AsyncContext_buildContext() {
49
- return __classPrivateFieldGet(this, _AsyncContext_transformers, "f").reduce(async (p, t) => {
50
- try {
51
- __classPrivateFieldSet(this, _AsyncContext_transforming, true, "f");
52
- const c = await p;
53
- return { ...c, ...await t(c) };
54
- }
55
- finally {
56
- __classPrivateFieldSet(this, _AsyncContext_transforming, false, "f");
57
- }
58
- }, resolveInit(__classPrivateFieldGet(this, _AsyncContext_init, "f")));
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"));
59
59
  };
60
60
  async function resolveInit(init) {
61
61
  return isInitializer(init) ? init() : init;
@@ -1 +1 @@
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,qCAA2D,EAAE,EAAA;QAC7D,0CAAoC;QACpC,kCAAyD,EAAE,EAAA;QAC3D,qCAAgB,KAAK,EAAA;QACrB,qCAAwE;QAEtE,uBAAA,IAAI,sBAAS,IAAI,MAAA,CAAA;IACnB,CAAC;IACD,UAAU,CAAwB,IAAkD;QAClF,IAAI,uBAAA,IAAI,0BAAM;YAAE,MAAM,IAAI,yBAAyB,EAAE,CAAA;QACrD,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,uBAAA,IAAI,kCAAc,CAAC,I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W,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 #transformers: Array<AsyncContext.Transformer<any, any>> = []\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 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()\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 this.#transformers.push(isTransformer(context) ? context : () => context)\n this.#resolving = undefined\n return this as AsyncContext<Init, LeftJoin<CurrentContext, AdditionalContext>>\n }\n clone() {\n return new AsyncContext(() => this.get())\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 this.#transformers.reduce(async (p, t) => {\n try {\n this.#transforming = true\n const c = await p\n return { ...c, ...await t(c) as any }\n }\n finally {\n this.#transforming = false\n }\n }, 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"]}
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,+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AO,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,CAA4B,CAAA;AAC5D,CAAC;AAUH,KAAK,UAAU,WAAW,CAExB,IAA2D;IAC3D,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 any 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 any 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
@@ -1,8 +1,8 @@
1
- import { IsoError, ModuleError } from 'iso-error';
1
+ import { ModuleError } from 'iso-error';
2
2
  export declare class ContextAlreadyInitialized extends ModuleError {
3
- constructor(options?: IsoError.Options);
3
+ constructor(options?: ModuleError.Options);
4
4
  }
5
5
  export declare class BlockingGetDetected extends ModuleError {
6
- constructor(options?: IsoError.Options);
6
+ constructor(options?: ModuleError.Options);
7
7
  }
8
8
  //# sourceMappingURL=errors.d.ts.map
@@ -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;AAED,qBAAa,mBAAoB,SAAQ,WAAW;gBACtC,OAAO,CAAC,EAAE,QAAQ,CAAC,OAAO;CAGvC"}
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;gBAC5C,OAAO,CAAC,EAAE,WAAW,CAAC,OAAO;CAG1C;AAED,qBAAa,mBAAoB,SAAQ,WAAW;gBACtC,OAAO,CAAC,EAAE,WAAW,CAAC,OAAO;CAG1C"}
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;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"]}
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;IACxD,YAAY,OAA6B;QACvC,KAAK,CAAC,wBAAwB,EAAE,uCAAuC,EAAE,OAAO,CAAC,CAAA;IACnF,CAAC;CACF;AAED,MAAM,OAAO,mBAAoB,SAAQ,WAAW;IAClD,YAAY,OAA6B;QACvC,KAAK,CAAC,wBAAwB,EAAE,gEAAgE,EAAE,OAAO,CAAC,CAAA;IAC5G,CAAC;CACF","sourcesContent":["import { ModuleError } from 'iso-error'\n\nexport class ContextAlreadyInitialized extends ModuleError {\n constructor(options?: ModuleError.Options) {\n super('@unional/async-context', 'context can only be initialized once.', options)\n }\n}\n\nexport class BlockingGetDetected extends ModuleError {\n constructor(options?: ModuleError.Options) {\n super('@unional/async-context', `a blocking get() detected. Are you calling it within extend()?`, options)\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unional/async-context",
3
- "version": "3.2.0",
3
+ "version": "8.0.1",
4
4
  "description": "Asynchronous context for functional style programming",
5
5
  "keywords": [
6
6
  "async",
@@ -34,8 +34,8 @@
34
34
  "!**/*.spec.*"
35
35
  ],
36
36
  "dependencies": {
37
- "iso-error": "^4.3.6",
38
- "type-plus": "^4.11.1"
37
+ "iso-error": "^4.4.0",
38
+ "type-plus": "^4.14.1"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@size-limit/preset-small-lib": "^8.0.1",
@@ -73,7 +73,7 @@
73
73
  "build": "run-p build:cjs build:esm",
74
74
  "build:cjs": "tsc -p tsconfig.cjs.json",
75
75
  "build:esm": "tsc",
76
- "clean": "rimraf cjs esm .tsbuildinfo",
76
+ "clean": "rimraf cjs esm coverage *.tsbuildinfo",
77
77
  "coverage": "jest --coverage",
78
78
  "depcheck": "depcheck",
79
79
  "lint": "cross-env TIMING=1 eslint --ext=ts,js,cjs,mjs .",
@@ -4,21 +4,21 @@ import { BlockingGetDetected, ContextAlreadyInitialized } from './errors'
4
4
  export class AsyncContext<
5
5
  Init extends Record<string | symbol, any>,
6
6
  Context extends Record<string | symbol, any> = Init
7
- > {
8
- #transformers: Array<AsyncContext.Transformer<any, any>> = []
7
+ > {
9
8
  #resolving: Promise<any> | undefined
10
9
  #resolvers: Array<((value: Promise<Context>) => void)> = []
11
10
  #transforming = false
12
11
  #init: Init | Promise<Init> | AsyncContext.Initializer<Init> | undefined
12
+ #original: AsyncContext<any, any> | undefined
13
13
  constructor(init?: Init | Promise<Init> | AsyncContext.Initializer<Init>) {
14
14
  this.#init = init
15
15
  }
16
16
  initialize<I extends Init = Init>(init: I | Promise<I> | AsyncContext.Initializer<I>): AsyncContext<I> {
17
- if (this.#init) throw new ContextAlreadyInitialized()
17
+ if (this.#init) throw new ContextAlreadyInitialized({ ssf: this.initialize })
18
18
  this.#init = init
19
19
 
20
20
  if (this.#resolvers.length > 0) this.#resolvers.forEach(r => r(this.#buildContext()))
21
- return this as AsyncContext<I>
21
+ return this as any as AsyncContext<I>
22
22
  }
23
23
  extend<
24
24
  CurrentContext extends Record<string | symbol, any> = Context,
@@ -26,12 +26,21 @@ export class AsyncContext<
26
26
  >(
27
27
  context: AdditionalContext | Promise<AdditionalContext> | AsyncContext.Transformer<CurrentContext, AdditionalContext>
28
28
  ): AsyncContext<Init, LeftJoin<CurrentContext, AdditionalContext>> {
29
- this.#transformers.push(isTransformer(context) ? context : () => context)
30
- this.#resolving = undefined
31
- return this as AsyncContext<Init, LeftJoin<CurrentContext, AdditionalContext>>
32
- }
33
- clone() {
34
- return new AsyncContext(() => this.get())
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
35
44
  }
36
45
  async get<C = Context>(): Promise<C> {
37
46
  if (this.#transforming) throw new BlockingGetDetected({ ssf: this.get })
@@ -39,16 +48,7 @@ export class AsyncContext<
39
48
  return this.#resolving = this.#init ? this.#buildContext() : new Promise<any>(a => this.#resolvers.push(a))
40
49
  }
41
50
  async #buildContext(): Promise<Context> {
42
- return this.#transformers.reduce(async (p, t) => {
43
- try {
44
- this.#transforming = true
45
- const c = await p
46
- return { ...c, ...await t(c) as any }
47
- }
48
- finally {
49
- this.#transforming = false
50
- }
51
- }, resolveInit(this.#init!)) as Promise<Context>
51
+ return resolveInit(this.#init!) as any as Promise<Context>
52
52
  }
53
53
  }
54
54
  export namespace AsyncContext {
@@ -56,12 +56,12 @@ export namespace AsyncContext {
56
56
  export type Transformer<
57
57
  CurrentContext,
58
58
  AdditionalContext
59
- > = (context: CurrentContext) => AdditionalContext | Promise<AdditionalContext>
59
+ > = (context: CurrentContext) => AdditionalContext | Promise<AdditionalContext>
60
60
  }
61
61
 
62
62
  async function resolveInit<
63
63
  Init extends Record<string | symbol, any>,
64
- >(init: Init | Promise<Init> | AsyncContext.Initializer<Init>): Promise<Init> {
64
+ >(init: Init | Promise<Init> | AsyncContext.Initializer<Init>): Promise<Init> {
65
65
  return isInitializer<Init>(init) ? init() : init
66
66
  }
67
67
 
package/ts/errors.ts CHANGED
@@ -1,13 +1,13 @@
1
- import { IsoError, ModuleError } from 'iso-error'
1
+ import { ModuleError } from 'iso-error'
2
2
 
3
3
  export class ContextAlreadyInitialized extends ModuleError {
4
- constructor(options?: IsoError.Options) {
4
+ constructor(options?: ModuleError.Options) {
5
5
  super('@unional/async-context', 'context can only be initialized once.', options)
6
6
  }
7
7
  }
8
8
 
9
9
  export class BlockingGetDetected extends ModuleError {
10
- constructor(options?: IsoError.Options) {
10
+ constructor(options?: ModuleError.Options) {
11
11
  super('@unional/async-context', `a blocking get() detected. Are you calling it within extend()?`, options)
12
12
  }
13
13
  }