@automerge/automerge-repo 2.0.0-alpha.20 → 2.0.0-alpha.22

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.
@@ -1,7 +1,7 @@
1
1
  import * as A from "@automerge/automerge/next"
2
2
  import assert from "assert"
3
3
  import { decode } from "cbor-x"
4
- import { describe, it, vi } from "vitest"
4
+ import { describe, expect, it, vi } from "vitest"
5
5
  import {
6
6
  encodeHeads,
7
7
  generateAutomergeUrl,
@@ -11,7 +11,6 @@ import { eventPromise } from "../src/helpers/eventPromise.js"
11
11
  import { pause } from "../src/helpers/pause.js"
12
12
  import { DocHandle, DocHandleChangePayload } from "../src/index.js"
13
13
  import { TestDoc } from "./types.js"
14
- import { UNLOADED } from "../src/DocHandle.js"
15
14
 
16
15
  describe("DocHandle", () => {
17
16
  const TEST_ID = parseAutomergeUrl(generateAutomergeUrl()).documentId
@@ -39,7 +38,7 @@ describe("DocHandle", () => {
39
38
  handle.update(doc => docFromMockStorage(doc))
40
39
 
41
40
  assert.equal(handle.isReady(), true)
42
- const doc = await handle.doc()
41
+ const doc = handle.doc()
43
42
  assert.equal(doc?.foo, "bar")
44
43
  })
45
44
 
@@ -51,13 +50,13 @@ describe("DocHandle", () => {
51
50
  handle.update(doc => docFromMockStorage(doc))
52
51
 
53
52
  assert.equal(handle.isReady(), true)
54
- const doc = await handle.doc()
55
- assert.deepEqual(doc, handle.docSync())
53
+ const doc = handle.doc()
54
+ assert.deepEqual(doc, handle.doc())
56
55
  })
57
56
 
58
- it("should return undefined if we access the doc before ready", async () => {
57
+ it("should throw an exception if we access the doc before ready", async () => {
59
58
  const handle = new DocHandle<TestDoc>(TEST_ID)
60
- assert.equal(handle.docSync(), undefined)
59
+ assert.throws(() => handle.doc())
61
60
  })
62
61
 
63
62
  it("should not return a doc until ready", async () => {
@@ -67,7 +66,7 @@ describe("DocHandle", () => {
67
66
  // simulate loading from storage
68
67
  handle.update(doc => docFromMockStorage(doc))
69
68
 
70
- const doc = await handle.doc()
69
+ const doc = handle.doc()
71
70
 
72
71
  assert.equal(handle.isReady(), true)
73
72
  assert.equal(doc?.foo, "bar")
@@ -87,15 +86,15 @@ describe("DocHandle", () => {
87
86
  handle.change(d => (d.foo = "bar"))
88
87
  assert.equal(handle.isReady(), true)
89
88
 
90
- const heads = encodeHeads(A.getHeads(handle.docSync()))
89
+ const heads = encodeHeads(A.getHeads(handle.doc()))
91
90
  assert.notDeepEqual(handle.heads(), [])
92
91
  assert.deepEqual(heads, handle.heads())
93
92
  })
94
93
 
95
- it("should return undefined if the heads aren't loaded", async () => {
94
+ it("should throw an if the heads aren't loaded", async () => {
96
95
  const handle = new DocHandle<TestDoc>(TEST_ID)
97
96
  assert.equal(handle.isReady(), false)
98
- assert.deepEqual(handle.heads(), undefined)
97
+ expect(() => handle.heads()).toThrow("DocHandle is not ready")
99
98
  })
100
99
 
101
100
  it("should return the history when requested", async () => {
@@ -128,7 +127,7 @@ describe("DocHandle", () => {
128
127
 
129
128
  const history = handle.history()
130
129
  const viewHandle = new DocHandle<TestDoc>(TEST_ID, { heads: history[0] })
131
- viewHandle.update(() => A.clone(handle.docSync()!))
130
+ viewHandle.update(() => A.clone(handle.doc()!))
132
131
  viewHandle.doneLoading()
133
132
 
134
133
  assert.deepEqual(await viewHandle.doc(), { foo: "zero" })
@@ -260,7 +259,7 @@ describe("DocHandle", () => {
260
259
  const handle = new DocHandle<TestDoc>(TEST_ID)
261
260
  assert.equal(handle.isReady(), false)
262
261
 
263
- handle.doc()
262
+ handle.legacyAsyncDoc()
264
263
 
265
264
  assert(vi.getTimerCount() > timerCount)
266
265
 
@@ -286,7 +285,7 @@ describe("DocHandle", () => {
286
285
  assert.equal(handle.isReady(), true)
287
286
  handle.change(d => (d.foo = "pizza"))
288
287
 
289
- const doc = await handle.doc()
288
+ const doc = handle.doc()
290
289
  assert.equal(doc?.foo, "pizza")
291
290
  })
292
291
 
@@ -296,7 +295,9 @@ describe("DocHandle", () => {
296
295
  // we don't have it in storage, so we request it from the network
297
296
  handle.request()
298
297
 
299
- assert.equal(handle.docSync(), undefined)
298
+ await expect(() => {
299
+ handle.doc()
300
+ }).toThrowError("DocHandle is not ready")
300
301
  assert.equal(handle.isReady(), false)
301
302
  assert.throws(() => handle.change(_ => {}))
302
303
  })
@@ -312,7 +313,7 @@ describe("DocHandle", () => {
312
313
  return A.change(doc, d => (d.foo = "bar"))
313
314
  })
314
315
 
315
- const doc = await handle.doc()
316
+ const doc = handle.doc()
316
317
  assert.equal(handle.isReady(), true)
317
318
  assert.equal(doc?.foo, "bar")
318
319
  })
@@ -328,7 +329,7 @@ describe("DocHandle", () => {
328
329
  doc.foo = "bar"
329
330
  })
330
331
 
331
- const doc = await handle.doc()
332
+ const doc = handle.doc()
332
333
  assert.equal(doc?.foo, "bar")
333
334
 
334
335
  const changePayload = await p
@@ -353,7 +354,7 @@ describe("DocHandle", () => {
353
354
 
354
355
  const p = new Promise<void>(resolve =>
355
356
  handle.once("change", ({ handle, doc }) => {
356
- assert.equal(handle.docSync()?.foo, doc.foo)
357
+ assert.equal(handle.doc()?.foo, doc.foo)
357
358
 
358
359
  resolve()
359
360
  })
@@ -390,7 +391,7 @@ describe("DocHandle", () => {
390
391
  doc.foo = "baz"
391
392
  })
392
393
 
393
- const doc = await handle.doc()
394
+ const doc = handle.doc()
394
395
  assert.equal(doc?.foo, "baz")
395
396
 
396
397
  return p
@@ -405,7 +406,7 @@ describe("DocHandle", () => {
405
406
  })
406
407
 
407
408
  await p
408
- const doc = await handle.doc()
409
+ const doc = handle.doc()
409
410
  assert.equal(doc?.foo, "bar")
410
411
  })
411
412
 
@@ -425,11 +426,7 @@ describe("DocHandle", () => {
425
426
  // set docHandle time out after 5 ms
426
427
  const handle = new DocHandle<TestDoc>(TEST_ID, { timeoutDelay: 5 })
427
428
 
428
- const doc = await handle.doc()
429
-
430
- assert.equal(doc, undefined)
431
-
432
- assert.equal(handle.state, "unavailable")
429
+ expect(() => handle.doc()).toThrowError("DocHandle is not ready")
433
430
  })
434
431
 
435
432
  it("should not time out if the document is loaded in time", async () => {
@@ -440,11 +437,11 @@ describe("DocHandle", () => {
440
437
  handle.update(doc => docFromMockStorage(doc))
441
438
 
442
439
  // now it should not time out
443
- const doc = await handle.doc()
440
+ const doc = handle.doc()
444
441
  assert.equal(doc?.foo, "bar")
445
442
  })
446
443
 
447
- it("should be undefined if loading from the network times out", async () => {
444
+ it("should throw an exception if loading from the network times out", async () => {
448
445
  // set docHandle time out after 5 ms
449
446
  const handle = new DocHandle<TestDoc>(TEST_ID, { timeoutDelay: 5 })
450
447
 
@@ -454,8 +451,7 @@ describe("DocHandle", () => {
454
451
  // there's no update
455
452
  await pause(10)
456
453
 
457
- const doc = await handle.doc()
458
- assert.equal(doc, undefined)
454
+ expect(() => handle.doc()).toThrowError("DocHandle is not ready")
459
455
  })
460
456
 
461
457
  it("should not time out if the document is updated in time", async () => {
@@ -473,7 +469,7 @@ describe("DocHandle", () => {
473
469
  // now it should not time out
474
470
  await pause(5)
475
471
 
476
- const doc = await handle.doc()
472
+ const doc = handle.doc()
477
473
  assert.equal(doc?.foo, "bar")
478
474
  })
479
475
 
@@ -489,49 +485,6 @@ describe("DocHandle", () => {
489
485
  assert.equal(handle.isDeleted(), true)
490
486
  })
491
487
 
492
- it("should clear document reference when unloaded", async () => {
493
- const handle = setup()
494
-
495
- handle.change(doc => {
496
- doc.foo = "bar"
497
- })
498
- const doc = await handle.doc()
499
- assert.equal(doc?.foo, "bar")
500
-
501
- handle.unload()
502
- assert.equal(handle.isUnloaded(), true)
503
-
504
- const clearedDoc = await handle.doc([UNLOADED])
505
- assert.notEqual(clearedDoc?.foo, "bar")
506
- })
507
-
508
- it("should allow reloading after unloading", async () => {
509
- const handle = setup()
510
-
511
- handle.change(doc => {
512
- doc.foo = "bar"
513
- })
514
- const doc = await handle.doc()
515
- assert.equal(doc?.foo, "bar")
516
-
517
- handle.unload()
518
-
519
- // reload to transition from unloaded to loading
520
- handle.reload()
521
-
522
- // simulate requesting from the network
523
- handle.request()
524
-
525
- // simulate updating from the network
526
- handle.update(doc => {
527
- return A.change(doc, d => (d.foo = "bar"))
528
- })
529
-
530
- const reloadedDoc = await handle.doc()
531
- assert.equal(handle.isReady(), true)
532
- assert.equal(reloadedDoc?.foo, "bar")
533
- })
534
-
535
488
  it("should allow changing at old heads", async () => {
536
489
  const handle = setup()
537
490