@dxos/context 0.1.56-main.bf228a7 → 0.1.56-main.c19b7cd
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/dist/lib/browser/index.mjs +22 -6
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +22 -6
- package/dist/lib/node/index.cjs.map +3 -3
- package/dist/lib/node/meta.json +1 -1
- package/dist/types/src/context.d.ts +7 -2
- package/dist/types/src/context.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/context.ts +24 -1
|
@@ -17,10 +17,15 @@ var Context = class Context1 {
|
|
|
17
17
|
constructor({ onError = (error) => {
|
|
18
18
|
void this.dispose();
|
|
19
19
|
throw error;
|
|
20
|
-
} } = {}) {
|
|
20
|
+
}, attributes = {}, parent } = {}) {
|
|
21
21
|
this._disposeCallbacks = [];
|
|
22
22
|
this._isDisposed = false;
|
|
23
|
+
this._parent = null;
|
|
23
24
|
this._onError = onError;
|
|
25
|
+
this._attributes = attributes;
|
|
26
|
+
if (parent !== void 0) {
|
|
27
|
+
this._parent = parent;
|
|
28
|
+
}
|
|
24
29
|
}
|
|
25
30
|
get disposed() {
|
|
26
31
|
return this._isDisposed;
|
|
@@ -42,7 +47,7 @@ var Context = class Context1 {
|
|
|
42
47
|
} catch (error) {
|
|
43
48
|
log.catch(error, void 0, {
|
|
44
49
|
F: __dxlog_file,
|
|
45
|
-
L:
|
|
50
|
+
L: 70,
|
|
46
51
|
S: this,
|
|
47
52
|
C: (f, a) => f(...a)
|
|
48
53
|
});
|
|
@@ -56,7 +61,7 @@ var Context = class Context1 {
|
|
|
56
61
|
safeThreshold: MAX_SAFE_DISPOSE_CALLBACKS
|
|
57
62
|
}, {
|
|
58
63
|
F: __dxlog_file,
|
|
59
|
-
L:
|
|
64
|
+
L: 77,
|
|
60
65
|
S: this,
|
|
61
66
|
C: (f, a) => f(...a)
|
|
62
67
|
});
|
|
@@ -89,7 +94,7 @@ var Context = class Context1 {
|
|
|
89
94
|
} catch (error) {
|
|
90
95
|
log.catch(error, void 0, {
|
|
91
96
|
F: __dxlog_file,
|
|
92
|
-
L:
|
|
97
|
+
L: 112,
|
|
93
98
|
S: this,
|
|
94
99
|
C: (f, a) => f(...a)
|
|
95
100
|
});
|
|
@@ -115,8 +120,9 @@ var Context = class Context1 {
|
|
|
115
120
|
void Promise.reject(err);
|
|
116
121
|
}
|
|
117
122
|
}
|
|
118
|
-
derive({ onError } = {}) {
|
|
123
|
+
derive({ onError, attributes } = {}) {
|
|
119
124
|
const newCtx = new Context({
|
|
125
|
+
// TODO(dmaretskyi): Optimize to not require allocating a new closure for every context.
|
|
120
126
|
onError: async (error) => {
|
|
121
127
|
if (!onError) {
|
|
122
128
|
this.raise(error);
|
|
@@ -127,12 +133,22 @@ var Context = class Context1 {
|
|
|
127
133
|
this.raise(error);
|
|
128
134
|
}
|
|
129
135
|
}
|
|
130
|
-
}
|
|
136
|
+
},
|
|
137
|
+
attributes
|
|
131
138
|
});
|
|
132
139
|
const clearDispose = this.onDispose(() => newCtx.dispose());
|
|
133
140
|
newCtx.onDispose(clearDispose);
|
|
134
141
|
return newCtx;
|
|
135
142
|
}
|
|
143
|
+
getAttribute(key) {
|
|
144
|
+
if (key in this._attributes) {
|
|
145
|
+
return this._attributes[key];
|
|
146
|
+
}
|
|
147
|
+
if (this._parent !== null) {
|
|
148
|
+
return this._parent.getAttribute(key);
|
|
149
|
+
}
|
|
150
|
+
return void 0;
|
|
151
|
+
}
|
|
136
152
|
};
|
|
137
153
|
Context = _ts_decorate([
|
|
138
154
|
safeInstanceof("Context")
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/context.ts", "../../../src/promise-utils.ts"],
|
|
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/**\n * Maximum number of dispose callbacks before we start logging warnings.\n */\nconst MAX_SAFE_DISPOSE_CALLBACKS = 300;\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 * @returns A function that can be used to remove the callback from the dispose list.\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 if (this._disposeCallbacks.length > MAX_SAFE_DISPOSE_CALLBACKS) {\n log.warn('Context has a large number of dispose callbacks. This might be a memory leak.', {\n count: this._disposeCallbacks.length,\n safeThreshold: MAX_SAFE_DISPOSE_CALLBACKS,\n });\n }\n\n return () => {\n const index = this._disposeCallbacks.indexOf(callback);\n if (index !== -1) {\n this._disposeCallbacks.splice(index, 1);\n }\n };\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 // TODO(dmaretskyi): Don't log those.\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 const clearDispose = this.onDispose(() => newCtx.dispose());\n newCtx.onDispose(clearDispose);\n return newCtx;\n }\n}\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { CancelledError } from '@dxos/errors';\n\nimport { Context } from './context';\n\n/**\n * @returns A promise that rejects when the context is disposed.\n */\n// TODO(dmaretskyi): Memory leak.\nexport const rejectOnDispose = (ctx: Context, error = new CancelledError()): Promise<never> =>\n new Promise((resolve, reject) => {\n ctx.onDispose(() => reject(error));\n });\n\n/**\n * Rejects the promise if the context is disposed.\n */\nexport const cancelWithContext = <T>(ctx: Context, promise: Promise<T>): Promise<T> => {\n let clearDispose: () => void;\n return Promise.race([\n promise,\n new Promise<never>((resolve, reject) => {\n // Will be called before .finally() handlers.\n clearDispose = ctx.onDispose(() => reject(new CancelledError()));\n }),\n ]).finally(() => clearDispose?.());\n};\n"],
|
|
5
|
-
"mappings": ";AAIA,SAASA,WAAW;AACpB,SAASC,sBAAsB;;;;;;;;;;;;
|
|
6
|
-
"names": ["log", "safeInstanceof", "MAX_SAFE_DISPOSE_CALLBACKS", "Context", "constructor", "onError", "error", "dispose", "_disposeCallbacks", "_isDisposed", "_onError", "disposed", "onDispose", "callback", "catch", "push", "length", "warn", "count", "safeThreshold", "index", "indexOf", "splice", "_disposePromise", "promises", "reverse", "Promise", "all", "then", "raise", "err", "reject", "derive", "newCtx", "clearDispose", "CancelledError", "rejectOnDispose", "ctx", "error", "CancelledError", "Promise", "resolve", "reject", "onDispose", "cancelWithContext", "promise", "clearDispose", "race", "finally"]
|
|
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 attributes?: Record<string, any>;\n parent?: Context;\n};\n\n/**\n * Maximum number of dispose callbacks before we start logging warnings.\n */\nconst MAX_SAFE_DISPOSE_CALLBACKS = 300;\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 private _parent: Context | null = null;\n\n private _attributes: Record<string, any>;\n\n constructor({\n onError = (error) => {\n void this.dispose();\n\n // Will generate an unhandled rejection.\n throw error;\n },\n attributes = {},\n parent,\n }: CreateContextParams = {}) {\n this._onError = onError;\n this._attributes = attributes;\n if (parent !== undefined) {\n this._parent = parent;\n }\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 * @returns A function that can be used to remove the callback from the dispose list.\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 if (this._disposeCallbacks.length > MAX_SAFE_DISPOSE_CALLBACKS) {\n log.warn('Context has a large number of dispose callbacks. This might be a memory leak.', {\n count: this._disposeCallbacks.length,\n safeThreshold: MAX_SAFE_DISPOSE_CALLBACKS,\n });\n }\n\n return () => {\n const index = this._disposeCallbacks.indexOf(callback);\n if (index !== -1) {\n this._disposeCallbacks.splice(index, 1);\n }\n };\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 // TODO(dmaretskyi): Don't log those.\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, attributes }: CreateContextParams = {}): Context {\n const newCtx = new Context({\n // TODO(dmaretskyi): Optimize to not require allocating a new closure for every 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 attributes,\n });\n const clearDispose = this.onDispose(() => newCtx.dispose());\n newCtx.onDispose(clearDispose);\n return newCtx;\n }\n\n getAttribute(key: string): any {\n if (key in this._attributes) {\n return this._attributes[key];\n }\n if (this._parent !== null) {\n return this._parent.getAttribute(key);\n }\n return undefined;\n }\n}\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { CancelledError } from '@dxos/errors';\n\nimport { Context } from './context';\n\n/**\n * @returns A promise that rejects when the context is disposed.\n */\n// TODO(dmaretskyi): Memory leak.\nexport const rejectOnDispose = (ctx: Context, error = new CancelledError()): Promise<never> =>\n new Promise((resolve, reject) => {\n ctx.onDispose(() => reject(error));\n });\n\n/**\n * Rejects the promise if the context is disposed.\n */\nexport const cancelWithContext = <T>(ctx: Context, promise: Promise<T>): Promise<T> => {\n let clearDispose: () => void;\n return Promise.race([\n promise,\n new Promise<never>((resolve, reject) => {\n // Will be called before .finally() handlers.\n clearDispose = ctx.onDispose(() => reject(new CancelledError()));\n }),\n ]).finally(() => clearDispose?.());\n};\n"],
|
|
5
|
+
"mappings": ";AAIA,SAASA,WAAW;AACpB,SAASC,sBAAsB;;;;;;;;;;;;AAe/B,IAAMC,6BAA6B;AAGnC,IAAaC,UAAN,MAAA,SAAA;EASLC,YAAY,EACVC,UAAU,CAACC,UAAAA;AACT,SAAK,KAAKC,QAAO;AAGjB,UAAMD;EACR,GACAE,aAAa,CAAC,GACdC,OAAM,IACiB,CAAC,GAAG;AAhBZC,6BAAuC,CAAA;AAChDC,uBAAc;AAEdC,mBAA0B;AAchC,SAAKC,WAAWR;AAChB,SAAKS,cAAcN;AACnB,QAAIC,WAAWM,QAAW;AACxB,WAAKH,UAAUH;IACjB;EACF;EAEA,IAAIO,WAAW;AACb,WAAO,KAAKL;EACd;;;;;;;;;;EAWAM,UAAUC,UAA2B;AACnC,QAAI,KAAKP,aAAa;AAEpB,YAAM,YAAA;AACJ,YAAI;AACF,gBAAMO,SAAAA;QACR,SAASZ,OAAP;AACAN,cAAImB,MAAMb,OAAAA,QAAAA;;;;;;QACZ;MACF,GAAA;IACF;AAEA,SAAKI,kBAAkBU,KAAKF,QAAAA;AAC5B,QAAI,KAAKR,kBAAkBW,SAASnB,4BAA4B;A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B,WAAOrB,UAAUsB,YAAAA;AACjB,WAAOD;EACT;EAEAE,aAAaC,KAAkB;AAC7B,QAAIA,OAAO,KAAK3B,aAAa;AAC3B,aAAO,KAAKA,YAAY2B,GAAAA;IAC1B;AACA,QAAI,KAAK7B,YAAY,MAAM;AACzB,aAAO,KAAKA,QAAQ4B,aAAaC,GAAAA;IACnC;AACA,WAAO1B;EACT;AACF;AApJaZ,UAAAA,aAAAA;EADZF,eAAe,SAAA;GACHE,OAAAA;;;ACnBb,SAASuC,sBAAsB;AAQxB,IAAMC,kBAAkB,CAACC,KAAcC,QAAQ,IAAIC,eAAAA,MACxD,IAAIC,QAAQ,CAACC,SAASC,WAAAA;AACpBL,MAAIM,UAAU,MAAMD,OAAOJ,KAAAA,CAAAA;AAC7B,CAAA;AAKK,IAAMM,oBAAoB,CAAIP,KAAcQ,YAAAA;AACjD,MAAIC;AACJ,SAAON,QAAQO,KAAK;IAClBF;IACA,IAAIL,QAAe,CAACC,SAASC,WAAAA;AAE3BI,qBAAeT,IAAIM,UAAU,MAAMD,OAAO,IAAIH,eAAAA,CAAAA,CAAAA;IAChD,CAAA;GACD,EAAES,QAAQ,MAAMF,8CAAAA;AACnB;",
|
|
6
|
+
"names": ["log", "safeInstanceof", "MAX_SAFE_DISPOSE_CALLBACKS", "Context", "constructor", "onError", "error", "dispose", "attributes", "parent", "_disposeCallbacks", "_isDisposed", "_parent", "_onError", "_attributes", "undefined", "disposed", "onDispose", "callback", "catch", "push", "length", "warn", "count", "safeThreshold", "index", "indexOf", "splice", "_disposePromise", "promises", "reverse", "Promise", "all", "then", "raise", "err", "reject", "derive", "newCtx", "clearDispose", "getAttribute", "key", "CancelledError", "rejectOnDispose", "ctx", "error", "CancelledError", "Promise", "resolve", "reject", "onDispose", "cancelWithContext", "promise", "clearDispose", "race", "finally"]
|
|
7
7
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs":{"packages/common/context/src/context.ts":{"bytes":
|
|
1
|
+
{"inputs":{"packages/common/context/src/context.ts":{"bytes":16481,"imports":[{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}]},"packages/common/context/src/promise-utils.ts":{"bytes":2954,"imports":[{"path":"@dxos/errors","kind":"import-statement","external":true}]},"packages/common/context/src/index.ts":{"bytes":563,"imports":[{"path":"packages/common/context/src/context.ts","kind":"import-statement","original":"./context"},{"path":"packages/common/context/src/promise-utils.ts","kind":"import-statement","original":"./promise-utils"}]}},"outputs":{"packages/common/context/dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":8988},"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},{"path":"@dxos/errors","kind":"import-statement","external":true}],"exports":["Context","cancelWithContext","rejectOnDispose"],"entryPoint":"packages/common/context/src/index.ts","inputs":{"packages/common/context/src/context.ts":{"bytesInOutput":4775},"packages/common/context/src/index.ts":{"bytesInOutput":0},"packages/common/context/src/promise-utils.ts":{"bytesInOutput":475}},"bytes":5438}}}
|
package/dist/lib/node/index.cjs
CHANGED
|
@@ -45,10 +45,15 @@ var Context = class Context1 {
|
|
|
45
45
|
constructor({ onError = (error) => {
|
|
46
46
|
void this.dispose();
|
|
47
47
|
throw error;
|
|
48
|
-
} } = {}) {
|
|
48
|
+
}, attributes = {}, parent } = {}) {
|
|
49
49
|
this._disposeCallbacks = [];
|
|
50
50
|
this._isDisposed = false;
|
|
51
|
+
this._parent = null;
|
|
51
52
|
this._onError = onError;
|
|
53
|
+
this._attributes = attributes;
|
|
54
|
+
if (parent !== void 0) {
|
|
55
|
+
this._parent = parent;
|
|
56
|
+
}
|
|
52
57
|
}
|
|
53
58
|
get disposed() {
|
|
54
59
|
return this._isDisposed;
|
|
@@ -70,7 +75,7 @@ var Context = class Context1 {
|
|
|
70
75
|
} catch (error) {
|
|
71
76
|
import_log.log.catch(error, void 0, {
|
|
72
77
|
F: __dxlog_file,
|
|
73
|
-
L:
|
|
78
|
+
L: 70,
|
|
74
79
|
S: this,
|
|
75
80
|
C: (f, a) => f(...a)
|
|
76
81
|
});
|
|
@@ -84,7 +89,7 @@ var Context = class Context1 {
|
|
|
84
89
|
safeThreshold: MAX_SAFE_DISPOSE_CALLBACKS
|
|
85
90
|
}, {
|
|
86
91
|
F: __dxlog_file,
|
|
87
|
-
L:
|
|
92
|
+
L: 77,
|
|
88
93
|
S: this,
|
|
89
94
|
C: (f, a) => f(...a)
|
|
90
95
|
});
|
|
@@ -117,7 +122,7 @@ var Context = class Context1 {
|
|
|
117
122
|
} catch (error) {
|
|
118
123
|
import_log.log.catch(error, void 0, {
|
|
119
124
|
F: __dxlog_file,
|
|
120
|
-
L:
|
|
125
|
+
L: 112,
|
|
121
126
|
S: this,
|
|
122
127
|
C: (f, a) => f(...a)
|
|
123
128
|
});
|
|
@@ -143,8 +148,9 @@ var Context = class Context1 {
|
|
|
143
148
|
void Promise.reject(err);
|
|
144
149
|
}
|
|
145
150
|
}
|
|
146
|
-
derive({ onError } = {}) {
|
|
151
|
+
derive({ onError, attributes } = {}) {
|
|
147
152
|
const newCtx = new Context({
|
|
153
|
+
// TODO(dmaretskyi): Optimize to not require allocating a new closure for every context.
|
|
148
154
|
onError: async (error) => {
|
|
149
155
|
if (!onError) {
|
|
150
156
|
this.raise(error);
|
|
@@ -155,12 +161,22 @@ var Context = class Context1 {
|
|
|
155
161
|
this.raise(error);
|
|
156
162
|
}
|
|
157
163
|
}
|
|
158
|
-
}
|
|
164
|
+
},
|
|
165
|
+
attributes
|
|
159
166
|
});
|
|
160
167
|
const clearDispose = this.onDispose(() => newCtx.dispose());
|
|
161
168
|
newCtx.onDispose(clearDispose);
|
|
162
169
|
return newCtx;
|
|
163
170
|
}
|
|
171
|
+
getAttribute(key) {
|
|
172
|
+
if (key in this._attributes) {
|
|
173
|
+
return this._attributes[key];
|
|
174
|
+
}
|
|
175
|
+
if (this._parent !== null) {
|
|
176
|
+
return this._parent.getAttribute(key);
|
|
177
|
+
}
|
|
178
|
+
return void 0;
|
|
179
|
+
}
|
|
164
180
|
};
|
|
165
181
|
Context = _ts_decorate([
|
|
166
182
|
(0, import_util.safeInstanceof)("Context")
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/index.ts", "../../../src/context.ts", "../../../src/promise-utils.ts"],
|
|
4
|
-
"sourcesContent": ["//\n// Copyright 2022 DXOS.org\n//\n\nexport * from './context';\nexport * from './promise-utils';\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/**\n * Maximum number of dispose callbacks before we start logging warnings.\n */\nconst MAX_SAFE_DISPOSE_CALLBACKS = 300;\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 * @returns A function that can be used to remove the callback from the dispose list.\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 if (this._disposeCallbacks.length > MAX_SAFE_DISPOSE_CALLBACKS) {\n log.warn('Context has a large number of dispose callbacks. This might be a memory leak.', {\n count: this._disposeCallbacks.length,\n safeThreshold: MAX_SAFE_DISPOSE_CALLBACKS,\n });\n }\n\n return () => {\n const index = this._disposeCallbacks.indexOf(callback);\n if (index !== -1) {\n this._disposeCallbacks.splice(index, 1);\n }\n };\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 // TODO(dmaretskyi): Don't log those.\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 const clearDispose = this.onDispose(() => newCtx.dispose());\n newCtx.onDispose(clearDispose);\n return newCtx;\n }\n}\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { CancelledError } from '@dxos/errors';\n\nimport { Context } from './context';\n\n/**\n * @returns A promise that rejects when the context is disposed.\n */\n// TODO(dmaretskyi): Memory leak.\nexport const rejectOnDispose = (ctx: Context, error = new CancelledError()): Promise<never> =>\n new Promise((resolve, reject) => {\n ctx.onDispose(() => reject(error));\n });\n\n/**\n * Rejects the promise if the context is disposed.\n */\nexport const cancelWithContext = <T>(ctx: Context, promise: Promise<T>): Promise<T> => {\n let clearDispose: () => void;\n return Promise.race([\n promise,\n new Promise<never>((resolve, reject) => {\n // Will be called before .finally() handlers.\n clearDispose = ctx.onDispose(() => reject(new CancelledError()));\n }),\n ]).finally(() => clearDispose?.());\n};\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;ACIA,iBAAoB;AACpB,kBAA+B;;;;;;;;;;;;
|
|
6
|
-
"names": ["MAX_SAFE_DISPOSE_CALLBACKS", "Context", "constructor", "onError", "error", "dispose", "_disposeCallbacks", "_isDisposed", "_onError", "disposed", "onDispose", "callback", "log", "catch", "push", "length", "warn", "count", "safeThreshold", "index", "indexOf", "splice", "_disposePromise", "promises", "reverse", "Promise", "all", "then", "raise", "err", "reject", "derive", "newCtx", "clearDispose", "safeInstanceof", "rejectOnDispose", "ctx", "error", "CancelledError", "Promise", "resolve", "reject", "onDispose", "cancelWithContext", "promise", "clearDispose", "race", "finally"]
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2022 DXOS.org\n//\n\nexport * from './context';\nexport * from './promise-utils';\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 attributes?: Record<string, any>;\n parent?: Context;\n};\n\n/**\n * Maximum number of dispose callbacks before we start logging warnings.\n */\nconst MAX_SAFE_DISPOSE_CALLBACKS = 300;\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 private _parent: Context | null = null;\n\n private _attributes: Record<string, any>;\n\n constructor({\n onError = (error) => {\n void this.dispose();\n\n // Will generate an unhandled rejection.\n throw error;\n },\n attributes = {},\n parent,\n }: CreateContextParams = {}) {\n this._onError = onError;\n this._attributes = attributes;\n if (parent !== undefined) {\n this._parent = parent;\n }\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 * @returns A function that can be used to remove the callback from the dispose list.\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 if (this._disposeCallbacks.length > MAX_SAFE_DISPOSE_CALLBACKS) {\n log.warn('Context has a large number of dispose callbacks. This might be a memory leak.', {\n count: this._disposeCallbacks.length,\n safeThreshold: MAX_SAFE_DISPOSE_CALLBACKS,\n });\n }\n\n return () => {\n const index = this._disposeCallbacks.indexOf(callback);\n if (index !== -1) {\n this._disposeCallbacks.splice(index, 1);\n }\n };\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 // TODO(dmaretskyi): Don't log those.\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, attributes }: CreateContextParams = {}): Context {\n const newCtx = new Context({\n // TODO(dmaretskyi): Optimize to not require allocating a new closure for every 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 attributes,\n });\n const clearDispose = this.onDispose(() => newCtx.dispose());\n newCtx.onDispose(clearDispose);\n return newCtx;\n }\n\n getAttribute(key: string): any {\n if (key in this._attributes) {\n return this._attributes[key];\n }\n if (this._parent !== null) {\n return this._parent.getAttribute(key);\n }\n return undefined;\n }\n}\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { CancelledError } from '@dxos/errors';\n\nimport { Context } from './context';\n\n/**\n * @returns A promise that rejects when the context is disposed.\n */\n// TODO(dmaretskyi): Memory leak.\nexport const rejectOnDispose = (ctx: Context, error = new CancelledError()): Promise<never> =>\n new Promise((resolve, reject) => {\n ctx.onDispose(() => reject(error));\n });\n\n/**\n * Rejects the promise if the context is disposed.\n */\nexport const cancelWithContext = <T>(ctx: Context, promise: Promise<T>): Promise<T> => {\n let clearDispose: () => void;\n return Promise.race([\n promise,\n new Promise<never>((resolve, reject) => {\n // Will be called before .finally() handlers.\n clearDispose = ctx.onDispose(() => reject(new CancelledError()));\n }),\n ]).finally(() => clearDispose?.());\n};\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;ACIA,iBAAoB;AACpB,kBAA+B;;;;;;;;;;;;AAe/B,IAAMA,6BAA6B;AAGnC,IAAaC,UAAN,MAAA,SAAA;EASLC,YAAY,EACVC,UAAU,CAACC,UAAAA;AACT,SAAK,KAAKC,QAAO;AAGjB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CAAA;GACD,EAAES,QAAQ,MAAMF,8CAAAA;AACnB;",
|
|
6
|
+
"names": ["MAX_SAFE_DISPOSE_CALLBACKS", "Context", "constructor", "onError", "error", "dispose", "attributes", "parent", "_disposeCallbacks", "_isDisposed", "_parent", "_onError", "_attributes", "undefined", "disposed", "onDispose", "callback", "log", "catch", "push", "length", "warn", "count", "safeThreshold", "index", "indexOf", "splice", "_disposePromise", "promises", "reverse", "Promise", "all", "then", "raise", "err", "reject", "derive", "newCtx", "clearDispose", "getAttribute", "key", "safeInstanceof", "rejectOnDispose", "ctx", "error", "CancelledError", "Promise", "resolve", "reject", "onDispose", "cancelWithContext", "promise", "clearDispose", "race", "finally"]
|
|
7
7
|
}
|
package/dist/lib/node/meta.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs":{"packages/common/context/src/context.ts":{"bytes":
|
|
1
|
+
{"inputs":{"packages/common/context/src/context.ts":{"bytes":16481,"imports":[{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}]},"packages/common/context/src/promise-utils.ts":{"bytes":2954,"imports":[{"path":"@dxos/errors","kind":"import-statement","external":true}]},"packages/common/context/src/index.ts":{"bytes":563,"imports":[{"path":"packages/common/context/src/context.ts","kind":"import-statement","original":"./context"},{"path":"packages/common/context/src/promise-utils.ts","kind":"import-statement","original":"./promise-utils"}]}},"outputs":{"packages/common/context/dist/lib/node/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":9123},"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},{"path":"@dxos/errors","kind":"require-call","external":true}],"exports":[],"entryPoint":"packages/common/context/src/index.ts","inputs":{"packages/common/context/src/index.ts":{"bytesInOutput":207},"packages/common/context/src/context.ts":{"bytesInOutput":4827},"packages/common/context/src/promise-utils.ts":{"bytesInOutput":501}},"bytes":6643}}}
|
|
@@ -2,13 +2,17 @@ export type ContextErrorHandler = (error: Error) => void;
|
|
|
2
2
|
export type DisposeCallback = () => void | Promise<void>;
|
|
3
3
|
export type CreateContextParams = {
|
|
4
4
|
onError?: ContextErrorHandler;
|
|
5
|
+
attributes?: Record<string, any>;
|
|
6
|
+
parent?: Context;
|
|
5
7
|
};
|
|
6
8
|
export declare class Context {
|
|
7
9
|
private readonly _onError;
|
|
8
10
|
private readonly _disposeCallbacks;
|
|
9
11
|
private _isDisposed;
|
|
10
12
|
private _disposePromise?;
|
|
11
|
-
|
|
13
|
+
private _parent;
|
|
14
|
+
private _attributes;
|
|
15
|
+
constructor({ onError, attributes, parent, }?: CreateContextParams);
|
|
12
16
|
get disposed(): boolean;
|
|
13
17
|
/**
|
|
14
18
|
* Schedules a callback to run when the context is disposed.
|
|
@@ -35,6 +39,7 @@ export declare class Context {
|
|
|
35
39
|
* IF the error handler is not set, the error will dispose the context and cause an unhandled rejection.
|
|
36
40
|
*/
|
|
37
41
|
raise(error: Error): void;
|
|
38
|
-
derive({ onError }?: CreateContextParams): Context;
|
|
42
|
+
derive({ onError, attributes }?: CreateContextParams): Context;
|
|
43
|
+
getAttribute(key: string): any;
|
|
39
44
|
}
|
|
40
45
|
//# sourceMappingURL=context.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../../src/context.ts"],"names":[],"mappings":"AAOA,MAAM,MAAM,mBAAmB,GAAG,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;AAEzD,MAAM,MAAM,eAAe,GAAG,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AAEzD,MAAM,MAAM,mBAAmB,GAAG;IAChC,OAAO,CAAC,EAAE,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../../src/context.ts"],"names":[],"mappings":"AAOA,MAAM,MAAM,mBAAmB,GAAG,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;AAEzD,MAAM,MAAM,eAAe,GAAG,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AAEzD,MAAM,MAAM,mBAAmB,GAAG;IAChC,OAAO,CAAC,EAAE,mBAAmB,CAAC;IAC9B,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IACjC,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB,CAAC;AAOF,qBACa,OAAO;IAClB,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAsB;IAC/C,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAyB;IAC3D,OAAO,CAAC,WAAW,CAAS;IAC5B,OAAO,CAAC,eAAe,CAAC,CAAgB;IACxC,OAAO,CAAC,OAAO,CAAwB;IAEvC,OAAO,CAAC,WAAW,CAAsB;gBAE7B,EACV,OAKC,EACD,UAAe,EACf,MAAM,GACP,GAAE,mBAAwB;IAQ3B,IAAI,QAAQ,YAEX;IAED;;;;;;;;OAQG;IACH,SAAS,CAAC,QAAQ,EAAE,eAAe;IA4BnC;;;;;;;OAOG;IACH,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAuBxB;;;;OAIG;IACH,KAAK,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI;IAezB,MAAM,CAAC,EAAE,OAAO,EAAE,UAAU,EAAE,GAAE,mBAAwB,GAAG,OAAO;IAqBlE,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG;CAS/B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/context",
|
|
3
|
-
"version": "0.1.56-main.
|
|
3
|
+
"version": "0.1.56-main.c19b7cd",
|
|
4
4
|
"description": "Async utils.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -16,9 +16,9 @@
|
|
|
16
16
|
"src"
|
|
17
17
|
],
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@dxos/errors": "0.1.56-main.
|
|
20
|
-
"@dxos/log": "0.1.56-main.
|
|
21
|
-
"@dxos/util": "0.1.56-main.
|
|
19
|
+
"@dxos/errors": "0.1.56-main.c19b7cd",
|
|
20
|
+
"@dxos/log": "0.1.56-main.c19b7cd",
|
|
21
|
+
"@dxos/util": "0.1.56-main.c19b7cd"
|
|
22
22
|
},
|
|
23
23
|
"publishConfig": {
|
|
24
24
|
"access": "public"
|
package/src/context.ts
CHANGED
|
@@ -11,6 +11,8 @@ export type DisposeCallback = () => void | Promise<void>;
|
|
|
11
11
|
|
|
12
12
|
export type CreateContextParams = {
|
|
13
13
|
onError?: ContextErrorHandler;
|
|
14
|
+
attributes?: Record<string, any>;
|
|
15
|
+
parent?: Context;
|
|
14
16
|
};
|
|
15
17
|
|
|
16
18
|
/**
|
|
@@ -24,6 +26,9 @@ export class Context {
|
|
|
24
26
|
private readonly _disposeCallbacks: DisposeCallback[] = [];
|
|
25
27
|
private _isDisposed = false;
|
|
26
28
|
private _disposePromise?: Promise<void>;
|
|
29
|
+
private _parent: Context | null = null;
|
|
30
|
+
|
|
31
|
+
private _attributes: Record<string, any>;
|
|
27
32
|
|
|
28
33
|
constructor({
|
|
29
34
|
onError = (error) => {
|
|
@@ -32,8 +37,14 @@ export class Context {
|
|
|
32
37
|
// Will generate an unhandled rejection.
|
|
33
38
|
throw error;
|
|
34
39
|
},
|
|
40
|
+
attributes = {},
|
|
41
|
+
parent,
|
|
35
42
|
}: CreateContextParams = {}) {
|
|
36
43
|
this._onError = onError;
|
|
44
|
+
this._attributes = attributes;
|
|
45
|
+
if (parent !== undefined) {
|
|
46
|
+
this._parent = parent;
|
|
47
|
+
}
|
|
37
48
|
}
|
|
38
49
|
|
|
39
50
|
get disposed() {
|
|
@@ -128,8 +139,9 @@ export class Context {
|
|
|
128
139
|
}
|
|
129
140
|
}
|
|
130
141
|
|
|
131
|
-
derive({ onError }: CreateContextParams = {}): Context {
|
|
142
|
+
derive({ onError, attributes }: CreateContextParams = {}): Context {
|
|
132
143
|
const newCtx = new Context({
|
|
144
|
+
// TODO(dmaretskyi): Optimize to not require allocating a new closure for every context.
|
|
133
145
|
onError: async (error) => {
|
|
134
146
|
if (!onError) {
|
|
135
147
|
this.raise(error);
|
|
@@ -141,9 +153,20 @@ export class Context {
|
|
|
141
153
|
}
|
|
142
154
|
}
|
|
143
155
|
},
|
|
156
|
+
attributes,
|
|
144
157
|
});
|
|
145
158
|
const clearDispose = this.onDispose(() => newCtx.dispose());
|
|
146
159
|
newCtx.onDispose(clearDispose);
|
|
147
160
|
return newCtx;
|
|
148
161
|
}
|
|
162
|
+
|
|
163
|
+
getAttribute(key: string): any {
|
|
164
|
+
if (key in this._attributes) {
|
|
165
|
+
return this._attributes[key];
|
|
166
|
+
}
|
|
167
|
+
if (this._parent !== null) {
|
|
168
|
+
return this._parent.getAttribute(key);
|
|
169
|
+
}
|
|
170
|
+
return undefined;
|
|
171
|
+
}
|
|
149
172
|
}
|