@dxos/context 0.1.53 → 0.1.54-next.b67d4c9
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.
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// packages/common/context/src/context.ts
|
|
2
2
|
import { log } from "@dxos/log";
|
|
3
3
|
import { safeInstanceof } from "@dxos/util";
|
|
4
|
-
|
|
4
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
5
5
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
6
6
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
7
7
|
r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -10,7 +10,8 @@ var __decorate = function(decorators, target, key, desc) {
|
|
|
10
10
|
if (d = decorators[i])
|
|
11
11
|
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
12
12
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
13
|
-
}
|
|
13
|
+
}
|
|
14
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/common/context/src/context.ts";
|
|
14
15
|
var MAX_SAFE_DISPOSE_CALLBACKS = 300;
|
|
15
16
|
var Context = class Context1 {
|
|
16
17
|
constructor({ onError = (error) => {
|
|
@@ -39,11 +40,11 @@ var Context = class Context1 {
|
|
|
39
40
|
try {
|
|
40
41
|
await callback();
|
|
41
42
|
} catch (error) {
|
|
42
|
-
log.catch(error,
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
43
|
+
log.catch(error, void 0, {
|
|
44
|
+
F: __dxlog_file,
|
|
45
|
+
L: 59,
|
|
46
|
+
S: this,
|
|
47
|
+
C: (f, a) => f(...a)
|
|
47
48
|
});
|
|
48
49
|
}
|
|
49
50
|
})();
|
|
@@ -54,10 +55,10 @@ var Context = class Context1 {
|
|
|
54
55
|
count: this._disposeCallbacks.length,
|
|
55
56
|
safeThreshold: MAX_SAFE_DISPOSE_CALLBACKS
|
|
56
57
|
}, {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
58
|
+
F: __dxlog_file,
|
|
59
|
+
L: 66,
|
|
60
|
+
S: this,
|
|
61
|
+
C: (f, a) => f(...a)
|
|
61
62
|
});
|
|
62
63
|
}
|
|
63
64
|
return () => {
|
|
@@ -86,11 +87,11 @@ var Context = class Context1 {
|
|
|
86
87
|
try {
|
|
87
88
|
await callback();
|
|
88
89
|
} catch (error) {
|
|
89
|
-
log.catch(error,
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
90
|
+
log.catch(error, void 0, {
|
|
91
|
+
F: __dxlog_file,
|
|
92
|
+
L: 101,
|
|
93
|
+
S: this,
|
|
94
|
+
C: (f, a) => f(...a)
|
|
94
95
|
});
|
|
95
96
|
}
|
|
96
97
|
})());
|
|
@@ -133,7 +134,7 @@ var Context = class Context1 {
|
|
|
133
134
|
return newCtx;
|
|
134
135
|
}
|
|
135
136
|
};
|
|
136
|
-
Context =
|
|
137
|
+
Context = _ts_decorate([
|
|
137
138
|
safeInstanceof("Context")
|
|
138
139
|
], Context);
|
|
139
140
|
|
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/context.ts", "../../../src/promise-utils.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/**\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", "Promise", "resolve", "reject", "onDispose", "cancelWithContext", "promise", "clearDispose", "race", "finally"]
|
|
5
|
+
"mappings":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,IAAIH,QAAAA,EAAUI,KAAK,MAAA;IAAO,CAAA;EACnE;;;;;;EAOAC,MAAMvB,OAAoB;AACxB,QAAI,KAAKG,aAAa;AAGpB;IACF;AAEA,QAAI;AACF,WAAKC,SAASJ,KAAAA;IAChB,SAASwB,KAAP;AAEA,WAAKJ,QAAQK,OAAOD,GAAAA;IACtB;EACF;EAEAE,OAAO,EAAE3B,QAAO,IAA0B,CAAC,GAAY;AACrD,UAAM4B,SAAS,IAAI9B,QAAQ;MACzBE,SAAS,OAAOC,UAAAA;AACd,YAAI,CAACD,SAAS;AACZ,eAAKwB,MAAMvB,KAAAA;QACb,OAAO;AACL,cAAI;AACF,kBAAMD,QAAQC,KAAAA;UAChB,SAAQ,GAAN;AACA,iBAAKuB,MAAMvB,KAAAA;UACb;QACF;MACF;IACF,CAAA;AACA,UAAM4B,eAAe,KAAKtB,UAAU,MAAMqB,OAAO1B,QAAO,CAAA;AACxD0B,WAAOrB,UAAUsB,YAAAA;AACjB,WAAOD;EACT;AACF;AA/Ha9B,UAAAA,aAAAA;EADZF,eAAe,SAAA;GACHE,OAAAA;;;ACjBb,SAASgC,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", "_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"]
|
|
7
7
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs":{"packages/common/context/src/context.ts":{"bytes":
|
|
1
|
+
{"inputs":{"packages/common/context/src/context.ts":{"bytes":14421,"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":7964},"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":4295},"packages/common/context/src/index.ts":{"bytesInOutput":0},"packages/common/context/src/promise-utils.ts":{"bytesInOutput":475}},"bytes":4958}}}
|
package/dist/lib/node/index.cjs
CHANGED
|
@@ -29,7 +29,7 @@ module.exports = __toCommonJS(src_exports);
|
|
|
29
29
|
// packages/common/context/src/context.ts
|
|
30
30
|
var import_log = require("@dxos/log");
|
|
31
31
|
var import_util = require("@dxos/util");
|
|
32
|
-
|
|
32
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
33
33
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
34
34
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
35
35
|
r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -38,7 +38,8 @@ var __decorate = function(decorators, target, key, desc) {
|
|
|
38
38
|
if (d = decorators[i])
|
|
39
39
|
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
40
40
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
41
|
-
}
|
|
41
|
+
}
|
|
42
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/common/context/src/context.ts";
|
|
42
43
|
var MAX_SAFE_DISPOSE_CALLBACKS = 300;
|
|
43
44
|
var Context = class Context1 {
|
|
44
45
|
constructor({ onError = (error) => {
|
|
@@ -67,11 +68,11 @@ var Context = class Context1 {
|
|
|
67
68
|
try {
|
|
68
69
|
await callback();
|
|
69
70
|
} catch (error) {
|
|
70
|
-
import_log.log.catch(error,
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
71
|
+
import_log.log.catch(error, void 0, {
|
|
72
|
+
F: __dxlog_file,
|
|
73
|
+
L: 59,
|
|
74
|
+
S: this,
|
|
75
|
+
C: (f, a) => f(...a)
|
|
75
76
|
});
|
|
76
77
|
}
|
|
77
78
|
})();
|
|
@@ -82,10 +83,10 @@ var Context = class Context1 {
|
|
|
82
83
|
count: this._disposeCallbacks.length,
|
|
83
84
|
safeThreshold: MAX_SAFE_DISPOSE_CALLBACKS
|
|
84
85
|
}, {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
86
|
+
F: __dxlog_file,
|
|
87
|
+
L: 66,
|
|
88
|
+
S: this,
|
|
89
|
+
C: (f, a) => f(...a)
|
|
89
90
|
});
|
|
90
91
|
}
|
|
91
92
|
return () => {
|
|
@@ -114,11 +115,11 @@ var Context = class Context1 {
|
|
|
114
115
|
try {
|
|
115
116
|
await callback();
|
|
116
117
|
} catch (error) {
|
|
117
|
-
import_log.log.catch(error,
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
118
|
+
import_log.log.catch(error, void 0, {
|
|
119
|
+
F: __dxlog_file,
|
|
120
|
+
L: 101,
|
|
121
|
+
S: this,
|
|
122
|
+
C: (f, a) => f(...a)
|
|
122
123
|
});
|
|
123
124
|
}
|
|
124
125
|
})());
|
|
@@ -161,7 +162,7 @@ var Context = class Context1 {
|
|
|
161
162
|
return newCtx;
|
|
162
163
|
}
|
|
163
164
|
};
|
|
164
|
-
Context =
|
|
165
|
+
Context = _ts_decorate([
|
|
165
166
|
(0, import_util.safeInstanceof)("Context")
|
|
166
167
|
], Context);
|
|
167
168
|
|
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/index.ts", "../../../src/context.ts", "../../../src/promise-utils.ts"],
|
|
4
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
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;ACIA,iBAAoB;AACpB,kBAA+B;;;;;;;;;;;;AAa/B,IAAMA,6BAA6B;AAGnC,IAAaC,UAAN,MAAA,SAAA;EAMLC,YAAY,EACVC,UAAU,CAACC,UAAAA;AACT,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;;;;;;;;;;EAWAG,UAAUC,UAA2B;AACnC,QAAI,KAAKJ,aAAa;AAEpB,YAAM,YAAA;AACJ,YAAI;AACF,gBAAMI,SAAAA;QACR,SAASP,OAAP;AACAQ,y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;AACA,iBAAKwB,MAAMxB,KAAAA;UACb;QACF;MACF;IACF,CAAA;AACA,UAAM6B,eAAe,KAAKvB,UAAU,MAAMsB,OAAO3B,QAAO,CAAA;AACxD2B,WAAOtB,UAAUuB,YAAAA;AACjB,WAAOD;EACT;AACF;AA/Ha/B,UAAAA,aAAAA;MADZiC,4BAAe,SAAA;GACHjC,OAAAA;;;ACjBb,oBAA+B;AAQxB,IAAMkC,kBAAkB,CAACC,KAAcC,QAAQ,IAAIC,6BAAAA,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,6BAAAA,CAAAA,CAAAA;IAChD,CAAA;GACD,EAAES,QAAQ,MAAMF,8CAAAA;AACnB;",
|
|
6
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"]
|
|
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":14421,"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":8099},"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":4347},"packages/common/context/src/promise-utils.ts":{"bytesInOutput":501}},"bytes":6163}}}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/context",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.54-next.b67d4c9",
|
|
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.
|
|
20
|
-
"@dxos/log": "0.1.
|
|
21
|
-
"@dxos/util": "0.1.
|
|
19
|
+
"@dxos/errors": "0.1.54-next.b67d4c9",
|
|
20
|
+
"@dxos/log": "0.1.54-next.b67d4c9",
|
|
21
|
+
"@dxos/util": "0.1.54-next.b67d4c9"
|
|
22
22
|
},
|
|
23
23
|
"publishConfig": {
|
|
24
24
|
"access": "public"
|