@dxos/context 0.1.41 → 0.1.45-next.45667d4

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.
@@ -86,12 +86,6 @@ var Context = class Context1 {
86
86
  */
87
87
  raise(error) {
88
88
  if (this._isDisposed) {
89
- log.warn("Error in disposed context", error, {
90
- file: "context.ts",
91
- line: 98,
92
- scope: this,
93
- callSite: (f, a) => f(...a)
94
- });
95
89
  return;
96
90
  }
97
91
  try {
@@ -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@safeInstanceof('Context')\nexport class Context {\n private readonly _onError: ContextErrorHandler;\n private readonly _disposeCallbacks: DisposeCallback[] = [];\n private _isDisposed = false;\n private _disposePromise?: Promise<void>;\n\n constructor({\n onError = (error) => {\n void this.dispose();\n\n // Will generate an unhandled rejection.\n throw error;\n }\n }: CreateContextParams = {}) {\n this._onError = onError;\n }\n\n get disposed() {\n return this._isDisposed;\n }\n\n /**\n * Schedules a callback to run when the context is disposed.\n * May be async, in this case the disposer might choose to wait for all resource to released.\n * Throwing an error inside the callback will result in the error being logged, but not re-thrown.\n *\n * NOTE: Will call the callback immediately if the context is already disposed.\n */\n onDispose(callback: DisposeCallback) {\n if (this._isDisposed) {\n // Call the callback immediately if the context is already disposed.\n void (async () => {\n try {\n await callback();\n } catch (error: any) {\n log.catch(error);\n }\n })();\n }\n\n this._disposeCallbacks.push(callback);\n }\n\n /**\n * Runs all dispose callbacks.\n * Sync callbacks are run in the reverse order they were added.\n * Async callbacks are run in parallel.\n * This function never throws.\n * It is safe to ignore the returned promise if the caller does not wish to wait for callbacks to complete.\n * Disposing context means that onDispose will throw an error and any errors raised will be logged and not propagated.\n */\n dispose(): Promise<void> {\n if (this._disposePromise) {\n return this._disposePromise;\n }\n this._isDisposed = true;\n\n const promises = [];\n for (const callback of this._disposeCallbacks.reverse()) {\n promises.push(\n (async () => {\n try {\n await callback();\n } catch (error: any) {\n log.catch(error);\n }\n })()\n );\n }\n this._disposeCallbacks.length = 0;\n\n return (this._disposePromise = Promise.all(promises).then(() => {}));\n }\n\n /**\n * Raise the error inside the context.\n * The error will be propagated to the error handler.\n * IF the error handler is not set, the error will dispose the context and cause an unhandled rejection.\n */\n raise(error: Error): void {\n if (this._isDisposed) {\n log.warn('Error in disposed context', error);\n return;\n }\n\n try {\n this._onError(error);\n } catch (err) {\n // Generate an unhandled rejection and stop the error propagation.\n void Promise.reject(err);\n }\n }\n\n derive({ onError }: CreateContextParams = {}): Context {\n const newCtx = new Context({\n onError: async (error) => {\n if (!onError) {\n this.raise(error);\n } else {\n try {\n await onError(error);\n } catch {\n this.raise(error);\n }\n }\n }\n });\n this.onDispose(() => newCtx.dispose());\n return newCtx;\n }\n}\n", "//\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 */\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 return Promise.race([promise, rejectOnDispose(ctx)]);\n};\n"],
5
- "mappings": ";AAIA,SAASA,WAAW;AACpB,SAASC,sBAAsB;AAD/B,IAAA,aAAA,SAAA,YAAA,QAAA,KAAA,MAAA;;;;;;;;;;AAYA,IAAaC,UAAN,MAAA,SAAA;EAMLC,YAAY,EACVC,UAAU,CAACC,UAAU;AACnB,SAAK,KAAKC,QAA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F,kBAAMD,QAAQC,KAAAA;UAChB,SAAQ,GAAN;AACA,iBAAKiB,MAAMjB,KAAAA;UACb;QACF;MACF;IACF,CAAA;AACA,SAAKM,UAAU,MAAMgB,OAAOrB,QAAO,CAAA;AACnC,WAAOqB;EACT;AACF;AA9GazB,UAAAA,WAAAA;EADZD,eAAe,SAAA;GACHC,OAAAA;;;ACZb,SAAS0B,sBAAsB;AAOxB,IAAMC,kBAAkB,CAACC,KAAcC,QAAQ,IAAIH,eAAAA,MACxD,IAAII,QAAQ,CAACC,SAASC,WAAW;AAC/BJ,MAAIK,UAAU,MAAMD,OAAOH,KAAAA,CAAAA;AAC7B,CAAA;AAKK,IAAMK,oBAAoB,CAAIN,KAAcO,YAAoC;AACrF,SAAOL,QAAQM,KAAK;IAACD;IAASR,gBAAgBC,GAAAA;GAAK;AACrD;",
6
- "names": ["log", "safeInstanceof", "Context", "constructor", "onError", "error", "dispose", "_disposeCallbacks", "_isDisposed", "_onError", "disposed", "onDispose", "callback", "catch", "push", "_disposePromise", "promises", "reverse", "length", "Promise", "all", "then", "raise", "warn", "err", "reject", "derive", "newCtx", "CancelledError", "rejectOnDispose", "ctx", "error", "Promise", "resolve", "reject", "onDispose", "cancelWithContext", "promise", "race"]
4
+ "sourcesContent": ["//\n// Copyright 2022 DXOS.org\n//\n\nimport { log } from '@dxos/log';\nimport { safeInstanceof } from '@dxos/util';\n\nexport type ContextErrorHandler = (error: Error) => void;\n\nexport type DisposeCallback = () => void | Promise<void>;\n\nexport type CreateContextParams = {\n onError?: ContextErrorHandler;\n};\n\n@safeInstanceof('Context')\nexport class Context {\n private readonly _onError: ContextErrorHandler;\n private readonly _disposeCallbacks: DisposeCallback[] = [];\n private _isDisposed = false;\n private _disposePromise?: Promise<void>;\n\n constructor({\n onError = (error) => {\n void this.dispose();\n\n // Will generate an unhandled rejection.\n throw error;\n },\n }: CreateContextParams = {}) {\n this._onError = onError;\n }\n\n get disposed() {\n return this._isDisposed;\n }\n\n /**\n * Schedules a callback to run when the context is disposed.\n * May be async, in this case the disposer might choose to wait for all resource to released.\n * Throwing an error inside the callback will result in the error being logged, but not re-thrown.\n *\n * NOTE: Will call the callback immediately if the context is already disposed.\n */\n onDispose(callback: DisposeCallback) {\n if (this._isDisposed) {\n // Call the callback immediately if the context is already disposed.\n void (async () => {\n try {\n await callback();\n } catch (error: any) {\n log.catch(error);\n }\n })();\n }\n\n this._disposeCallbacks.push(callback);\n }\n\n /**\n * Runs all dispose callbacks.\n * Sync callbacks are run in the reverse order they were added.\n * Async callbacks are run in parallel.\n * This function never throws.\n * It is safe to ignore the returned promise if the caller does not wish to wait for callbacks to complete.\n * Disposing context means that onDispose will throw an error and any errors raised will be logged and not propagated.\n */\n dispose(): Promise<void> {\n if (this._disposePromise) {\n return this._disposePromise;\n }\n this._isDisposed = true;\n\n const promises = [];\n for (const callback of this._disposeCallbacks.reverse()) {\n promises.push(\n (async () => {\n try {\n await callback();\n } catch (error: any) {\n log.catch(error);\n }\n })(),\n );\n }\n this._disposeCallbacks.length = 0;\n\n return (this._disposePromise = Promise.all(promises).then(() => {}));\n }\n\n /**\n * Raise the error inside the context.\n * The error will be propagated to the error handler.\n * IF the error handler is not set, the error will dispose the context and cause an unhandled rejection.\n */\n raise(error: Error): void {\n if (this._isDisposed) {\n // 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 this.onDispose(() => newCtx.dispose());\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 */\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 return Promise.race([promise, rejectOnDispose(ctx)]);\n};\n"],
5
+ "mappings": ";AAIA,SAASA,WAAW;AACpB,SAASC,sBAAsB;AAD/B,IAAA,aAAA,SAAA,YAAA,QAAA,KAAA,MAAA;;;;;;;;;;AAYA,IAAaC,UAAN,MAAA,SAAA;EAMLC,YAAY,EACVC,UAAU,CAACC,UAAU;AACnB,SAAK,KAAKC,QAAO;AAGjB,UAAMD;EACR,EAAC,IACsB,CAAC,GAAG;AAXZE,6BAAuC,CAAA;AAChDC,uBAAc;AAWpB,SAAKC,WAAWL;EAClB;EAEA,IAAIM,WAAW;AACb,WAAO,KAAKF;EACd;;;;;;;;EASAG,UAAUC,UAA2B;AACnC,QAAI,KAAKJ,aAAa;AAEpB,YAAM,YAAY;AAChB,YAAI;AACF,gBAAMI,SAAAA;QACR,SAASP,OAAP;AACAL,cAAIa,MAAMR,OAAAA,CAAAA,GAAAA;;;;;;QACZ;MACF,GAAA;IACF;AAEA,SAAKE,kBAAkBO,KAAKF,QAAAA;EAC9B;;;;;;;;;EAUAN,UAAyB;AACvB,QAAI,KAAKS,iBAAiB;AACxB,aAAO,KAAKA;IACd;AACA,SAAKP,cAAc;AAEnB,UAAMQ,WAAW,CAAA;AACjB,eAAWJ,YAAY,KAAKL,kBAAkBU,QAAO,GAAI;AACvDD,eAASF,MACN,YAAY;AACX,YAAI;AACF,gBAAMF,SAAAA;QACR,SAASP,OAAP;AACAL,cAAIa,MAAMR,OAAAA,CAAAA,GAAAA;;;;;;QACZ;MACF,GAAA,CAAA;IAEJ;AACA,SAAKE,kBAAkBW,SAAS;AAEhC,WAAQ,KAAKH,kBAAkBI,QAAQC,IAAIJ,QAAAA,EAAUK,KAAK,MAAM;IAAC,CAAA;EACnE;;;;;;EAOAC,MAAMjB,OAAoB;AACxB,QAAI,KAAKG,aAAa;AAGpB;IACF;AAEA,QAAI;AACF,WAAKC,SAASJ,KAAAA;IAChB,SAASkB,KAAP;AAEA,WAAKJ,QAAQK,OAAOD,GAAAA;IACtB;EACF;EAEAE,OAAO,EAAErB,QAAO,IAA0B,CAAC,GAAY;AACrD,UAAMsB,SAAS,IAAIxB,QAAQ;MACzBE,SAAS,OAAOC,UAAU;AACxB,YAAI,CAACD,SAAS;AACZ,eAAKkB,MAAMjB,KAAAA;QACb,OAAO;AACL,cAAI;AACF,kBAAMD,QAAQC,KAAAA;UAChB,SAAQ,GAAN;AACA,iBAAKiB,MAAMjB,KAAAA;UACb;QACF;MACF;IACF,CAAA;AACA,SAAKM,UAAU,MAAMe,OAAOpB,QAAO,CAAA;AACnC,WAAOoB;EACT;AACF;AA/GaxB,UAAAA,WAAAA;EADZD,eAAe,SAAA;GACHC,OAAAA;;;ACZb,SAASyB,sBAAsB;AAOxB,IAAMC,kBAAkB,CAACC,KAAcC,QAAQ,IAAIH,eAAAA,MACxD,IAAII,QAAQ,CAACC,SAASC,WAAW;AAC/BJ,MAAIK,UAAU,MAAMD,OAAOH,KAAAA,CAAAA;AAC7B,CAAA;AAKK,IAAMK,oBAAoB,CAAIN,KAAcO,YAAoC;AACrF,SAAOL,QAAQM,KAAK;IAACD;IAASR,gBAAgBC,GAAAA;GAAK;AACrD;",
6
+ "names": ["log", "safeInstanceof", "Context", "constructor", "onError", "error", "dispose", "_disposeCallbacks", "_isDisposed", "_onError", "disposed", "onDispose", "callback", "catch", "push", "_disposePromise", "promises", "reverse", "length", "Promise", "all", "then", "raise", "err", "reject", "derive", "newCtx", "CancelledError", "rejectOnDispose", "ctx", "error", "Promise", "resolve", "reject", "onDispose", "cancelWithContext", "promise", "race"]
7
7
  }
@@ -1 +1 @@
1
- {"inputs":{"packages/common/context/src/context.ts":{"bytes":11895,"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":2064,"imports":[{"path":"@dxos/errors","kind":"import-statement","external":true}]},"packages/common/context/src/index.ts":{"bytes":476,"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":6402},"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":3666},"packages/common/context/src/index.ts":{"bytesInOutput":0},"packages/common/context/src/promise-utils.ts":{"bytesInOutput":300}},"bytes":4154}}}
1
+ {"inputs":{"packages/common/context/src/context.ts":{"bytes":11838,"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":2064,"imports":[{"path":"@dxos/errors","kind":"import-statement","external":true}]},"packages/common/context/src/index.ts":{"bytes":476,"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":6405},"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":3500},"packages/common/context/src/index.ts":{"bytesInOutput":0},"packages/common/context/src/promise-utils.ts":{"bytesInOutput":300}},"bytes":3988}}}
@@ -114,12 +114,6 @@ var Context = class Context1 {
114
114
  */
115
115
  raise(error) {
116
116
  if (this._isDisposed) {
117
- import_log.log.warn("Error in disposed context", error, {
118
- file: "context.ts",
119
- line: 98,
120
- scope: this,
121
- callSite: (f, a) => f(...a)
122
- });
123
117
  return;
124
118
  }
125
119
  try {
@@ -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@safeInstanceof('Context')\nexport class Context {\n private readonly _onError: ContextErrorHandler;\n private readonly _disposeCallbacks: DisposeCallback[] = [];\n private _isDisposed = false;\n private _disposePromise?: Promise<void>;\n\n constructor({\n onError = (error) => {\n void this.dispose();\n\n // Will generate an unhandled rejection.\n throw error;\n }\n }: CreateContextParams = {}) {\n this._onError = onError;\n }\n\n get disposed() {\n return this._isDisposed;\n }\n\n /**\n * Schedules a callback to run when the context is disposed.\n * May be async, in this case the disposer might choose to wait for all resource to released.\n * Throwing an error inside the callback will result in the error being logged, but not re-thrown.\n *\n * NOTE: Will call the callback immediately if the context is already disposed.\n */\n onDispose(callback: DisposeCallback) {\n if (this._isDisposed) {\n // Call the callback immediately if the context is already disposed.\n void (async () => {\n try {\n await callback();\n } catch (error: any) {\n log.catch(error);\n }\n })();\n }\n\n this._disposeCallbacks.push(callback);\n }\n\n /**\n * Runs all dispose callbacks.\n * Sync callbacks are run in the reverse order they were added.\n * Async callbacks are run in parallel.\n * This function never throws.\n * It is safe to ignore the returned promise if the caller does not wish to wait for callbacks to complete.\n * Disposing context means that onDispose will throw an error and any errors raised will be logged and not propagated.\n */\n dispose(): Promise<void> {\n if (this._disposePromise) {\n return this._disposePromise;\n }\n this._isDisposed = true;\n\n const promises = [];\n for (const callback of this._disposeCallbacks.reverse()) {\n promises.push(\n (async () => {\n try {\n await callback();\n } catch (error: any) {\n log.catch(error);\n }\n })()\n );\n }\n this._disposeCallbacks.length = 0;\n\n return (this._disposePromise = Promise.all(promises).then(() => {}));\n }\n\n /**\n * Raise the error inside the context.\n * The error will be propagated to the error handler.\n * IF the error handler is not set, the error will dispose the context and cause an unhandled rejection.\n */\n raise(error: Error): void {\n if (this._isDisposed) {\n log.warn('Error in disposed context', error);\n return;\n }\n\n try {\n this._onError(error);\n } catch (err) {\n // Generate an unhandled rejection and stop the error propagation.\n void Promise.reject(err);\n }\n }\n\n derive({ onError }: CreateContextParams = {}): Context {\n const newCtx = new Context({\n onError: async (error) => {\n if (!onError) {\n this.raise(error);\n } else {\n try {\n await onError(error);\n } catch {\n this.raise(error);\n }\n }\n }\n });\n this.onDispose(() => newCtx.dispose());\n return newCtx;\n }\n}\n", "//\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 */\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 return Promise.race([promise, rejectOnDispose(ctx)]);\n};\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;ACIA,iBAAoB;AACpB,kBAA+B;AAD/B,IAAA,aAAA,SAAA,YAAA,QAAA,KAAA,MAAA;;;;;;;;;;AAYA,IAAaA,UAAN,MAAA,SAAA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AACD,SAAS;AACZ,eAAKmB,MAAMlB,KAAAA;QACb,OAAO;AACL,cAAI;AACF,kBAAMD,QAAQC,KAAAA;UAChB,SAAQ,GAAN;AACA,iBAAKkB,MAAMlB,KAAAA;UACb;QACF;MACF;IACF,CAAA;AACA,SAAKM,UAAU,MAAMiB,OAAOtB,QAAO,CAAA;AACnC,WAAOsB;EACT;AACF;AA9Ga1B,UAAAA,WAAAA;MADZ2B,4BAAe,SAAA;GACH3B,OAAAA;;;ACZb,oBAA+B;AAOxB,IAAM4B,kBAAkB,CAACC,KAAcC,QAAQ,IAAIC,6BAAAA,MACxD,IAAIC,QAAQ,CAACC,SAASC,WAAW;AAC/BL,MAAIM,UAAU,MAAMD,OAAOJ,KAAAA,CAAAA;AAC7B,CAAA;AAKK,IAAMM,oBAAoB,CAAIP,KAAcQ,YAAoC;AACrF,SAAOL,QAAQM,KAAK;IAACD;IAAST,gBAAgBC,GAAAA;GAAK;AACrD;",
6
- "names": ["Context", "constructor", "onError", "error", "dispose", "_disposeCallbacks", "_isDisposed", "_onError", "disposed", "onDispose", "callback", "log", "catch", "push", "_disposePromise", "promises", "reverse", "length", "Promise", "all", "then", "raise", "warn", "err", "reject", "derive", "newCtx", "safeInstanceof", "rejectOnDispose", "ctx", "error", "CancelledError", "Promise", "resolve", "reject", "onDispose", "cancelWithContext", "promise", "race"]
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@safeInstanceof('Context')\nexport class Context {\n private readonly _onError: ContextErrorHandler;\n private readonly _disposeCallbacks: DisposeCallback[] = [];\n private _isDisposed = false;\n private _disposePromise?: Promise<void>;\n\n constructor({\n onError = (error) => {\n void this.dispose();\n\n // Will generate an unhandled rejection.\n throw error;\n },\n }: CreateContextParams = {}) {\n this._onError = onError;\n }\n\n get disposed() {\n return this._isDisposed;\n }\n\n /**\n * Schedules a callback to run when the context is disposed.\n * May be async, in this case the disposer might choose to wait for all resource to released.\n * Throwing an error inside the callback will result in the error being logged, but not re-thrown.\n *\n * NOTE: Will call the callback immediately if the context is already disposed.\n */\n onDispose(callback: DisposeCallback) {\n if (this._isDisposed) {\n // Call the callback immediately if the context is already disposed.\n void (async () => {\n try {\n await callback();\n } catch (error: any) {\n log.catch(error);\n }\n })();\n }\n\n this._disposeCallbacks.push(callback);\n }\n\n /**\n * Runs all dispose callbacks.\n * Sync callbacks are run in the reverse order they were added.\n * Async callbacks are run in parallel.\n * This function never throws.\n * It is safe to ignore the returned promise if the caller does not wish to wait for callbacks to complete.\n * Disposing context means that onDispose will throw an error and any errors raised will be logged and not propagated.\n */\n dispose(): Promise<void> {\n if (this._disposePromise) {\n return this._disposePromise;\n }\n this._isDisposed = true;\n\n const promises = [];\n for (const callback of this._disposeCallbacks.reverse()) {\n promises.push(\n (async () => {\n try {\n await callback();\n } catch (error: any) {\n log.catch(error);\n }\n })(),\n );\n }\n this._disposeCallbacks.length = 0;\n\n return (this._disposePromise = Promise.all(promises).then(() => {}));\n }\n\n /**\n * Raise the error inside the context.\n * The error will be propagated to the error handler.\n * IF the error handler is not set, the error will dispose the context and cause an unhandled rejection.\n */\n raise(error: Error): void {\n if (this._isDisposed) {\n // 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 this.onDispose(() => newCtx.dispose());\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 */\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 return Promise.race([promise, rejectOnDispose(ctx)]);\n};\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;ACIA,iBAAoB;AACpB,kBAA+B;AAD/B,IAAA,aAAA,SAAA,YAAA,QAAA,KAAA,MAAA;;;;;;;;;;AAYA,IAAaA,UAAN,MAAA,SAAA;EAMLC,YAAY,EACVC,UAAU,CAACC,UAAU;AACnB,SAAK,KAAKC,QAAO;AAGjB,UAAMD;EACR,EAAC,IACsB,CAAC,GAAG;AAXZE,6BAAuC,CAAA;AAChDC,uBAAc;AAWpB,SAAKC,WAAWL;EAClB;EAEA,IAAIM,WAAW;AACb,WAAO,KAAKF;EACd;;;;;;;;EASAG,UAAUC,UAA2B;AACnC,QAAI,KAAKJ,aAAa;AAEpB,YAAM,YAAY;AAChB,YAAI;AACF,gBAAMI,SAAAA;QACR,SAASP,OAAP;AACAQ,yBAAIC,MAAMT,OAAAA,CAAAA,GAAAA;;;;;;QACZ;MACF,GAAA;IACF;AAEA,SAAKE,kBAAkBQ,KAAKH,QAAAA;EAC9B;;;;;;;;;EAUAN,UAAyB;AACvB,QAAI,KAAKU,iBAAiB;AACxB,aAAO,KAAKA;IACd;AACA,SAAKR,cAAc;AAEnB,UAAMS,WAAW,CAAA;AACjB,eAAWL,YAAY,KAAKL,kBAAkBW,QAAO,GAAI;AACvDD,eAASF,MACN,YAAY;AACX,YAAI;AACF,gBAAMH,SAAAA;QACR,SAASP,OAAP;AACAQ,yBAAIC,MAAMT,OAAAA,CAAAA,GAAAA;;;;;;QACZ;MACF,GAAA,CAAA;IAEJ;AACA,SAAKE,kBAAkBY,SAAS;AAEhC,WAAQ,KAAKH,kBAAkBI,QAAQC,IAAIJ,QAAAA,EAAUK,KAAK,MAAM;IAAC,CAAA;EACnE;;;;;;EAOAC,MAAMlB,OAAoB;AACxB,QAAI,KAAKG,aAAa;AAGpB;IACF;AAEA,QAAI;AACF,WAAKC,SAASJ,KAAAA;IAChB,SAASmB,KAAP;AAEA,WAAKJ,QAAQK,OAAOD,GAAAA;IACtB;EACF;EAEAE,OAAO,EAAEtB,QAAO,IAA0B,CAAC,GAAY;AACrD,UAAMuB,SAAS,IAAIzB,QAAQ;MACzBE,SAAS,OAAOC,UAAU;AACxB,YAAI,CAACD,SAAS;AACZ,eAAKmB,MAAMlB,KAAAA;QACb,OAAO;AACL,cAAI;AACF,kBAAMD,QAAQC,KAAAA;UAChB,SAAQ,GAAN;AACA,iBAAKkB,MAAMlB,KAAAA;UACb;QACF;MACF;IACF,CAAA;AACA,SAAKM,UAAU,MAAMgB,OAAOrB,QAAO,CAAA;AACnC,WAAOqB;EACT;AACF;AA/GazB,UAAAA,WAAAA;MADZ0B,4BAAe,SAAA;GACH1B,OAAAA;;;ACZb,oBAA+B;AAOxB,IAAM2B,kBAAkB,CAACC,KAAcC,QAAQ,IAAIC,6BAAAA,MACxD,IAAIC,QAAQ,CAACC,SAASC,WAAW;AAC/BL,MAAIM,UAAU,MAAMD,OAAOJ,KAAAA,CAAAA;AAC7B,CAAA;AAKK,IAAMM,oBAAoB,CAAIP,KAAcQ,YAAoC;AACrF,SAAOL,QAAQM,KAAK;IAACD;IAAST,gBAAgBC,GAAAA;GAAK;AACrD;",
6
+ "names": ["Context", "constructor", "onError", "error", "dispose", "_disposeCallbacks", "_isDisposed", "_onError", "disposed", "onDispose", "callback", "log", "catch", "push", "_disposePromise", "promises", "reverse", "length", "Promise", "all", "then", "raise", "err", "reject", "derive", "newCtx", "safeInstanceof", "rejectOnDispose", "ctx", "error", "CancelledError", "Promise", "resolve", "reject", "onDispose", "cancelWithContext", "promise", "race"]
7
7
  }
@@ -1 +1 @@
1
- {"inputs":{"packages/common/context/src/context.ts":{"bytes":11895,"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":2064,"imports":[{"path":"@dxos/errors","kind":"import-statement","external":true}]},"packages/common/context/src/index.ts":{"bytes":476,"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":6553},"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":3718},"packages/common/context/src/promise-utils.ts":{"bytesInOutput":312}},"bytes":5345}}}
1
+ {"inputs":{"packages/common/context/src/context.ts":{"bytes":11838,"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":2064,"imports":[{"path":"@dxos/errors","kind":"import-statement","external":true}]},"packages/common/context/src/index.ts":{"bytes":476,"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":6557},"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":3541},"packages/common/context/src/promise-utils.ts":{"bytesInOutput":312}},"bytes":5168}}}
@@ -1,6 +1,6 @@
1
- export declare type ContextErrorHandler = (error: Error) => void;
2
- export declare type DisposeCallback = () => void | Promise<void>;
3
- export declare type CreateContextParams = {
1
+ export type ContextErrorHandler = (error: Error) => void;
2
+ export type DisposeCallback = () => void | Promise<void>;
3
+ export type CreateContextParams = {
4
4
  onError?: ContextErrorHandler;
5
5
  };
6
6
  export declare class Context {
@@ -8,7 +8,7 @@ export declare class Context {
8
8
  private readonly _disposeCallbacks;
9
9
  private _isDisposed;
10
10
  private _disposePromise?;
11
- constructor({ onError }?: CreateContextParams);
11
+ constructor({ onError, }?: CreateContextParams);
12
12
  get disposed(): boolean;
13
13
  /**
14
14
  * Schedules a callback to run when the context is disposed.
@@ -1 +1 @@
1
- {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../../src/context.ts"],"names":[],"mappings":"AAOA,oBAAY,mBAAmB,GAAG,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;AAEzD,oBAAY,eAAe,GAAG,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AAEzD,oBAAY,mBAAmB,GAAG;IAChC,OAAO,CAAC,EAAE,mBAAmB,CAAC;CAC/B,CAAC;AAEF,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;gBAE5B,EACV,OAKC,EACF,GAAE,mBAAwB;IAI3B,IAAI,QAAQ,YAEX;IAED;;;;;;OAMG;IACH,SAAS,CAAC,QAAQ,EAAE,eAAe;IAenC;;;;;;;OAOG;IACH,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAuBxB;;;;OAIG;IACH,KAAK,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI;IAczB,MAAM,CAAC,EAAE,OAAO,EAAE,GAAE,mBAAwB,GAAG,OAAO;CAiBvD"}
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;CAC/B,CAAC;AAEF,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;gBAE5B,EACV,OAKC,GACF,GAAE,mBAAwB;IAI3B,IAAI,QAAQ,YAEX;IAED;;;;;;OAMG;IACH,SAAS,CAAC,QAAQ,EAAE,eAAe;IAenC;;;;;;;OAOG;IACH,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAuBxB;;;;OAIG;IACH,KAAK,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI;IAezB,MAAM,CAAC,EAAE,OAAO,EAAE,GAAE,mBAAwB,GAAG,OAAO;CAiBvD"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/context",
3
- "version": "0.1.41",
3
+ "version": "0.1.45-next.45667d4",
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.41",
20
- "@dxos/log": "0.1.41",
21
- "@dxos/util": "0.1.41"
19
+ "@dxos/errors": "0.1.45-next.45667d4",
20
+ "@dxos/log": "0.1.45-next.45667d4",
21
+ "@dxos/util": "0.1.45-next.45667d4"
22
22
  },
23
23
  "publishConfig": {
24
24
  "access": "public"
@@ -30,7 +30,7 @@ describe('Context', () => {
30
30
  const ctx = new Context({
31
31
  onError: (err) => {
32
32
  error = err;
33
- }
33
+ },
34
34
  });
35
35
 
36
36
  ctx.raise(new Error('test'));
package/src/context.ts CHANGED
@@ -26,7 +26,7 @@ export class Context {
26
26
 
27
27
  // Will generate an unhandled rejection.
28
28
  throw error;
29
- }
29
+ },
30
30
  }: CreateContextParams = {}) {
31
31
  this._onError = onError;
32
32
  }
@@ -80,7 +80,7 @@ export class Context {
80
80
  } catch (error: any) {
81
81
  log.catch(error);
82
82
  }
83
- })()
83
+ })(),
84
84
  );
85
85
  }
86
86
  this._disposeCallbacks.length = 0;
@@ -95,7 +95,8 @@ export class Context {
95
95
  */
96
96
  raise(error: Error): void {
97
97
  if (this._isDisposed) {
98
- log.warn('Error in disposed context', error);
98
+ // TODO(dmaretskyi): Don't log those.
99
+ // log.warn('Error in disposed context', error);
99
100
  return;
100
101
  }
101
102
 
@@ -119,7 +120,7 @@ export class Context {
119
120
  this.raise(error);
120
121
  }
121
122
  }
122
- }
123
+ },
123
124
  });
124
125
  this.onDispose(() => newCtx.dispose());
125
126
  return newCtx;