@automerge/automerge-repo
Advanced tools
Comparing version 2.0.0-alpha.22 to 2.0.0-alpha.23
@@ -68,11 +68,2 @@ import * as A from "@automerge/automerge/slim/next"; | ||
/** | ||
* @returns the current state of this handle's Automerge document. | ||
* | ||
* This is the recommended way to access a handle's document. Note that this waits for the handle | ||
* to be ready if necessary. If loading (or synchronization) fails, this will never resolve. | ||
*/ | ||
legacyAsyncDoc( | ||
/** states to wait for, such as "LOADING". mostly for internal use. */ | ||
awaitStates?: HandleState[]): Promise<A.Doc<T> | undefined>; | ||
/** | ||
* Returns the current state of the Automerge document this handle manages. | ||
@@ -79,0 +70,0 @@ * |
@@ -232,22 +232,2 @@ import * as A from "@automerge/automerge/slim/next"; | ||
/** | ||
* @returns the current state of this handle's Automerge document. | ||
* | ||
* This is the recommended way to access a handle's document. Note that this waits for the handle | ||
* to be ready if necessary. If loading (or synchronization) fails, this will never resolve. | ||
*/ | ||
async legacyAsyncDoc( | ||
/** states to wait for, such as "LOADING". mostly for internal use. */ | ||
awaitStates = ["ready", "unavailable"]) { | ||
try { | ||
// wait for the document to enter one of the desired states | ||
await this.#statePromise(awaitStates); | ||
} | ||
catch (error) { | ||
// if we timed out, return undefined | ||
return undefined; | ||
} | ||
// Return the document | ||
return !this.isUnavailable() ? this.#doc : undefined; | ||
} | ||
/** | ||
* Returns the current state of the Automerge document this handle manages. | ||
@@ -537,3 +517,3 @@ * | ||
handle: this, | ||
data: encode(message), | ||
data: new Uint8Array(encode(message)), | ||
}); | ||
@@ -540,0 +520,0 @@ } |
@@ -40,3 +40,2 @@ import * as A from "@automerge/automerge/slim/next"; | ||
void (async () => { | ||
await handle.whenReady([READY, REQUESTING]); | ||
this.#processAllPendingSyncMessages(); | ||
@@ -53,6 +52,10 @@ })(); | ||
async #syncWithPeers() { | ||
const doc = await this.#handle.legacyAsyncDoc(); // XXX THIS ONE IS WEIRD | ||
if (doc === undefined) | ||
return; | ||
this.#peers.forEach(peerId => this.#sendSyncMessage(peerId, doc)); | ||
try { | ||
await this.#handle.whenReady(); | ||
const doc = this.#handle.doc(); // XXX THIS ONE IS WEIRD | ||
this.#peers.forEach(peerId => this.#sendSyncMessage(peerId, doc)); | ||
} | ||
catch (e) { | ||
console.log("sync with peers threw an exception"); | ||
} | ||
} | ||
@@ -160,18 +163,12 @@ async #broadcastToPeers({ data, }) { | ||
async beginSync(peerIds) { | ||
const noPeersWithDocument = peerIds.every(peerId => this.#peerDocumentStatuses[peerId] in ["unavailable", "wants"]); | ||
// At this point if we don't have anything in our storage, we need to use an empty doc to sync | ||
// with; but we don't want to surface that state to the front end | ||
const docPromise = this.#handle // TODO THIS IS ALSO WEIRD | ||
.legacyAsyncDoc([READY, REQUESTING, UNAVAILABLE]) | ||
.then(doc => { | ||
// we register out peers first, then say that sync has started | ||
void this.#handle | ||
.whenReady([READY, REQUESTING, UNAVAILABLE]) | ||
.then(() => { | ||
this.#syncStarted = true; | ||
this.#checkDocUnavailable(); | ||
const wasUnavailable = doc === undefined; | ||
if (wasUnavailable && noPeersWithDocument) { | ||
return; | ||
} | ||
// If the doc is unavailable we still need a blank document to generate | ||
// the sync message from | ||
return doc ?? A.init(); | ||
}) | ||
.catch(e => { | ||
console.log("caught whenready", e); | ||
this.#syncStarted = true; | ||
this.#checkDocUnavailable(); | ||
}); | ||
@@ -192,7 +189,18 @@ const peersWithDocument = this.#peers.some(peerId => { | ||
this.#setSyncState(peerId, reparsedSyncState); | ||
docPromise | ||
.then(doc => { | ||
if (doc) { | ||
this.#sendSyncMessage(peerId, doc); | ||
// At this point if we don't have anything in our storage, we need to use an empty doc to sync | ||
// with; but we don't want to surface that state to the front end | ||
this.#handle | ||
.whenReady([READY, REQUESTING, UNAVAILABLE]) | ||
.then(() => { | ||
const doc = this.#handle.isReady() | ||
? this.#handle.doc() | ||
: A.init(); | ||
const noPeersWithDocument = peerIds.every(peerId => this.#peerDocumentStatuses[peerId] in ["unavailable", "wants"]); | ||
const wasUnavailable = doc === undefined; | ||
if (wasUnavailable && noPeersWithDocument) { | ||
return; | ||
} | ||
// If the doc is unavailable we still need a blank document to generate | ||
// the sync message from | ||
this.#sendSyncMessage(peerId, doc ?? A.init()); | ||
}) | ||
@@ -199,0 +207,0 @@ .catch(err => { |
{ | ||
"name": "@automerge/automerge-repo", | ||
"version": "2.0.0-alpha.22", | ||
"version": "2.0.0-alpha.23", | ||
"description": "A repository object to manage a collection of automerge documents", | ||
@@ -23,2 +23,3 @@ "repository": "https://github.com/automerge/automerge-repo/tree/master/packages/automerge-repo", | ||
"http-server": "^14.1.0", | ||
"ts-node": "^10.9.2", | ||
"vite": "^5.0.8" | ||
@@ -33,4 +34,2 @@ }, | ||
"fast-sha256": "^1.3.0", | ||
"tiny-typed-emitter": "^2.1.0", | ||
"ts-node": "^10.9.1", | ||
"uuid": "^9.0.0", | ||
@@ -65,3 +64,3 @@ "xstate": "^5.9.1" | ||
}, | ||
"gitHead": "b30af9827bed4615ba3c5e9ee93ca483915e4016" | ||
"gitHead": "82a9bed7cc6a92940f3aa68167f6990123dda58e" | ||
} |
@@ -235,3 +235,3 @@ # Automerge Repo | ||
// main.tsx | ||
import { BrowserWebSocketClientAdapter } from "@automerge/automerge-repo-network-websocket" // <-- add this line | ||
import { WebSocketClientAdapter } from "@automerge/automerge-repo-network-websocket" // <-- add this line | ||
@@ -243,3 +243,3 @@ // ... | ||
new BroadcastChannelNetworkAdapter(), | ||
new BrowserWebSocketClientAdapter("ws://localhost:3030"), // <-- add this line | ||
new WebSocketClientAdapter("ws://localhost:3030"), // <-- add this line | ||
], | ||
@@ -266,1 +266,2 @@ storage: new IndexedDBStorageAdapter(), | ||
- Dylan Mackenzie | ||
- Maciek Sakrejda |
@@ -279,23 +279,2 @@ import * as A from "@automerge/automerge/slim/next" | ||
/** | ||
* @returns the current state of this handle's Automerge document. | ||
* | ||
* This is the recommended way to access a handle's document. Note that this waits for the handle | ||
* to be ready if necessary. If loading (or synchronization) fails, this will never resolve. | ||
*/ | ||
async legacyAsyncDoc( | ||
/** states to wait for, such as "LOADING". mostly for internal use. */ | ||
awaitStates: HandleState[] = ["ready", "unavailable"] | ||
) { | ||
try { | ||
// wait for the document to enter one of the desired states | ||
await this.#statePromise(awaitStates) | ||
} catch (error) { | ||
// if we timed out, return undefined | ||
return undefined | ||
} | ||
// Return the document | ||
return !this.isUnavailable() ? this.#doc : undefined | ||
} | ||
/** | ||
* Returns the current state of the Automerge document this handle manages. | ||
@@ -639,3 +618,3 @@ * | ||
handle: this, | ||
data: encode(message), | ||
data: new Uint8Array(encode(message)), | ||
}) | ||
@@ -642,0 +621,0 @@ } |
@@ -89,3 +89,2 @@ import * as A from "@automerge/automerge/slim/next" | ||
void (async () => { | ||
await handle.whenReady([READY, REQUESTING]) | ||
this.#processAllPendingSyncMessages() | ||
@@ -106,5 +105,9 @@ })() | ||
async #syncWithPeers() { | ||
const doc = await this.#handle.legacyAsyncDoc() // XXX THIS ONE IS WEIRD | ||
if (doc === undefined) return | ||
this.#peers.forEach(peerId => this.#sendSyncMessage(peerId, doc)) | ||
try { | ||
await this.#handle.whenReady() | ||
const doc = this.#handle.doc() // XXX THIS ONE IS WEIRD | ||
this.#peers.forEach(peerId => this.#sendSyncMessage(peerId, doc)) | ||
} catch (e) { | ||
console.log("sync with peers threw an exception") | ||
} | ||
} | ||
@@ -236,24 +239,13 @@ | ||
async beginSync(peerIds: PeerId[]) { | ||
const noPeersWithDocument = peerIds.every( | ||
peerId => this.#peerDocumentStatuses[peerId] in ["unavailable", "wants"] | ||
) | ||
// At this point if we don't have anything in our storage, we need to use an empty doc to sync | ||
// with; but we don't want to surface that state to the front end | ||
const docPromise = this.#handle // TODO THIS IS ALSO WEIRD | ||
.legacyAsyncDoc([READY, REQUESTING, UNAVAILABLE]) | ||
.then(doc => { | ||
// we register out peers first, then say that sync has started | ||
void this.#handle | ||
.whenReady([READY, REQUESTING, UNAVAILABLE]) | ||
.then(() => { | ||
this.#syncStarted = true | ||
this.#checkDocUnavailable() | ||
const wasUnavailable = doc === undefined | ||
if (wasUnavailable && noPeersWithDocument) { | ||
return | ||
} | ||
// If the doc is unavailable we still need a blank document to generate | ||
// the sync message from | ||
return doc ?? A.init<unknown>() | ||
}) | ||
.catch(e => { | ||
console.log("caught whenready", e) | ||
this.#syncStarted = true | ||
this.#checkDocUnavailable() | ||
}) | ||
@@ -279,7 +271,24 @@ const peersWithDocument = this.#peers.some(peerId => { | ||
docPromise | ||
.then(doc => { | ||
if (doc) { | ||
this.#sendSyncMessage(peerId, doc) | ||
// At this point if we don't have anything in our storage, we need to use an empty doc to sync | ||
// with; but we don't want to surface that state to the front end | ||
this.#handle | ||
.whenReady([READY, REQUESTING, UNAVAILABLE]) | ||
.then(() => { | ||
const doc = this.#handle.isReady() | ||
? this.#handle.doc() | ||
: A.init<unknown>() | ||
const noPeersWithDocument = peerIds.every( | ||
peerId => | ||
this.#peerDocumentStatuses[peerId] in ["unavailable", "wants"] | ||
) | ||
const wasUnavailable = doc === undefined | ||
if (wasUnavailable && noPeersWithDocument) { | ||
return | ||
} | ||
// If the doc is unavailable we still need a blank document to generate | ||
// the sync message from | ||
this.#sendSyncMessage(peerId, doc ?? A.init<unknown>()) | ||
}) | ||
@@ -286,0 +295,0 @@ .catch(err => { |
@@ -262,4 +262,2 @@ import * as A from "@automerge/automerge/next" | ||
handle.legacyAsyncDoc() | ||
assert(vi.getTimerCount() > timerCount) | ||
@@ -266,0 +264,0 @@ |
@@ -258,3 +258,2 @@ import { next as A } from "@automerge/automerge" | ||
const bobHandle = await repo2.find<TestDoc>(handle.url) | ||
await bobHandle.whenReady() | ||
assert.deepEqual(bobHandle.doc(), { foo: "saved" }) | ||
@@ -310,3 +309,2 @@ }) | ||
assert.equal(handle.isReady(), true) | ||
await handle.whenReady() | ||
@@ -1068,7 +1066,2 @@ await pause() | ||
// make sure the doc is ready | ||
if (!doc.isReady()) { | ||
await doc.whenReady() | ||
} | ||
// make a random change to it | ||
@@ -1246,3 +1239,2 @@ doc.change(d => { | ||
const charlieHandle = await charlieRepo.find<TestDoc>(handle.url) | ||
await charlieHandle.whenReady() | ||
@@ -1249,0 +1241,0 @@ // make a change on charlie |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
8
265
507112
3
11850
- Removedtiny-typed-emitter@^2.1.0
- Removedts-node@^10.9.1
- Removed@cspotcode/source-map-support@0.8.1(transitive)
- Removed@jridgewell/resolve-uri@3.1.2(transitive)
- Removed@jridgewell/sourcemap-codec@1.5.0(transitive)
- Removed@jridgewell/trace-mapping@0.3.9(transitive)
- Removed@tsconfig/node10@1.0.11(transitive)
- Removed@tsconfig/node12@1.0.11(transitive)
- Removed@tsconfig/node14@1.0.3(transitive)
- Removed@tsconfig/node16@1.0.4(transitive)
- Removed@types/node@22.13.8(transitive)
- Removedacorn@8.14.0(transitive)
- Removedacorn-walk@8.3.4(transitive)
- Removedarg@4.1.3(transitive)
- Removedcreate-require@1.1.1(transitive)
- Removeddiff@4.0.2(transitive)
- Removedmake-error@1.3.6(transitive)
- Removedtiny-typed-emitter@2.1.0(transitive)
- Removedts-node@10.9.2(transitive)
- Removedtypescript@5.8.2(transitive)
- Removedundici-types@6.20.0(transitive)
- Removedv8-compile-cache-lib@3.0.1(transitive)
- Removedyn@3.1.1(transitive)