@dxos/context 0.1.20 → 0.1.22
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.
|
@@ -23,6 +23,13 @@ var Context = class Context1 {
|
|
|
23
23
|
get disposed() {
|
|
24
24
|
return this._isDisposed;
|
|
25
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Schedules a callback to run when the context is disposed.
|
|
28
|
+
* May be async, in this case the disposer might choose to wait for all resource to released.
|
|
29
|
+
* Throwing an error inside the callback will result in the error being logged, but not re-thrown.
|
|
30
|
+
*
|
|
31
|
+
* NOTE: Will call the callback immediately if the context is already disposed.
|
|
32
|
+
*/
|
|
26
33
|
onDispose(callback) {
|
|
27
34
|
if (this._isDisposed) {
|
|
28
35
|
void (async () => {
|
|
@@ -40,6 +47,14 @@ var Context = class Context1 {
|
|
|
40
47
|
}
|
|
41
48
|
this._disposeCallbacks.push(callback);
|
|
42
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* Runs all dispose callbacks.
|
|
52
|
+
* Sync callbacks are run in the reverse order they were added.
|
|
53
|
+
* Async callbacks are run in parallel.
|
|
54
|
+
* This function never throws.
|
|
55
|
+
* It is safe to ignore the returned promise if the caller does not wish to wait for callbacks to complete.
|
|
56
|
+
* Disposing context means that onDispose will throw an error and any errors raised will be logged and not propagated.
|
|
57
|
+
*/
|
|
43
58
|
dispose() {
|
|
44
59
|
if (this._disposePromise) {
|
|
45
60
|
return this._disposePromise;
|
|
@@ -64,6 +79,11 @@ var Context = class Context1 {
|
|
|
64
79
|
return this._disposePromise = Promise.all(promises).then(() => {
|
|
65
80
|
});
|
|
66
81
|
}
|
|
82
|
+
/**
|
|
83
|
+
* Raise the error inside the context.
|
|
84
|
+
* The error will be propagated to the error handler.
|
|
85
|
+
* IF the error handler is not set, the error will dispose the context and cause an unhandled rejection.
|
|
86
|
+
*/
|
|
67
87
|
raise(error) {
|
|
68
88
|
if (this._isDisposed) {
|
|
69
89
|
log.warn("Error in disposed context", error, {
|
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/context.ts"],
|
|
4
4
|
"sourcesContent": ["//\n// Copyright 2022 DXOS.org\n//\n\nimport { log } from '@dxos/log';\nimport { safeInstanceof } from '@dxos/util';\n\nexport type ContextErrorHandler = (error: Error) => void;\n\nexport type DisposeCallback = () => void | Promise<void>;\n\nexport type CreateContextParams = {\n onError?: ContextErrorHandler;\n};\n\n@safeInstanceof('Context')\nexport class Context {\n private readonly _onError: ContextErrorHandler;\n private readonly _disposeCallbacks: DisposeCallback[] = [];\n private _isDisposed = false;\n private _disposePromise?: Promise<void>;\n\n constructor({\n onError = (error) => {\n void this.dispose();\n\n // Will generate an unhandled rejection.\n throw error;\n }\n }: CreateContextParams = {}) {\n this._onError = onError;\n }\n\n get disposed() {\n return this._isDisposed;\n }\n\n /**\n * Schedules a callback to run when the context is disposed.\n * May be async, in this case the disposer might choose to wait for all resource to released.\n * Throwing an error inside the callback will result in the error being logged, but not re-thrown.\n *\n * NOTE: Will call the callback immediately if the context is already disposed.\n */\n onDispose(callback: DisposeCallback) {\n if (this._isDisposed) {\n // Call the callback immediately if the context is already disposed.\n void (async () => {\n try {\n await callback();\n } catch (error: any) {\n log.catch(error);\n }\n })();\n }\n\n this._disposeCallbacks.push(callback);\n }\n\n /**\n * Runs all dispose callbacks.\n * Sync callbacks are run in the reverse order they were added.\n * Async callbacks are run in parallel.\n * This function never throws.\n * It is safe to ignore the returned promise if the caller does not wish to wait for callbacks to complete.\n * Disposing context means that onDispose will throw an error and any errors raised will be logged and not propagated.\n */\n dispose(): Promise<void> {\n if (this._disposePromise) {\n return this._disposePromise;\n }\n this._isDisposed = true;\n\n const promises = [];\n for (const callback of this._disposeCallbacks.reverse()) {\n promises.push(\n (async () => {\n try {\n await callback();\n } catch (error: any) {\n log.catch(error);\n }\n })()\n );\n }\n this._disposeCallbacks.length = 0;\n\n return (this._disposePromise = Promise.all(promises).then(() => {}));\n }\n\n /**\n * Raise the error inside the context.\n * The error will be propagated to the error handler.\n * IF the error handler is not set, the error will dispose the context and cause an unhandled rejection.\n */\n raise(error: Error): void {\n if (this._isDisposed) {\n log.warn('Error in disposed context', error);\n return;\n }\n\n try {\n this._onError(error);\n } catch (err) {\n // Generate an unhandled rejection and stop the error propagation.\n void Promise.reject(err);\n }\n }\n\n derive({ onError }: CreateContextParams): Context {\n const newCtx = new Context({\n onError: async (error) => {\n if (!onError) {\n this.raise(error);\n } else {\n try {\n await onError(error);\n } catch {\n this.raise(error);\n }\n }\n }\n });\n this.onDispose(() => newCtx.dispose());\n return newCtx;\n }\n}\n"],
|
|
5
|
-
"mappings": ";AAIA,SAASA,WAAW;AACpB,SAASC,sBAAsB;AAD/B,IAAA,aAAA,SAAA,YAAA,QAAA,KAAA,MAAA;;;;;;;;;;AAYA,IAAaC,UAAN,MAAA,SAAA;EAMLC,YAAY,EACVC,UAAU,CAACC,UAAU;AACnB,SAAK,KAAKC,QAAO;AAGjB,UAAMD;EACR,EAAC,IACsB,CAAC,GAAG;AAXZE,6BAAuC,CAAA;AAChDC,uBAAc;AAWpB,SAAKC,WAAWL;EAClB;EAEA,IAAIM,WAAW;AACb,WAAO,KAAKF;EACd
|
|
5
|
+
"mappings": ";AAIA,SAASA,WAAW;AACpB,SAASC,sBAAsB;AAD/B,IAAA,aAAA,SAAA,YAAA,QAAA,KAAA,MAAA;;;;;;;;;;AAYA,IAAaC,UAAN,MAAA,SAAA;EAMLC,YAAY,EACVC,UAAU,CAACC,UAAU;AACnB,SAAK,KAAKC,QAAO;AAGjB,UAAMD;EACR,EAAC,IACsB,CAAC,GAAG;AAXZE,6BAAuC,CAAA;AAChDC,uBAAc;AAWpB,SAAKC,WAAWL;EAClB;EAEA,IAAIM,WAAW;AACb,WAAO,KAAKF;EACd;;;;;;;;EASAG,UAAUC,UAA2B;AACnC,QAAI,KAAKJ,aAAa;AAEpB,YAAM,YAAY;AAChB,YAAI;AACF,gBAAMI,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"constructor", "onError", "error", "dispose", "_disposeCallbacks", "_isDisposed", "_onError", "disposed", "onDispose", "callback", "catch", "push", "_disposePromise", "promises", "reverse", "length", "Promise", "all", "then", "raise", "warn", "err", "reject", "derive", "newCtx"]
|
|
7
7
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs":{"packages/common/context/src/context.ts":{"bytes":11862,"imports":[]},"packages/common/context/src/index.ts":{"bytes":375,"imports":[{"path":"packages/common/context/src/context.ts","kind":"import-statement"}]}},"outputs":{"packages/common/context/dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":
|
|
1
|
+
{"inputs":{"packages/common/context/src/context.ts":{"bytes":11862,"imports":[{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}]},"packages/common/context/src/index.ts":{"bytes":375,"imports":[{"path":"packages/common/context/src/context.ts","kind":"import-statement","original":"./context"}]}},"outputs":{"packages/common/context/dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":5359},"packages/common/context/dist/lib/browser/index.mjs":{"imports":[{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}],"exports":["Context"],"entryPoint":"packages/common/context/src/index.ts","inputs":{"packages/common/context/src/context.ts":{"bytesInOutput":3661},"packages/common/context/src/index.ts":{"bytesInOutput":0}},"bytes":3760}}}
|
package/dist/lib/node/index.cjs
CHANGED
|
@@ -49,6 +49,13 @@ var Context = class Context1 {
|
|
|
49
49
|
get disposed() {
|
|
50
50
|
return this._isDisposed;
|
|
51
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* Schedules a callback to run when the context is disposed.
|
|
54
|
+
* May be async, in this case the disposer might choose to wait for all resource to released.
|
|
55
|
+
* Throwing an error inside the callback will result in the error being logged, but not re-thrown.
|
|
56
|
+
*
|
|
57
|
+
* NOTE: Will call the callback immediately if the context is already disposed.
|
|
58
|
+
*/
|
|
52
59
|
onDispose(callback) {
|
|
53
60
|
if (this._isDisposed) {
|
|
54
61
|
void (async () => {
|
|
@@ -66,6 +73,14 @@ var Context = class Context1 {
|
|
|
66
73
|
}
|
|
67
74
|
this._disposeCallbacks.push(callback);
|
|
68
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* Runs all dispose callbacks.
|
|
78
|
+
* Sync callbacks are run in the reverse order they were added.
|
|
79
|
+
* Async callbacks are run in parallel.
|
|
80
|
+
* This function never throws.
|
|
81
|
+
* It is safe to ignore the returned promise if the caller does not wish to wait for callbacks to complete.
|
|
82
|
+
* Disposing context means that onDispose will throw an error and any errors raised will be logged and not propagated.
|
|
83
|
+
*/
|
|
69
84
|
dispose() {
|
|
70
85
|
if (this._disposePromise) {
|
|
71
86
|
return this._disposePromise;
|
|
@@ -90,6 +105,11 @@ var Context = class Context1 {
|
|
|
90
105
|
return this._disposePromise = Promise.all(promises).then(() => {
|
|
91
106
|
});
|
|
92
107
|
}
|
|
108
|
+
/**
|
|
109
|
+
* Raise the error inside the context.
|
|
110
|
+
* The error will be propagated to the error handler.
|
|
111
|
+
* IF the error handler is not set, the error will dispose the context and cause an unhandled rejection.
|
|
112
|
+
*/
|
|
93
113
|
raise(error) {
|
|
94
114
|
if (this._isDisposed) {
|
|
95
115
|
import_log.log.warn("Error in disposed context", error, {
|
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/index.ts", "../../../src/context.ts"],
|
|
4
4
|
"sourcesContent": ["//\n// Copyright 2022 DXOS.org\n//\n\nexport * from './context';\n", "//\n// Copyright 2022 DXOS.org\n//\n\nimport { log } from '@dxos/log';\nimport { safeInstanceof } from '@dxos/util';\n\nexport type ContextErrorHandler = (error: Error) => void;\n\nexport type DisposeCallback = () => void | Promise<void>;\n\nexport type CreateContextParams = {\n onError?: ContextErrorHandler;\n};\n\n@safeInstanceof('Context')\nexport class Context {\n private readonly _onError: ContextErrorHandler;\n private readonly _disposeCallbacks: DisposeCallback[] = [];\n private _isDisposed = false;\n private _disposePromise?: Promise<void>;\n\n constructor({\n onError = (error) => {\n void this.dispose();\n\n // Will generate an unhandled rejection.\n throw error;\n }\n }: CreateContextParams = {}) {\n this._onError = onError;\n }\n\n get disposed() {\n return this._isDisposed;\n }\n\n /**\n * Schedules a callback to run when the context is disposed.\n * May be async, in this case the disposer might choose to wait for all resource to released.\n * Throwing an error inside the callback will result in the error being logged, but not re-thrown.\n *\n * NOTE: Will call the callback immediately if the context is already disposed.\n */\n onDispose(callback: DisposeCallback) {\n if (this._isDisposed) {\n // Call the callback immediately if the context is already disposed.\n void (async () => {\n try {\n await callback();\n } catch (error: any) {\n log.catch(error);\n }\n })();\n }\n\n this._disposeCallbacks.push(callback);\n }\n\n /**\n * Runs all dispose callbacks.\n * Sync callbacks are run in the reverse order they were added.\n * Async callbacks are run in parallel.\n * This function never throws.\n * It is safe to ignore the returned promise if the caller does not wish to wait for callbacks to complete.\n * Disposing context means that onDispose will throw an error and any errors raised will be logged and not propagated.\n */\n dispose(): Promise<void> {\n if (this._disposePromise) {\n return this._disposePromise;\n }\n this._isDisposed = true;\n\n const promises = [];\n for (const callback of this._disposeCallbacks.reverse()) {\n promises.push(\n (async () => {\n try {\n await callback();\n } catch (error: any) {\n log.catch(error);\n }\n })()\n );\n }\n this._disposeCallbacks.length = 0;\n\n return (this._disposePromise = Promise.all(promises).then(() => {}));\n }\n\n /**\n * Raise the error inside the context.\n * The error will be propagated to the error handler.\n * IF the error handler is not set, the error will dispose the context and cause an unhandled rejection.\n */\n raise(error: Error): void {\n if (this._isDisposed) {\n log.warn('Error in disposed context', error);\n return;\n }\n\n try {\n this._onError(error);\n } catch (err) {\n // Generate an unhandled rejection and stop the error propagation.\n void Promise.reject(err);\n }\n }\n\n derive({ onError }: CreateContextParams): Context {\n const newCtx = new Context({\n onError: async (error) => {\n if (!onError) {\n this.raise(error);\n } else {\n try {\n await onError(error);\n } catch {\n this.raise(error);\n }\n }\n }\n });\n this.onDispose(() => newCtx.d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YAAY;AACX,YAAI;AACF,gBAAMH,SAAAA;QACR,SAASP,OAAP;AACAQ,yBAAIC,MAAMT,OAAAA,CAAAA,GAAAA;;;;;;QACZ;MACF,GAAA,CAAA;IAEJ;AACA,SAAKE,kBAAkBY,SAAS;AAEhC,WAAQ,KAAKH,kBAAkBI,QAAQC,IAAIJ,QAAAA,EAAUK,KAAK,MAAM;IAAC,CAAA;EACnE;;;;;;EAOAC,MAAMlB,OAAoB;AACxB,QAAI,KAAKG,aAAa;AACpBK,qBAAIW,KAAK,6BAA6BnB,OAAAA;;;;;;AACtC;IACF;AAEA,QAAI;AACF,WAAKI,SAASJ,KAAAA;IAChB,SAASoB,KAAP;AAEA,WAAKL,QAAQM,OAAOD,GAAAA;IACtB;EACF;EAEAE,OAAO,EAAEvB,QAAO,GAAkC;AAChD,UAAMwB,SAAS,IAAI1B,QAAQ;MACzBE,SAAS,OAAOC,UAAU;AACxB,YAAI,CAACD,SAAS;AACZ,eAAKmB,MAAMlB,KAAAA;QACb,OAAO;AACL,cAAI;AACF,kBAAMD,QAAQC,KAAAA;UAChB,SAAQ,GAAN;AACA,iBAAKkB,MAAMlB,KAAAA;UACb;QACF;MACF;IACF,CAAA;AACA,SAAKM,UAAU,MAAMiB,OAAOtB,QAAO,CAAA;AACnC,WAAOsB;EACT;AACF;AA9Ga1B,UAAAA,WAAAA;MADZ2B,4BAAe,SAAA;GACH3B,OAAAA;",
|
|
6
6
|
"names": ["Context", "constructor", "onError", "error", "dispose", "_disposeCallbacks", "_isDisposed", "_onError", "disposed", "onDispose", "callback", "log", "catch", "push", "_disposePromise", "promises", "reverse", "length", "Promise", "all", "then", "raise", "warn", "err", "reject", "derive", "newCtx", "safeInstanceof"]
|
|
7
7
|
}
|
package/dist/lib/node/meta.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs":{"packages/common/context/src/context.ts":{"bytes":11862,"imports":[]},"packages/common/context/src/index.ts":{"bytes":375,"imports":[{"path":"packages/common/context/src/context.ts","kind":"import-statement"}]}},"outputs":{"packages/common/context/dist/lib/node/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":
|
|
1
|
+
{"inputs":{"packages/common/context/src/context.ts":{"bytes":11862,"imports":[{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}]},"packages/common/context/src/index.ts":{"bytes":375,"imports":[{"path":"packages/common/context/src/context.ts","kind":"import-statement","original":"./context"}]}},"outputs":{"packages/common/context/dist/lib/node/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":5479},"packages/common/context/dist/lib/node/index.cjs":{"imports":[{"path":"@dxos/log","kind":"require-call","external":true},{"path":"@dxos/util","kind":"require-call","external":true}],"exports":[],"entryPoint":"packages/common/context/src/index.ts","inputs":{"packages/common/context/src/index.ts":{"bytesInOutput":119},"packages/common/context/src/context.ts":{"bytesInOutput":3713}},"bytes":4851}}}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/context",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.22",
|
|
4
4
|
"description": "Async utils.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
"src"
|
|
17
17
|
],
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@dxos/log": "0.1.
|
|
20
|
-
"@dxos/util": "0.1.
|
|
19
|
+
"@dxos/log": "0.1.22",
|
|
20
|
+
"@dxos/util": "0.1.22"
|
|
21
21
|
},
|
|
22
22
|
"publishConfig": {
|
|
23
23
|
"access": "public"
|