@dxos/context 0.1.57-main.e87098f → 0.1.57

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.
@@ -11,7 +11,7 @@ function _ts_decorate(decorators, target, key, desc) {
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
+ var __dxlog_file = "/mnt/ramdisk/work/packages/common/context/src/context.ts";
15
15
  var MAX_SAFE_DISPOSE_CALLBACKS = 300;
16
16
  var Context = class Context1 {
17
17
  constructor({ onError = (error) => {
@@ -22,6 +22,7 @@ var Context = class Context1 {
22
22
  this._isDisposed = false;
23
23
  this._disposePromise = void 0;
24
24
  this._parent = null;
25
+ this.maxSafeDisposeCallbacks = MAX_SAFE_DISPOSE_CALLBACKS;
25
26
  this._onError = onError;
26
27
  this._attributes = attributes;
27
28
  if (parent !== void 0) {
@@ -48,7 +49,7 @@ var Context = class Context1 {
48
49
  } catch (error) {
49
50
  log.catch(error, void 0, {
50
51
  F: __dxlog_file,
51
- L: 70,
52
+ L: 72,
52
53
  S: this,
53
54
  C: (f, a) => f(...a)
54
55
  });
@@ -56,13 +57,13 @@ var Context = class Context1 {
56
57
  })();
57
58
  }
58
59
  this._disposeCallbacks.push(callback);
59
- if (this._disposeCallbacks.length > MAX_SAFE_DISPOSE_CALLBACKS) {
60
+ if (this._disposeCallbacks.length > this.maxSafeDisposeCallbacks) {
60
61
  log.warn("Context has a large number of dispose callbacks. This might be a memory leak.", {
61
62
  count: this._disposeCallbacks.length,
62
- safeThreshold: MAX_SAFE_DISPOSE_CALLBACKS
63
+ safeThreshold: this.maxSafeDisposeCallbacks
63
64
  }, {
64
65
  F: __dxlog_file,
65
- L: 77,
66
+ L: 79,
66
67
  S: this,
67
68
  C: (f, a) => f(...a)
68
69
  });
@@ -95,7 +96,7 @@ var Context = class Context1 {
95
96
  } catch (error) {
96
97
  log.catch(error, void 0, {
97
98
  F: __dxlog_file,
98
- L: 112,
99
+ L: 114,
99
100
  S: this,
100
101
  C: (f, a) => f(...a)
101
102
  });
@@ -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 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> = undefined;\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;AACdC,2BAAkCC;AAClCC,mBAA0B;AAchC,SAAKC,WAAWV;AAChB,SAAKW,cAAcR;AACnB,QAAIC,WAAWI,QAAW;AACxB,WAAKC,UAAUL;IACjB;EACF;EAEA,IAAIQ,WAAW;AACb,WAAO,KAAKN;EACd;;;;;;;;;;EAWAO,UAAUC,UAA2B;AACnC,QAAI,KAAKR,aAAa;AAEpB,YAAM,YAAA;AACJ,YAAI;AACF,gBAAMQ,SAAAA;QACR,SAASb,OAAY;AACnBN,cAAIoB,MAAMd,OAAAA,QAAAA;;;;;;QACZ;MACF,GAAA;IACF;AAEA,SAAKI,kBAAkBW,KAAKF,QAAAA;AAC5B,QAAI,KAAKT,kBAAkBY,SAASpB,4BAA4B;AAC9DF,UAAIuB,KAAK,iFAAiF;QAC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AOD;EACT;EAEAE,aAAaC,KAAkB;AAC7B,QAAIA,OAAO,KAAKzB,aAAa;AAC3B,aAAO,KAAKA,YAAYyB,GAAAA;IAC1B;AACA,QAAI,KAAK3B,YAAY,MAAM;AACzB,aAAO,KAAKA,QAAQ0B,aAAaC,GAAAA;IACnC;AACA,WAAO5B;EACT;AACF;AApJaV,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,eAAAA,CAAAA;AACnB;",
6
- "names": ["log", "safeInstanceof", "MAX_SAFE_DISPOSE_CALLBACKS", "Context", "constructor", "onError", "error", "dispose", "attributes", "parent", "_disposeCallbacks", "_isDisposed", "_disposePromise", "undefined", "_parent", "_onError", "_attributes", "disposed", "onDispose", "callback", "catch", "push", "length", "warn", "count", "safeThreshold", "index", "indexOf", "splice", "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"]
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> = undefined;\n private _parent: Context | null = null;\n\n private _attributes: Record<string, any>;\n\n public maxSafeDisposeCallbacks = MAX_SAFE_DISPOSE_CALLBACKS;\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 > this.maxSafeDisposeCallbacks) {\n log.warn('Context has a large number of dispose callbacks. This might be a memory leak.', {\n count: this._disposeCallbacks.length,\n safeThreshold: this.maxSafeDisposeCallbacks,\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;EAWLC,YAAY,EACVC,UAAU,CAACC,UAAAA;AACT,SAAK,KAAKC,QAAO;AAGjB,UAAMD;EACR,GACAE,aAAa,CAAC,GACdC,OAAM,IACiB,CAAC,GAAG;AAlBZC,6BAAuC,CAAA;AAChDC,uBAAc;AACdC,2BAAkCC;AAClCC,mBAA0B;AAI3BC,mCAA0Bb;AAY/B,SAAKc,WAAWX;AAChB,SAAKY,cAAcT;AACnB,QAAIC,WAAWI,QAAW;AACxB,WAAKC,UAAUL;IACjB;EACF;EAEA,IAAIS,WAAW;AACb,WAAO,KAAKP;EACd;;;;;;;;;;EAWAQ,UAAUC,UAA2B;AACnC,QAAI,KAAKT,aAAa;AAEpB,YAAM,YAAA;AACJ,YAAI;AACF,gBAAMS,SAAAA;QACR,SAASd,OAAY;AACnBN,cAAIqB,MAAMf,OAAAA,QAAAA;;;;;;QACZ;MACF,GAAA;IACF;AAEA,SAAKI,kBAAkBY,KAAKF,QAAAA;AAC5B,QAAI,KAAKV,kBAAkBa,SAAS,KAAKR,yBAAyB;AAChEf,UAAIwB,KAAK,iFAAiF;QACxFC,OAAO,KAAKf,kBAAkBa;QAC9BG,eAAe,KAAKX;MACtB,GAAA;;;;;;IACF;AAEA,WAAO,MAAA;AACL,YAAMY,QAAQ,KAAKjB,kBAAkBkB,QAAQR,QAAAA;AAC7C,UAAIO,UAAU,IAAI;AAChB,aAAKjB,kBAAkBmB,OAAOF,OAAO,CAAA;MACvC;IACF;EACF;;;;;;;;;EAUApB,UAAyB;AACvB,QAAI,KAAKK,iBAAiB;AACxB,aAAO,KAAKA;IACd;AACA,SA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BAAkB,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,eAAAA,CAAAA;AACnB;",
6
+ "names": ["log", "safeInstanceof", "MAX_SAFE_DISPOSE_CALLBACKS", "Context", "constructor", "onError", "error", "dispose", "attributes", "parent", "_disposeCallbacks", "_isDisposed", "_disposePromise", "undefined", "_parent", "maxSafeDisposeCallbacks", "_onError", "_attributes", "disposed", "onDispose", "callback", "catch", "push", "length", "warn", "count", "safeThreshold", "index", "indexOf", "splice", "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":16517,"imports":[{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}],"format":"esm"},"packages/common/context/src/promise-utils.ts":{"bytes":2954,"imports":[{"path":"@dxos/errors","kind":"import-statement","external":true}],"format":"esm"},"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"}],"format":"esm"}},"outputs":{"packages/common/context/dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":9018},"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":4806},"packages/common/context/src/index.ts":{"bytesInOutput":0},"packages/common/context/src/promise-utils.ts":{"bytesInOutput":445}},"bytes":5439}}}
1
+ {"inputs":{"packages/common/context/src/context.ts":{"bytes":16765,"imports":[{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}],"format":"esm"},"packages/common/context/src/promise-utils.ts":{"bytes":2944,"imports":[{"path":"@dxos/errors","kind":"import-statement","external":true}],"format":"esm"},"packages/common/context/src/index.ts":{"bytes":553,"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"}],"format":"esm"}},"outputs":{"packages/common/context/dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":9137},"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":4863},"packages/common/context/src/index.ts":{"bytesInOutput":0},"packages/common/context/src/promise-utils.ts":{"bytesInOutput":445}},"bytes":5496}}}
@@ -39,7 +39,7 @@ function _ts_decorate(decorators, target, key, desc) {
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
+ var __dxlog_file = "/mnt/ramdisk/work/packages/common/context/src/context.ts";
43
43
  var MAX_SAFE_DISPOSE_CALLBACKS = 300;
44
44
  var Context = class Context1 {
45
45
  constructor({ onError = (error) => {
@@ -50,6 +50,7 @@ var Context = class Context1 {
50
50
  this._isDisposed = false;
51
51
  this._disposePromise = void 0;
52
52
  this._parent = null;
53
+ this.maxSafeDisposeCallbacks = MAX_SAFE_DISPOSE_CALLBACKS;
53
54
  this._onError = onError;
54
55
  this._attributes = attributes;
55
56
  if (parent !== void 0) {
@@ -76,7 +77,7 @@ var Context = class Context1 {
76
77
  } catch (error) {
77
78
  import_log.log.catch(error, void 0, {
78
79
  F: __dxlog_file,
79
- L: 70,
80
+ L: 72,
80
81
  S: this,
81
82
  C: (f, a) => f(...a)
82
83
  });
@@ -84,13 +85,13 @@ var Context = class Context1 {
84
85
  })();
85
86
  }
86
87
  this._disposeCallbacks.push(callback);
87
- if (this._disposeCallbacks.length > MAX_SAFE_DISPOSE_CALLBACKS) {
88
+ if (this._disposeCallbacks.length > this.maxSafeDisposeCallbacks) {
88
89
  import_log.log.warn("Context has a large number of dispose callbacks. This might be a memory leak.", {
89
90
  count: this._disposeCallbacks.length,
90
- safeThreshold: MAX_SAFE_DISPOSE_CALLBACKS
91
+ safeThreshold: this.maxSafeDisposeCallbacks
91
92
  }, {
92
93
  F: __dxlog_file,
93
- L: 77,
94
+ L: 79,
94
95
  S: this,
95
96
  C: (f, a) => f(...a)
96
97
  });
@@ -123,7 +124,7 @@ var Context = class Context1 {
123
124
  } catch (error) {
124
125
  import_log.log.catch(error, void 0, {
125
126
  F: __dxlog_file,
126
- L: 112,
127
+ L: 114,
127
128
  S: this,
128
129
  C: (f, a) => f(...a)
129
130
  });
@@ -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 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> = undefined;\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,UAAMD;EACR,GACAE,aAAa,CAAC,GACdC,OAAM,IACiB,CAAC,GAAG;AAhBZC,6BAAuC,CAAA;AAChDC,uBAAc;AACdC,2BAAkCC;AAClCC,mBAA0B;AAchC,SAAKC,WAAWV;AAChB,SAAKW,cAAcR;AACnB,QAAIC,WAAWI,QAAW;AACxB,WAAKC,UAAUL;IACjB;EACF;EAEA,IAAIQ,WAAW;AACb,WAAO,KAAKN;EACd;;;;;;;;;;EAWAO,UAAUC,UAA2B;AACnC,QAAI,KAAKR,aAAa;AAEpB,YAAM,YAAA;AACJ,YAAI;AACF,gBAAMQ,SAAAA;QACR,SAASb,OAAY;AACnBc,yBAAIC,MAAMf,OAAAA,QAAAA;;;;;;QACZ;MACF,GAAA;IACF;AAEA,SAAKI,kBAAkBY,KAAKH,QAAAA;AAC5B,QAAI,KAAKT,kBAAkBa,SAASrB,4BAA4B;AAC9DkB,qBAAII,KAAK,iFAAiF;QACxFC,OAAO,KAAKf,kBAAkBa;QAC9BG,eAAexB;MACjB,GAAA;;;;;;IACF;AAEA,WAAO,MAAA;AACL,YAAMyB,QAAQ,KAAKjB,kBAAkBkB,QAAQT,QAAAA;AAC7C,UAAIQ,UAAU,IAAI;AAChB,aAAKjB,kBAAkBmB,OAAOF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AAO7B;EACT;AACF;AApJaV,UAAAA,aAAAA;MADZwC,4BAAe,SAAA;GACHxC,OAAAA;;;ACnBb,oBAA+B;AAQxB,IAAMyC,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,eAAAA,CAAAA;AACnB;",
6
- "names": ["MAX_SAFE_DISPOSE_CALLBACKS", "Context", "constructor", "onError", "error", "dispose", "attributes", "parent", "_disposeCallbacks", "_isDisposed", "_disposePromise", "undefined", "_parent", "_onError", "_attributes", "disposed", "onDispose", "callback", "log", "catch", "push", "length", "warn", "count", "safeThreshold", "index", "indexOf", "splice", "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"]
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> = undefined;\n private _parent: Context | null = null;\n\n private _attributes: Record<string, any>;\n\n public maxSafeDisposeCallbacks = MAX_SAFE_DISPOSE_CALLBACKS;\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 > this.maxSafeDisposeCallbacks) {\n log.warn('Context has a large number of dispose callbacks. This might be a memory leak.', {\n count: this._disposeCallbacks.length,\n safeThreshold: this.maxSafeDisposeCallbacks,\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": ";;;;;;;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OJ,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,eAAAA,CAAAA;AACnB;",
6
+ "names": ["MAX_SAFE_DISPOSE_CALLBACKS", "Context", "constructor", "onError", "error", "dispose", "attributes", "parent", "_disposeCallbacks", "_isDisposed", "_disposePromise", "undefined", "_parent", "maxSafeDisposeCallbacks", "_onError", "_attributes", "disposed", "onDispose", "callback", "log", "catch", "push", "length", "warn", "count", "safeThreshold", "index", "indexOf", "splice", "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
  }
@@ -1 +1 @@
1
- {"inputs":{"packages/common/context/src/context.ts":{"bytes":16517,"imports":[{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}],"format":"esm"},"packages/common/context/src/promise-utils.ts":{"bytes":2954,"imports":[{"path":"@dxos/errors","kind":"import-statement","external":true}],"format":"esm"},"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"}],"format":"esm"}},"outputs":{"packages/common/context/dist/lib/node/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":9151},"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":4858},"packages/common/context/src/promise-utils.ts":{"bytesInOutput":471}},"bytes":6644}}}
1
+ {"inputs":{"packages/common/context/src/context.ts":{"bytes":16765,"imports":[{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}],"format":"esm"},"packages/common/context/src/promise-utils.ts":{"bytes":2944,"imports":[{"path":"@dxos/errors","kind":"import-statement","external":true}],"format":"esm"},"packages/common/context/src/index.ts":{"bytes":553,"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"}],"format":"esm"}},"outputs":{"packages/common/context/dist/lib/node/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":9272},"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":4915},"packages/common/context/src/promise-utils.ts":{"bytesInOutput":471}},"bytes":6701}}}
@@ -12,6 +12,7 @@ export declare class Context {
12
12
  private _disposePromise?;
13
13
  private _parent;
14
14
  private _attributes;
15
+ maxSafeDisposeCallbacks: number;
15
16
  constructor({ onError, attributes, parent, }?: CreateContextParams);
16
17
  get disposed(): boolean;
17
18
  /**
@@ -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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AAM,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,CAA4B;IACpD,OAAO,CAAC,OAAO,CAAwB;IAEvC,OAAO,CAAC,WAAW,CAAsB;IAElC,uBAAuB,SAA8B;gBAEhD,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.57-main.e87098f",
3
+ "version": "0.1.57",
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.57-main.e87098f",
20
- "@dxos/log": "0.1.57-main.e87098f",
21
- "@dxos/util": "0.1.57-main.e87098f"
19
+ "@dxos/errors": "0.1.57",
20
+ "@dxos/log": "0.1.57",
21
+ "@dxos/util": "0.1.57"
22
22
  },
23
23
  "publishConfig": {
24
24
  "access": "public"
package/src/context.ts CHANGED
@@ -30,6 +30,8 @@ export class Context {
30
30
 
31
31
  private _attributes: Record<string, any>;
32
32
 
33
+ public maxSafeDisposeCallbacks = MAX_SAFE_DISPOSE_CALLBACKS;
34
+
33
35
  constructor({
34
36
  onError = (error) => {
35
37
  void this.dispose();
@@ -73,10 +75,10 @@ export class Context {
73
75
  }
74
76
 
75
77
  this._disposeCallbacks.push(callback);
76
- if (this._disposeCallbacks.length > MAX_SAFE_DISPOSE_CALLBACKS) {
78
+ if (this._disposeCallbacks.length > this.maxSafeDisposeCallbacks) {
77
79
  log.warn('Context has a large number of dispose callbacks. This might be a memory leak.', {
78
80
  count: this._disposeCallbacks.length,
79
- safeThreshold: MAX_SAFE_DISPOSE_CALLBACKS,
81
+ safeThreshold: this.maxSafeDisposeCallbacks,
80
82
  });
81
83
  }
82
84