@automerge/automerge-repo 2.0.0-beta.1 → 2.0.0-beta.5
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/Repo.d.ts.map +1 -1
- package/dist/Repo.js +14 -14
- package/dist/helpers/abortable.d.ts +10 -13
- package/dist/helpers/abortable.d.ts.map +1 -1
- package/dist/helpers/abortable.js +25 -23
- package/package.json +2 -2
- package/src/Repo.ts +10 -6
- package/src/helpers/abortable.ts +28 -27
package/dist/Repo.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Repo.d.ts","sourceRoot":"","sources":["../src/Repo.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAQ5C,OAAO,EAEL,SAAS,EAKV,MAAM,gBAAgB,CAAA;AAIvB,OAAO,EACL,uBAAuB,EACvB,KAAK,YAAY,EAClB,MAAM,sCAAsC,CAAA;AAC7C,OAAO,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAA;AAEhE,OAAO,EAAE,uBAAuB,EAAE,MAAM,sCAAsC,CAAA;AAC9E,OAAO,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAA;AAChE,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AAC9C,OAAO,EAAE,sBAAsB,EAAE,MAAM,0CAA0C,CAAA;AACjF,OAAO,EACL,cAAc,EAEf,MAAM,gCAAgC,CAAA;AACvC,OAAO,KAAK,EACV,aAAa,EACb,YAAY,EACZ,UAAU,EACV,MAAM,EACP,MAAM,YAAY,CAAA;AACnB,OAAO,EAAa,YAAY,EAAE,MAAM,wBAAwB,CAAA;AAChE,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAEhD,MAAM,MAAM,uBAAuB,CAAC,CAAC,IAAI,YAAY,CAAC,CAAC,CAAC,GAAG;IACzD,UAAU,EAAE,CAAC,eAAe,EAAE,MAA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,EAAE,UAAU,CAAA;IACtB,cAAc,EAAE,MAAM,CAAA;IACtB,MAAM,EAAE,MAAM,CAAA;IACd,UAAU,EAAE,MAAM,CAAA;CACnB,GACD;IACE,IAAI,EAAE,YAAY,CAAA;IAClB,UAAU,EAAE,UAAU,CAAA;CACvB,CAAA"}
|
package/dist/Repo.js
CHANGED
|
@@ -290,7 +290,6 @@ export class Repo extends EventEmitter {
|
|
|
290
290
|
}
|
|
291
291
|
findWithProgress(id, options = {}) {
|
|
292
292
|
const { signal } = options;
|
|
293
|
-
const abortPromise = abortable(signal);
|
|
294
293
|
const { documentId, heads } = isValidAutomergeUrl(id)
|
|
295
294
|
? parseAutomergeUrl(id)
|
|
296
295
|
: { documentId: interpretAsDocumentId(id), heads: undefined };
|
|
@@ -358,7 +357,7 @@ export class Repo extends EventEmitter {
|
|
|
358
357
|
};
|
|
359
358
|
progressSignal.notify(initial);
|
|
360
359
|
// Start the loading process
|
|
361
|
-
void this.#loadDocumentWithProgress(id, documentId, handle, progressSignal,
|
|
360
|
+
void this.#loadDocumentWithProgress(id, documentId, handle, progressSignal, signal ? abortable(new Promise(() => { }), signal) : new Promise(() => { }));
|
|
362
361
|
const result = {
|
|
363
362
|
...initial,
|
|
364
363
|
peek: progressSignal.peek,
|
|
@@ -421,6 +420,10 @@ export class Repo extends EventEmitter {
|
|
|
421
420
|
}
|
|
422
421
|
async find(id, options = {}) {
|
|
423
422
|
const { allowableStates = ["ready"], signal } = options;
|
|
423
|
+
// Check if already aborted
|
|
424
|
+
if (signal?.aborted) {
|
|
425
|
+
throw new Error("Operation aborted");
|
|
426
|
+
}
|
|
424
427
|
const progress = this.findWithProgress(id, { signal });
|
|
425
428
|
if ("subscribe" in progress) {
|
|
426
429
|
this.#registerHandleWithSubsystems(progress.handle);
|
|
@@ -488,19 +491,16 @@ export class Repo extends EventEmitter {
|
|
|
488
491
|
id, options = {}) {
|
|
489
492
|
const documentId = interpretAsDocumentId(id);
|
|
490
493
|
const { allowableStates, signal } = options;
|
|
491
|
-
return
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
throw new Error(`Document ${id} is unavailable`);
|
|
498
|
-
}
|
|
494
|
+
return abortable((async () => {
|
|
495
|
+
const handle = await this.#loadDocument(documentId);
|
|
496
|
+
if (!allowableStates) {
|
|
497
|
+
await handle.whenReady([READY, UNAVAILABLE]);
|
|
498
|
+
if (handle.state === UNAVAILABLE && !signal?.aborted) {
|
|
499
|
+
throw new Error(`Document ${id} is unavailable`);
|
|
499
500
|
}
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
]);
|
|
501
|
+
}
|
|
502
|
+
return handle;
|
|
503
|
+
})(), signal);
|
|
504
504
|
}
|
|
505
505
|
delete(
|
|
506
506
|
/** The url or documentId of the handle to delete */
|
|
@@ -1,35 +1,32 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Wraps a Promise and causes it to reject when the signal is aborted.
|
|
3
3
|
*
|
|
4
4
|
* @remarks
|
|
5
|
-
* This utility
|
|
6
|
-
* It's designed to
|
|
5
|
+
* This utility wraps a Promise and rejects when the provided AbortSignal is aborted.
|
|
6
|
+
* It's designed to make Promise awaits abortable.
|
|
7
7
|
*
|
|
8
8
|
* @example
|
|
9
9
|
* ```typescript
|
|
10
10
|
* const controller = new AbortController();
|
|
11
11
|
*
|
|
12
12
|
* try {
|
|
13
|
-
* const result = await
|
|
14
|
-
*
|
|
15
|
-
* abortable(controller.signal)
|
|
16
|
-
* ]);
|
|
13
|
+
* const result = await abortable(fetch('https://api.example.com/data'), controller.signal);
|
|
14
|
+
* // Meanwhile, to abort in concurrent code before the above line returns: controller.abort();
|
|
17
15
|
* } catch (err) {
|
|
18
16
|
* if (err.name === 'AbortError') {
|
|
19
17
|
* console.log('The operation was aborted');
|
|
20
18
|
* }
|
|
21
19
|
* }
|
|
22
20
|
*
|
|
23
|
-
* // Later, to abort:
|
|
24
|
-
* controller.abort();
|
|
25
21
|
* ```
|
|
26
22
|
*
|
|
23
|
+
* @param p - A Promise to wrap
|
|
27
24
|
* @param signal - An AbortSignal that can be used to abort the operation
|
|
28
|
-
* @
|
|
29
|
-
*
|
|
30
|
-
* @throws {DOMException} With name "AbortError"
|
|
25
|
+
* @returns A wrapper Promise that rejects with AbortError if the signal is aborted
|
|
26
|
+
* before the promise p settles, and settles as p settles otherwise
|
|
27
|
+
* @throws {DOMException} With name "AbortError" if aborted before p settles
|
|
31
28
|
*/
|
|
32
|
-
export declare function abortable(signal
|
|
29
|
+
export declare function abortable<T>(p: Promise<T>, signal: AbortSignal | undefined): Promise<T>;
|
|
33
30
|
/**
|
|
34
31
|
* Include this type in an options object to pass an AbortSignal to a function.
|
|
35
32
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"abortable.d.ts","sourceRoot":"","sources":["../../src/helpers/abortable.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"abortable.d.ts","sourceRoot":"","sources":["../../src/helpers/abortable.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH,wBAAgB,SAAS,CAAC,CAAC,EACzB,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,EACb,MAAM,EAAE,WAAW,GAAG,SAAS,GAC9B,OAAO,CAAC,CAAC,CAAC,CAsBZ;AAED;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,MAAM,CAAC,EAAE,WAAW,CAAA;CACrB"}
|
|
@@ -1,45 +1,47 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Wraps a Promise and causes it to reject when the signal is aborted.
|
|
3
3
|
*
|
|
4
4
|
* @remarks
|
|
5
|
-
* This utility
|
|
6
|
-
* It's designed to
|
|
5
|
+
* This utility wraps a Promise and rejects when the provided AbortSignal is aborted.
|
|
6
|
+
* It's designed to make Promise awaits abortable.
|
|
7
7
|
*
|
|
8
8
|
* @example
|
|
9
9
|
* ```typescript
|
|
10
10
|
* const controller = new AbortController();
|
|
11
11
|
*
|
|
12
12
|
* try {
|
|
13
|
-
* const result = await
|
|
14
|
-
*
|
|
15
|
-
* abortable(controller.signal)
|
|
16
|
-
* ]);
|
|
13
|
+
* const result = await abortable(fetch('https://api.example.com/data'), controller.signal);
|
|
14
|
+
* // Meanwhile, to abort in concurrent code before the above line returns: controller.abort();
|
|
17
15
|
* } catch (err) {
|
|
18
16
|
* if (err.name === 'AbortError') {
|
|
19
17
|
* console.log('The operation was aborted');
|
|
20
18
|
* }
|
|
21
19
|
* }
|
|
22
20
|
*
|
|
23
|
-
* // Later, to abort:
|
|
24
|
-
* controller.abort();
|
|
25
21
|
* ```
|
|
26
22
|
*
|
|
23
|
+
* @param p - A Promise to wrap
|
|
27
24
|
* @param signal - An AbortSignal that can be used to abort the operation
|
|
28
|
-
* @
|
|
29
|
-
*
|
|
30
|
-
* @throws {DOMException} With name "AbortError"
|
|
25
|
+
* @returns A wrapper Promise that rejects with AbortError if the signal is aborted
|
|
26
|
+
* before the promise p settles, and settles as p settles otherwise
|
|
27
|
+
* @throws {DOMException} With name "AbortError" if aborted before p settles
|
|
31
28
|
*/
|
|
32
|
-
export function abortable(
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
return new Promise((_, reject) => {
|
|
40
|
-
signal.addEventListener("abort", () => {
|
|
41
|
-
cleanup?.();
|
|
42
|
-
reject(new DOMException("Operation aborted", "AbortError"));
|
|
29
|
+
export function abortable(p, signal) {
|
|
30
|
+
let settled = false;
|
|
31
|
+
return new Promise((resolve, reject) => {
|
|
32
|
+
signal?.addEventListener("abort", () => {
|
|
33
|
+
if (!settled) {
|
|
34
|
+
reject(new DOMException("Operation aborted", "AbortError"));
|
|
35
|
+
}
|
|
43
36
|
}, { once: true });
|
|
37
|
+
p.then(result => {
|
|
38
|
+
resolve(result);
|
|
39
|
+
})
|
|
40
|
+
.catch(error => {
|
|
41
|
+
reject(error);
|
|
42
|
+
})
|
|
43
|
+
.finally(() => {
|
|
44
|
+
settled = true;
|
|
45
|
+
});
|
|
44
46
|
});
|
|
45
47
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@automerge/automerge-repo",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.5",
|
|
4
4
|
"description": "A repository object to manage a collection of automerge documents",
|
|
5
5
|
"repository": "https://github.com/automerge/automerge-repo/tree/master/packages/automerge-repo",
|
|
6
6
|
"author": "Peter van Hardenberg <pvh@pvh.ca>",
|
|
@@ -59,5 +59,5 @@
|
|
|
59
59
|
"publishConfig": {
|
|
60
60
|
"access": "public"
|
|
61
61
|
},
|
|
62
|
-
"gitHead": "
|
|
62
|
+
"gitHead": "0a1f8195dbe0a84c21611c5f58003b68f35b9dc1"
|
|
63
63
|
}
|
package/src/Repo.ts
CHANGED
|
@@ -432,8 +432,6 @@ export class Repo extends EventEmitter<RepoEvents> {
|
|
|
432
432
|
options: AbortOptions = {}
|
|
433
433
|
): FindProgressWithMethods<T> | FindProgress<T> {
|
|
434
434
|
const { signal } = options
|
|
435
|
-
const abortPromise = abortable(signal)
|
|
436
|
-
|
|
437
435
|
const { documentId, heads } = isValidAutomergeUrl(id)
|
|
438
436
|
? parseAutomergeUrl(id)
|
|
439
437
|
: { documentId: interpretAsDocumentId(id), heads: undefined }
|
|
@@ -514,7 +512,7 @@ export class Repo extends EventEmitter<RepoEvents> {
|
|
|
514
512
|
documentId,
|
|
515
513
|
handle,
|
|
516
514
|
progressSignal,
|
|
517
|
-
|
|
515
|
+
signal ? abortable(new Promise(() => {}), signal) : new Promise(() => {})
|
|
518
516
|
)
|
|
519
517
|
|
|
520
518
|
const result = {
|
|
@@ -597,6 +595,12 @@ export class Repo extends EventEmitter<RepoEvents> {
|
|
|
597
595
|
options: RepoFindOptions & AbortOptions = {}
|
|
598
596
|
): Promise<DocHandle<T>> {
|
|
599
597
|
const { allowableStates = ["ready"], signal } = options
|
|
598
|
+
|
|
599
|
+
// Check if already aborted
|
|
600
|
+
if (signal?.aborted) {
|
|
601
|
+
throw new Error("Operation aborted")
|
|
602
|
+
}
|
|
603
|
+
|
|
600
604
|
const progress = this.findWithProgress<T>(id, { signal })
|
|
601
605
|
|
|
602
606
|
if ("subscribe" in progress) {
|
|
@@ -669,7 +673,7 @@ export class Repo extends EventEmitter<RepoEvents> {
|
|
|
669
673
|
const documentId = interpretAsDocumentId(id)
|
|
670
674
|
const { allowableStates, signal } = options
|
|
671
675
|
|
|
672
|
-
return
|
|
676
|
+
return abortable(
|
|
673
677
|
(async () => {
|
|
674
678
|
const handle = await this.#loadDocument<T>(documentId)
|
|
675
679
|
if (!allowableStates) {
|
|
@@ -680,8 +684,8 @@ export class Repo extends EventEmitter<RepoEvents> {
|
|
|
680
684
|
}
|
|
681
685
|
return handle
|
|
682
686
|
})(),
|
|
683
|
-
|
|
684
|
-
|
|
687
|
+
signal
|
|
688
|
+
)
|
|
685
689
|
}
|
|
686
690
|
|
|
687
691
|
delete(
|
package/src/helpers/abortable.ts
CHANGED
|
@@ -1,55 +1,56 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Wraps a Promise and causes it to reject when the signal is aborted.
|
|
3
3
|
*
|
|
4
4
|
* @remarks
|
|
5
|
-
* This utility
|
|
6
|
-
* It's designed to
|
|
5
|
+
* This utility wraps a Promise and rejects when the provided AbortSignal is aborted.
|
|
6
|
+
* It's designed to make Promise awaits abortable.
|
|
7
7
|
*
|
|
8
8
|
* @example
|
|
9
9
|
* ```typescript
|
|
10
10
|
* const controller = new AbortController();
|
|
11
11
|
*
|
|
12
12
|
* try {
|
|
13
|
-
* const result = await
|
|
14
|
-
*
|
|
15
|
-
* abortable(controller.signal)
|
|
16
|
-
* ]);
|
|
13
|
+
* const result = await abortable(fetch('https://api.example.com/data'), controller.signal);
|
|
14
|
+
* // Meanwhile, to abort in concurrent code before the above line returns: controller.abort();
|
|
17
15
|
* } catch (err) {
|
|
18
16
|
* if (err.name === 'AbortError') {
|
|
19
17
|
* console.log('The operation was aborted');
|
|
20
18
|
* }
|
|
21
19
|
* }
|
|
22
20
|
*
|
|
23
|
-
* // Later, to abort:
|
|
24
|
-
* controller.abort();
|
|
25
21
|
* ```
|
|
26
22
|
*
|
|
23
|
+
* @param p - A Promise to wrap
|
|
27
24
|
* @param signal - An AbortSignal that can be used to abort the operation
|
|
28
|
-
* @
|
|
29
|
-
*
|
|
30
|
-
* @throws {DOMException} With name "AbortError"
|
|
25
|
+
* @returns A wrapper Promise that rejects with AbortError if the signal is aborted
|
|
26
|
+
* before the promise p settles, and settles as p settles otherwise
|
|
27
|
+
* @throws {DOMException} With name "AbortError" if aborted before p settles
|
|
31
28
|
*/
|
|
32
|
-
export function abortable(
|
|
33
|
-
signal?: AbortSignal,
|
|
34
|
-
cleanup?: () => void
|
|
35
|
-
): Promise<never> {
|
|
36
|
-
if (signal?.aborted) {
|
|
37
|
-
throw new DOMException("Operation aborted", "AbortError")
|
|
38
|
-
}
|
|
39
29
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
30
|
+
export function abortable<T>(
|
|
31
|
+
p: Promise<T>,
|
|
32
|
+
signal: AbortSignal | undefined
|
|
33
|
+
): Promise<T> {
|
|
34
|
+
let settled = false
|
|
35
|
+
return new Promise((resolve, reject) => {
|
|
36
|
+
signal?.addEventListener(
|
|
46
37
|
"abort",
|
|
47
38
|
() => {
|
|
48
|
-
|
|
49
|
-
|
|
39
|
+
if (!settled) {
|
|
40
|
+
reject(new DOMException("Operation aborted", "AbortError"))
|
|
41
|
+
}
|
|
50
42
|
},
|
|
51
43
|
{ once: true }
|
|
52
44
|
)
|
|
45
|
+
p.then(result => {
|
|
46
|
+
resolve(result)
|
|
47
|
+
})
|
|
48
|
+
.catch(error => {
|
|
49
|
+
reject(error)
|
|
50
|
+
})
|
|
51
|
+
.finally(() => {
|
|
52
|
+
settled = true
|
|
53
|
+
})
|
|
53
54
|
})
|
|
54
55
|
}
|
|
55
56
|
|