@dxos/context 0.8.4-main.7ace549 → 0.8.4-main.937b3ca
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/browser/index.mjs +16 -5
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +16 -5
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/context.d.ts +4 -3
- package/dist/types/src/context.d.ts.map +1 -1
- package/dist/types/src/resource.d.ts +1 -0
- package/dist/types/src/resource.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +11 -6
- package/src/context.ts +16 -4
- package/src/resource.ts +3 -0
package/package.json
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/context",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.937b3ca",
|
|
4
4
|
"description": "Async utils.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/dxos/dxos"
|
|
10
|
+
},
|
|
7
11
|
"license": "MIT",
|
|
8
12
|
"author": "DXOS.org",
|
|
9
|
-
"sideEffects":
|
|
13
|
+
"sideEffects": false,
|
|
10
14
|
"type": "module",
|
|
11
15
|
"exports": {
|
|
12
16
|
".": {
|
|
@@ -27,10 +31,11 @@
|
|
|
27
31
|
"src"
|
|
28
32
|
],
|
|
29
33
|
"dependencies": {
|
|
30
|
-
"@
|
|
31
|
-
"@dxos/
|
|
32
|
-
"@dxos/
|
|
33
|
-
"@dxos/
|
|
34
|
+
"@hazae41/symbol-dispose-polyfill": "^1.0.2",
|
|
35
|
+
"@dxos/debug": "0.8.4-main.937b3ca",
|
|
36
|
+
"@dxos/util": "0.8.4-main.937b3ca",
|
|
37
|
+
"@dxos/node-std": "0.8.4-main.937b3ca",
|
|
38
|
+
"@dxos/log": "0.8.4-main.937b3ca"
|
|
34
39
|
},
|
|
35
40
|
"publishConfig": {
|
|
36
41
|
"access": "public"
|
package/src/context.ts
CHANGED
|
@@ -14,7 +14,7 @@ export type ContextErrorHandler = (error: Error, ctx: Context) => void;
|
|
|
14
14
|
|
|
15
15
|
export type DisposeCallback = () => any | Promise<any>;
|
|
16
16
|
|
|
17
|
-
export type
|
|
17
|
+
export type CreateContextProps = {
|
|
18
18
|
name?: string;
|
|
19
19
|
parent?: Context;
|
|
20
20
|
attributes?: Record<string, any>;
|
|
@@ -67,9 +67,11 @@ export class Context {
|
|
|
67
67
|
#flags: ContextFlags = 0;
|
|
68
68
|
#disposePromise?: Promise<boolean> = undefined;
|
|
69
69
|
|
|
70
|
+
#signal: AbortSignal | undefined = undefined;
|
|
71
|
+
|
|
70
72
|
public maxSafeDisposeCallbacks = MAX_SAFE_DISPOSE_CALLBACKS;
|
|
71
73
|
|
|
72
|
-
constructor(params:
|
|
74
|
+
constructor(params: CreateContextProps = {}, callMeta?: Partial<CallMetadata>) {
|
|
73
75
|
this.#name = getContextName(params, callMeta);
|
|
74
76
|
this.#parent = params.parent;
|
|
75
77
|
this.#attributes = params.attributes ?? {};
|
|
@@ -100,6 +102,16 @@ export class Context {
|
|
|
100
102
|
return this.#disposeCallbacks.length;
|
|
101
103
|
}
|
|
102
104
|
|
|
105
|
+
get signal(): AbortSignal {
|
|
106
|
+
if (this.#signal) {
|
|
107
|
+
return this.#signal;
|
|
108
|
+
}
|
|
109
|
+
const controller = new AbortController();
|
|
110
|
+
this.#signal = controller.signal;
|
|
111
|
+
this.onDispose(() => controller.abort());
|
|
112
|
+
return this.#signal;
|
|
113
|
+
}
|
|
114
|
+
|
|
103
115
|
/**
|
|
104
116
|
* Schedules a callback to run when the context is disposed.
|
|
105
117
|
* May be async, in this case the disposer might choose to wait for all resource to released.
|
|
@@ -222,7 +234,7 @@ export class Context {
|
|
|
222
234
|
}
|
|
223
235
|
}
|
|
224
236
|
|
|
225
|
-
derive({ onError, attributes }:
|
|
237
|
+
derive({ onError, attributes }: CreateContextProps = {}): Context {
|
|
226
238
|
const newCtx = new Context({
|
|
227
239
|
// TODO(dmaretskyi): Optimize to not require allocating a new closure for every context.
|
|
228
240
|
onError: async (error) => {
|
|
@@ -267,7 +279,7 @@ export class Context {
|
|
|
267
279
|
}
|
|
268
280
|
}
|
|
269
281
|
|
|
270
|
-
const getContextName = (params:
|
|
282
|
+
const getContextName = (params: CreateContextProps, callMeta?: Partial<CallMetadata>): string | undefined => {
|
|
271
283
|
if (params.name) {
|
|
272
284
|
return params.name;
|
|
273
285
|
}
|
package/src/resource.ts
CHANGED
|
@@ -6,6 +6,8 @@ import { throwUnhandledError } from '@dxos/util';
|
|
|
6
6
|
|
|
7
7
|
import { Context } from './context';
|
|
8
8
|
|
|
9
|
+
import '@hazae41/symbol-dispose-polyfill';
|
|
10
|
+
|
|
9
11
|
export enum LifecycleState {
|
|
10
12
|
CLOSED = 'CLOSED',
|
|
11
13
|
OPEN = 'OPEN',
|
|
@@ -25,6 +27,7 @@ const CLOSE_RESOURCE_ON_UNHANDLED_ERROR = false;
|
|
|
25
27
|
*/
|
|
26
28
|
export abstract class Resource implements Lifecycle {
|
|
27
29
|
#lifecycleState = LifecycleState.CLOSED;
|
|
30
|
+
|
|
28
31
|
#openPromise: Promise<void> | null = null;
|
|
29
32
|
#closePromise: Promise<void> | null = null;
|
|
30
33
|
|