Skip to content

Commit

Permalink
explicitly initiate a sync on create, import, or clone with all conne…
Browse files Browse the repository at this point in the history
…cted peers
  • Loading branch information
heckj committed Jul 3, 2024
1 parent 5300a5a commit da50f68
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions Sources/AutomergeRepo/Repo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -360,16 +360,20 @@ public final class Repo {
.sink { [weak self] _ in
guard let self else { return }
self.saveSignalPublisher.send(id)
for peer in self.peerMetadataByPeerId.keys {
Task {
await self.beginSync(docId: id, to: peer)
}
}
syncToAllPeers(id: id)
}
observerHandles[id] = handleObserver
}
}

func syncToAllPeers(id: DocumentId) {
for peer in self.peerMetadataByPeerId.keys {
Task {
await self.beginSync(docId: id, to: peer)
}
}
}

// MARK: Synchronization Pieces - For Network Subsystem Access

func handleSync(msg: SyncV1Msg.SyncMsg) async {
Expand Down Expand Up @@ -512,6 +516,7 @@ public final class Repo {
handles[handle.id] = handle
docHandlePublisher.send(handle.snapshot())
let resolved = try await resolveDocHandle(id: handle.id)
syncToAllPeers(id: handle.id)
return resolved
}

Expand All @@ -526,12 +531,13 @@ public final class Repo {
handles[handle.id] = handle
docHandlePublisher.send(handle.snapshot())
let resolved = try await resolveDocHandle(id: handle.id)
syncToAllPeers(id: handle.id)
return resolved
}

/// Imports an Automerge document with an option Id.
/// - Parameter handle: The handle to the Automerge document to import
/// - Returns: A handle to the Automerge document
/// - Parameter handle: The handle to the Automerge document to import.
/// - Returns: A handle to the Automerge document.
///
/// If the ID you provide in the handle already exists in the local repository,
/// the import attempts to merge the document you provide with an existing document.
Expand All @@ -547,6 +553,7 @@ public final class Repo {
/// ```
///
/// Use the handle you create with `import(handle:)` to load it into the repository.
@discardableResult
public func `import`(handle: DocHandle) async throws -> DocHandle {
if let existingHandle = handles[handle.id] {
if [.loading, .requesting].contains(existingHandle.state) {
Expand Down Expand Up @@ -579,7 +586,9 @@ public final class Repo {
// calling resolveDocHandle when we've just set the state to .ready
// is tantamount to asking for a DocHandle from the internal variation
// but with some extra checks to make sure nothing is awry with expected state.
return try await resolveDocHandle(id: existingHandle.id)
let handle = try await resolveDocHandle(id: existingHandle.id)
syncToAllPeers(id: handle.id)
return handle
} else {
// Id from the handle provided does not yet exist within the repository
// establish a new internal doc handle
Expand All @@ -590,13 +599,15 @@ public final class Repo {
// explicitly save to persistent storage, if available, before returning
try await storage?.saveDoc(id: handle.id, doc: handle.doc)
docHandlePublisher.send(internalHandle.snapshot())
return try await resolveDocHandle(id: handle.id)
let handle = try await resolveDocHandle(id: handle.id)
syncToAllPeers(id: handle.id)
return handle
}
}

/// Clones a document the repo already knows to create a new, shared document.
/// - Parameter id: The id of the document to clone.
/// - Returns: The Automerge document.
/// - Returns: A handle to the Automerge document.
public func clone(id: DocumentId) async throws -> DocHandle {
let handle = try await resolveDocHandle(id: id)
let fork = handle.doc.fork()
Expand All @@ -605,9 +616,13 @@ public final class Repo {
handles[newHandle.id] = newHandle
docHandlePublisher.send(newHandle.snapshot())
let resolved = try await resolveDocHandle(id: newHandle.id)
syncToAllPeers(id: resolved.id)
return resolved
}


/// Requests a document from any connected peers.
/// - Parameter id: The id of the document to retrieve.
/// - Returns: A handle to the Automerge document or throws an error if the document is unavailable.
public func find(id: DocumentId) async throws -> DocHandle {
// generally of the idea that we'll drive DocHandle state updates from within Repo
// and these async methods
Expand Down

0 comments on commit da50f68

Please sign in to comment.