@unional/async-context 8.0.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.
- package/cjs/AsyncContext.d.ts.map +1 -1
- package/cjs/AsyncContext.js.map +1 -1
- package/cjs/errors.d.ts +3 -3
- package/cjs/errors.d.ts.map +1 -1
- package/cjs/errors.js.map +1 -1
- package/esm/AsyncContext.d.ts.map +1 -1
- package/esm/AsyncContext.js.map +1 -1
- package/esm/errors.d.ts +3 -3
- package/esm/errors.d.ts.map +1 -1
- package/esm/errors.js.map +1 -1
- package/package.json +4 -4
- package/ts/AsyncContext.ts +5 -5
- package/ts/errors.ts +3 -3
|
@@ -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,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export 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 {
|
|
1
|
+
import { ModuleError } from 'iso-error';
|
|
2
2
|
export declare class ContextAlreadyInitialized extends ModuleError {
|
|
3
|
-
constructor(options?:
|
|
3
|
+
constructor(options?: ModuleError.Options);
|
|
4
4
|
}
|
|
5
5
|
export declare class BlockingGetDetected extends ModuleError {
|
|
6
|
-
constructor(options?:
|
|
6
|
+
constructor(options?: ModuleError.Options);
|
|
7
7
|
}
|
|
8
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,
|
|
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,
|
|
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"]}
|
|
@@ -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,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AO,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 {
|
|
1
|
+
import { ModuleError } from 'iso-error';
|
|
2
2
|
export declare class ContextAlreadyInitialized extends ModuleError {
|
|
3
|
-
constructor(options?:
|
|
3
|
+
constructor(options?: ModuleError.Options);
|
|
4
4
|
}
|
|
5
5
|
export declare class BlockingGetDetected extends ModuleError {
|
|
6
|
-
constructor(options?:
|
|
6
|
+
constructor(options?: ModuleError.Options);
|
|
7
7
|
}
|
|
8
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,
|
|
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,
|
|
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": "8.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.
|
|
38
|
-
"type-plus": "^4.
|
|
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
|
|
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 .",
|
package/ts/AsyncContext.ts
CHANGED
|
@@ -4,7 +4,7 @@ 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
|
-
|
|
7
|
+
> {
|
|
8
8
|
#resolving: Promise<any> | undefined
|
|
9
9
|
#resolvers: Array<((value: Promise<Context>) => void)> = []
|
|
10
10
|
#transforming = false
|
|
@@ -18,7 +18,7 @@ export class AsyncContext<
|
|
|
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,
|
|
@@ -48,7 +48,7 @@ export class AsyncContext<
|
|
|
48
48
|
return this.#resolving = this.#init ? this.#buildContext() : new Promise<any>(a => this.#resolvers.push(a))
|
|
49
49
|
}
|
|
50
50
|
async #buildContext(): Promise<Context> {
|
|
51
|
-
return 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
|
-
|
|
59
|
+
> = (context: CurrentContext) => AdditionalContext | Promise<AdditionalContext>
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
async function resolveInit<
|
|
63
63
|
Init extends Record<string | symbol, any>,
|
|
64
|
-
|
|
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 {
|
|
1
|
+
import { ModuleError } from 'iso-error'
|
|
2
2
|
|
|
3
3
|
export class ContextAlreadyInitialized extends ModuleError {
|
|
4
|
-
constructor(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?:
|
|
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
|
}
|