@dxos/context 0.1.23 → 0.1.24

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.
@@ -100,7 +100,7 @@ var Context = class Context1 {
100
100
  void Promise.reject(err);
101
101
  }
102
102
  }
103
- derive({ onError }) {
103
+ derive({ onError } = {}) {
104
104
  const newCtx = new Context({
105
105
  onError: async (error) => {
106
106
  if (!onError) {
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/context.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"],
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;AACpBR,UAAIuB,KAAK,6BAA6BlB,OAAAA;;;;;;AACtC;IACF;AAEA,QAAI;AACF,WAAKI,SAASJ,KAAAA;IAChB,SAASmB,KAAP;AAEA,WAAKL,QAAQM,OAAOD,GAAAA;IACtB;EACF;EAEAE,OAAO,EAAEtB,QAAO,GAAkC;AAChD,UAAMuB,SAAS,IAAIzB,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,MAAMgB,OAAOrB,QAAO,CAAA;AACnC,WAAOqB;EACT;AACF;AA9GazB,UAAAA,WAAAA;EADZD,eAAe,SAAA;GACHC,OAAAA;",
4
+ "sourcesContent": ["//\n// Copyright 2022 DXOS.org\n//\n\nimport { log } from '@dxos/log';\nimport { safeInstanceof } from '@dxos/util';\n\nexport type ContextErrorHandler = (error: Error) => void;\n\nexport type DisposeCallback = () => void | Promise<void>;\n\nexport type CreateContextParams = {\n onError?: ContextErrorHandler;\n};\n\n@safeInstanceof('Context')\nexport class Context {\n private readonly _onError: ContextErrorHandler;\n private readonly _disposeCallbacks: DisposeCallback[] = [];\n private _isDisposed = false;\n private _disposePromise?: Promise<void>;\n\n constructor({\n onError = (error) => {\n void this.dispose();\n\n // Will generate an unhandled rejection.\n throw error;\n }\n }: CreateContextParams = {}) {\n this._onError = onError;\n }\n\n get disposed() {\n return this._isDisposed;\n }\n\n /**\n * Schedules a callback to run when the context is disposed.\n * May be async, in this case the disposer might choose to wait for all resource to released.\n * Throwing an error inside the callback will result in the error being logged, but not re-thrown.\n *\n * NOTE: Will call the callback immediately if the context is already disposed.\n */\n onDispose(callback: DisposeCallback) {\n if (this._isDisposed) {\n // Call the callback immediately if the context is already disposed.\n void (async () => {\n try {\n await callback();\n } catch (error: any) {\n log.catch(error);\n }\n })();\n }\n\n this._disposeCallbacks.push(callback);\n }\n\n /**\n * Runs all dispose callbacks.\n * Sync callbacks are run in the reverse order they were added.\n * Async callbacks are run in parallel.\n * This function never throws.\n * It is safe to ignore the returned promise if the caller does not wish to wait for callbacks to complete.\n * Disposing context means that onDispose will throw an error and any errors raised will be logged and not propagated.\n */\n dispose(): Promise<void> {\n if (this._disposePromise) {\n return this._disposePromise;\n }\n this._isDisposed = true;\n\n const promises = [];\n for (const callback of this._disposeCallbacks.reverse()) {\n promises.push(\n (async () => {\n try {\n await callback();\n } catch (error: any) {\n log.catch(error);\n }\n })()\n );\n }\n this._disposeCallbacks.length = 0;\n\n return (this._disposePromise = Promise.all(promises).then(() => {}));\n }\n\n /**\n * Raise the error inside the context.\n * The error will be propagated to the error handler.\n * IF the error handler is not set, the error will dispose the context and cause an unhandled rejection.\n */\n raise(error: Error): void {\n if (this._isDisposed) {\n log.warn('Error in disposed context', error);\n return;\n }\n\n try {\n this._onError(error);\n } catch (err) {\n // Generate an unhandled rejection and stop the error propagation.\n void Promise.reject(err);\n }\n }\n\n derive({ onError }: CreateContextParams = {}): Context {\n const newCtx = new Context({\n onError: async (error) => {\n if (!onError) {\n this.raise(error);\n } else {\n try {\n await onError(error);\n } catch {\n this.raise(error);\n }\n }\n }\n });\n this.onDispose(() => newCtx.dispose());\n return newCtx;\n }\n}\n"],
5
+ "mappings": ";AAIA,SAASA,WAAW;AACpB,SAASC,sBAAsB;AAD/B,IAAA,aAAA,SAAA,YAAA,QAAA,KAAA,MAAA;;;;;;;;;;AAYA,IAAaC,UAAN,MAAA,SAAA;EAMLC,YAAY,EACVC,UAAU,CAACC,UAAU;AACnB,SAAK,KAAKC,QAAO;AAGjB,UAAMD;EACR,EAAC,IACsB,CAAC,GAAG;AAXZE,6BAAuC,CAAA;AAChDC,uBAAc;AAWpB,SAAKC,WAAWL;EAClB;EAEA,IAAIM,WAAW;AACb,WAAO,KAAKF;EACd;;;;;;;;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;AACpBR,UAAIuB,KAAK,6BAA6BlB,OAAAA;;;;;;AACtC;IACF;AAEA,QAAI;AACF,WAAKI,SAASJ,KAAAA;IAChB,SAASmB,KAAP;AAEA,WAAKL,QAAQM,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,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,MAAMgB,OAAOrB,QAAO,CAAA;AACnC,WAAOqB;EACT;AACF;AA9GazB,UAAAA,WAAAA;EADZD,eAAe,SAAA;GACHC,OAAAA;",
6
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"]
7
7
  }
@@ -1 +1 @@
1
- {"inputs":{"packages/common/context/src/context.ts":{"bytes":11862,"imports":[{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}]},"packages/common/context/src/index.ts":{"bytes":375,"imports":[{"path":"packages/common/context/src/context.ts","kind":"import-statement","original":"./context"}]}},"outputs":{"packages/common/context/dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":5359},"packages/common/context/dist/lib/browser/index.mjs":{"imports":[{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}],"exports":["Context"],"entryPoint":"packages/common/context/src/index.ts","inputs":{"packages/common/context/src/context.ts":{"bytesInOutput":3661},"packages/common/context/src/index.ts":{"bytesInOutput":0}},"bytes":3760}}}
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/index.ts":{"bytes":375,"imports":[{"path":"packages/common/context/src/context.ts","kind":"import-statement","original":"./context"}]}},"outputs":{"packages/common/context/dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":5374},"packages/common/context/dist/lib/browser/index.mjs":{"imports":[{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}],"exports":["Context"],"entryPoint":"packages/common/context/src/index.ts","inputs":{"packages/common/context/src/context.ts":{"bytesInOutput":3666},"packages/common/context/src/index.ts":{"bytesInOutput":0}},"bytes":3765}}}
@@ -126,7 +126,7 @@ var Context = class Context1 {
126
126
  void Promise.reject(err);
127
127
  }
128
128
  }
129
- derive({ onError }) {
129
+ derive({ onError } = {}) {
130
130
  const newCtx = new Context({
131
131
  onError: async (error) => {
132
132
  if (!onError) {
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/index.ts", "../../../src/context.ts"],
4
- "sourcesContent": ["//\n// Copyright 2022 DXOS.org\n//\n\nexport * from './context';\n", "//\n// Copyright 2022 DXOS.org\n//\n\nimport { log } from '@dxos/log';\nimport { safeInstanceof } from '@dxos/util';\n\nexport type ContextErrorHandler = (error: Error) => void;\n\nexport type DisposeCallback = () => void | Promise<void>;\n\nexport type CreateContextParams = {\n onError?: ContextErrorHandler;\n};\n\n@safeInstanceof('Context')\nexport class Context {\n private readonly _onError: ContextErrorHandler;\n private readonly _disposeCallbacks: DisposeCallback[] = [];\n private _isDisposed = false;\n private _disposePromise?: Promise<void>;\n\n constructor({\n onError = (error) => {\n void this.dispose();\n\n // Will generate an unhandled rejection.\n throw error;\n }\n }: CreateContextParams = {}) {\n this._onError = onError;\n }\n\n get disposed() {\n return this._isDisposed;\n }\n\n /**\n * Schedules a callback to run when the context is disposed.\n * May be async, in this case the disposer might choose to wait for all resource to released.\n * Throwing an error inside the callback will result in the error being logged, but not re-thrown.\n *\n * NOTE: Will call the callback immediately if the context is already disposed.\n */\n onDispose(callback: DisposeCallback) {\n if (this._isDisposed) {\n // Call the callback immediately if the context is already disposed.\n void (async () => {\n try {\n await callback();\n } catch (error: any) {\n log.catch(error);\n }\n })();\n }\n\n this._disposeCallbacks.push(callback);\n }\n\n /**\n * Runs all dispose callbacks.\n * Sync callbacks are run in the reverse order they were added.\n * Async callbacks are run in parallel.\n * This function never throws.\n * It is safe to ignore the returned promise if the caller does not wish to wait for callbacks to complete.\n * Disposing context means that onDispose will throw an error and any errors raised will be logged and not propagated.\n */\n dispose(): Promise<void> {\n if (this._disposePromise) {\n return this._disposePromise;\n }\n this._isDisposed = true;\n\n const promises = [];\n for (const callback of this._disposeCallbacks.reverse()) {\n promises.push(\n (async () => {\n try {\n await callback();\n } catch (error: any) {\n log.catch(error);\n }\n })()\n );\n }\n this._disposeCallbacks.length = 0;\n\n return (this._disposePromise = Promise.all(promises).then(() => {}));\n }\n\n /**\n * Raise the error inside the context.\n * The error will be propagated to the error handler.\n * IF the error handler is not set, the error will dispose the context and cause an unhandled rejection.\n */\n raise(error: Error): void {\n if (this._isDisposed) {\n log.warn('Error in disposed context', error);\n return;\n }\n\n try {\n this._onError(error);\n } catch (err) {\n // Generate an unhandled rejection and stop the error propagation.\n void Promise.reject(err);\n }\n }\n\n derive({ onError }: CreateContextParams): Context {\n const newCtx = new Context({\n onError: async (error) => {\n if (!onError) {\n this.raise(error);\n } else {\n try {\n await onError(error);\n } catch {\n this.raise(error);\n }\n }\n }\n });\n this.onDispose(() => newCtx.dispose());\n return newCtx;\n }\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;AACpBK,qBAAIW,KAAK,6BAA6BnB,OAAAA;;;;;;AACtC;IACF;AAEA,QAAI;AACF,WAAKI,SAASJ,KAAAA;IAChB,SAASoB,KAAP;AAEA,WAAKL,QAAQM,OAAOD,GAAAA;IACtB;EACF;EAEAE,OAAO,EAAEvB,QAAO,GAAkC;AAChD,UAAMwB,SAAS,IAAI1B,QAAQ;MACzBE,SAAS,OAAOC,UAAU;AACxB,YAAI,CAACD,SAAS;AACZ,eAAKmB,MAAMlB,KAAAA;QACb,OAAO;AACL,cAAI;AACF,kBAAMD,QAAQC,KAAAA;UAChB,SAAQ,GAAN;AACA,iBAAKkB,MAAMlB,KAAAA;UACb;QACF;MACF;IACF,CAAA;AACA,SAAKM,UAAU,MAAMiB,OAAOtB,QAAO,CAAA;AACnC,WAAOsB;EACT;AACF;AA9Ga1B,UAAAA,WAAAA;MADZ2B,4BAAe,SAAA;GACH3B,OAAAA;",
4
+ "sourcesContent": ["//\n// Copyright 2022 DXOS.org\n//\n\nexport * from './context';\n", "//\n// Copyright 2022 DXOS.org\n//\n\nimport { log } from '@dxos/log';\nimport { safeInstanceof } from '@dxos/util';\n\nexport type ContextErrorHandler = (error: Error) => void;\n\nexport type DisposeCallback = () => void | Promise<void>;\n\nexport type CreateContextParams = {\n onError?: ContextErrorHandler;\n};\n\n@safeInstanceof('Context')\nexport class Context {\n private readonly _onError: ContextErrorHandler;\n private readonly _disposeCallbacks: DisposeCallback[] = [];\n private _isDisposed = false;\n private _disposePromise?: Promise<void>;\n\n constructor({\n onError = (error) => {\n void this.dispose();\n\n // Will generate an unhandled rejection.\n throw error;\n }\n }: CreateContextParams = {}) {\n this._onError = onError;\n }\n\n get disposed() {\n return this._isDisposed;\n }\n\n /**\n * Schedules a callback to run when the context is disposed.\n * May be async, in this case the disposer might choose to wait for all resource to released.\n * Throwing an error inside the callback will result in the error being logged, but not re-thrown.\n *\n * NOTE: Will call the callback immediately if the context is already disposed.\n */\n onDispose(callback: DisposeCallback) {\n if (this._isDisposed) {\n // Call the callback immediately if the context is already disposed.\n void (async () => {\n try {\n await callback();\n } catch (error: any) {\n log.catch(error);\n }\n })();\n }\n\n this._disposeCallbacks.push(callback);\n }\n\n /**\n * Runs all dispose callbacks.\n * Sync callbacks are run in the reverse order they were added.\n * Async callbacks are run in parallel.\n * This function never throws.\n * It is safe to ignore the returned promise if the caller does not wish to wait for callbacks to complete.\n * Disposing context means that onDispose will throw an error and any errors raised will be logged and not propagated.\n */\n dispose(): Promise<void> {\n if (this._disposePromise) {\n return this._disposePromise;\n }\n this._isDisposed = true;\n\n const promises = [];\n for (const callback of this._disposeCallbacks.reverse()) {\n promises.push(\n (async () => {\n try {\n await callback();\n } catch (error: any) {\n log.catch(error);\n }\n })()\n );\n }\n this._disposeCallbacks.length = 0;\n\n return (this._disposePromise = Promise.all(promises).then(() => {}));\n }\n\n /**\n * Raise the error inside the context.\n * The error will be propagated to the error handler.\n * IF the error handler is not set, the error will dispose the context and cause an unhandled rejection.\n */\n raise(error: Error): void {\n if (this._isDisposed) {\n log.warn('Error in disposed context', error);\n return;\n }\n\n try {\n this._onError(error);\n } catch (err) {\n // Generate an unhandled rejection and stop the error propagation.\n void Promise.reject(err);\n }\n }\n\n derive({ onError }: CreateContextParams = {}): Context {\n const newCtx = new Context({\n onError: async (error) => {\n if (!onError) {\n this.raise(error);\n } else {\n try {\n await onError(error);\n } catch {\n this.raise(error);\n }\n }\n }\n });\n this.onDispose(() => newCtx.dispose());\n return newCtx;\n }\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;AACpBK,qBAAIW,KAAK,6BAA6BnB,OAAAA;;;;;;AACtC;IACF;AAEA,QAAI;AACF,WAAKI,SAASJ,KAAAA;IAChB,SAASoB,KAAP;AAEA,WAAKL,QAAQM,OAAOD,GAAAA;IACtB;EACF;EAEAE,OAAO,EAAEvB,QAAO,IAA0B,CAAC,GAAY;AACrD,UAAMwB,SAAS,IAAI1B,QAAQ;MACzBE,SAAS,OAAOC,UAAU;AACxB,YAAI,CAACD,SAAS;AACZ,eAAKmB,MAAMlB,KAAAA;QACb,OAAO;AACL,cAAI;AACF,kBAAMD,QAAQC,KAAAA;UAChB,SAAQ,GAAN;AACA,iBAAKkB,MAAMlB,KAAAA;UACb;QACF;MACF;IACF,CAAA;AACA,SAAKM,UAAU,MAAMiB,OAAOtB,QAAO,CAAA;AACnC,WAAOsB;EACT;AACF;AA9Ga1B,UAAAA,WAAAA;MADZ2B,4BAAe,SAAA;GACH3B,OAAAA;",
6
6
  "names": ["Context", "constructor", "onError", "error", "dispose", "_disposeCallbacks", "_isDisposed", "_onError", "disposed", "onDispose", "callback", "log", "catch", "push", "_disposePromise", "promises", "reverse", "length", "Promise", "all", "then", "raise", "warn", "err", "reject", "derive", "newCtx", "safeInstanceof"]
7
7
  }
@@ -1 +1 @@
1
- {"inputs":{"packages/common/context/src/context.ts":{"bytes":11862,"imports":[{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}]},"packages/common/context/src/index.ts":{"bytes":375,"imports":[{"path":"packages/common/context/src/context.ts","kind":"import-statement","original":"./context"}]}},"outputs":{"packages/common/context/dist/lib/node/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":5479},"packages/common/context/dist/lib/node/index.cjs":{"imports":[{"path":"@dxos/log","kind":"require-call","external":true},{"path":"@dxos/util","kind":"require-call","external":true}],"exports":[],"entryPoint":"packages/common/context/src/index.ts","inputs":{"packages/common/context/src/index.ts":{"bytesInOutput":119},"packages/common/context/src/context.ts":{"bytesInOutput":3713}},"bytes":4851}}}
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/index.ts":{"bytes":375,"imports":[{"path":"packages/common/context/src/context.ts","kind":"import-statement","original":"./context"}]}},"outputs":{"packages/common/context/dist/lib/node/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":5494},"packages/common/context/dist/lib/node/index.cjs":{"imports":[{"path":"@dxos/log","kind":"require-call","external":true},{"path":"@dxos/util","kind":"require-call","external":true}],"exports":[],"entryPoint":"packages/common/context/src/index.ts","inputs":{"packages/common/context/src/index.ts":{"bytesInOutput":119},"packages/common/context/src/context.ts":{"bytesInOutput":3718}},"bytes":4856}}}
@@ -33,6 +33,6 @@ export declare class Context {
33
33
  * IF the error handler is not set, the error will dispose the context and cause an unhandled rejection.
34
34
  */
35
35
  raise(error: Error): void;
36
- derive({ onError }: CreateContextParams): Context;
36
+ derive({ onError }?: CreateContextParams): Context;
37
37
  }
38
38
  //# sourceMappingURL=context.d.ts.map
@@ -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,EAAE,mBAAmB,GAAG,OAAO;CAiBlD"}
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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/context",
3
- "version": "0.1.23",
3
+ "version": "0.1.24",
4
4
  "description": "Async utils.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -16,8 +16,8 @@
16
16
  "src"
17
17
  ],
18
18
  "dependencies": {
19
- "@dxos/log": "0.1.23",
20
- "@dxos/util": "0.1.23"
19
+ "@dxos/log": "0.1.24",
20
+ "@dxos/util": "0.1.24"
21
21
  },
22
22
  "publishConfig": {
23
23
  "access": "public"
package/src/context.ts CHANGED
@@ -107,7 +107,7 @@ export class Context {
107
107
  }
108
108
  }
109
109
 
110
- derive({ onError }: CreateContextParams): Context {
110
+ derive({ onError }: CreateContextParams = {}): Context {
111
111
  const newCtx = new Context({
112
112
  onError: async (error) => {
113
113
  if (!onError) {