Skip to content

Commit

Permalink
cleaning up sync issues and tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
heckj committed May 6, 2024
1 parent 2e05796 commit cd59173
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
27 changes: 16 additions & 11 deletions MeetingNotes/MeetingNotesDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ final class MeetingNotesDocument: ReferenceFileDocument {
}

syncedDocumentTrigger = doc.objectWillChange.sink {
Logger.syncflow.trace("\(self.id) ** objectWillChange **")
Logger.syncflow.trace("APPSYNC: \(self.id) ** objectWillChange **")
self.objectWillChange.send()
}
}
Expand All @@ -111,13 +111,18 @@ final class MeetingNotesDocument: ReferenceFileDocument {
// Set the identifier of this document, external from the Automerge document.
id = wrappedDocument.id
// Then deserialize the Automerge document from the wrappers data.
doc = try Document(wrappedDocument.data)
Logger.document
.debug(
"Created Automerge doc of ID \(self.id, privacy: .public) from CBOR encoded data of \(wrappedDocument.data.count, privacy: .public) bytes"
)
modelEncoder = AutomergeEncoder(doc: doc, strategy: .createWhenNeeded)
modelDecoder = AutomergeDecoder(doc: doc)
do {
doc = try Document(wrappedDocument.data)
Logger.document
.debug(
"Created Automerge doc of ID \(self.id, privacy: .public) from CBOR encoded data of \(wrappedDocument.data.count, privacy: .public) bytes"
)
modelEncoder = AutomergeEncoder(doc: doc, strategy: .createWhenNeeded)
modelDecoder = AutomergeDecoder(doc: doc)
} catch {
Logger.document.error("Failed to construct Automerge doc from data: \(error.localizedDescription)")
throw error
}
do {
model = try modelDecoder.decode(MeetingNotesModel.self)
} catch let DecodingError.dataCorrupted(context) {
Expand Down Expand Up @@ -152,7 +157,7 @@ final class MeetingNotesDocument: ReferenceFileDocument {
.throttle(for: 1.0, scheduler: DispatchQueue.main, latest: true)
.receive(on: RunLoop.main)
.sink {
Logger.syncflow.trace("\(self.id) ** objectWillChange (1 sec delay) **")
Logger.syncflow.trace("APPSYNC: \(self.id) ** Automerge document objectWillChange (1 sec delay) **")
do {
try self.getModelUpdates()
} catch {
Expand Down Expand Up @@ -197,14 +202,14 @@ final class MeetingNotesDocument: ReferenceFileDocument {

/// Updates the Automerge document with the current value from the model.
func storeModelUpdates() throws {
Logger.syncflow.debug("Storing model updates")
Logger.syncflow.debug("APPSYNC: Storing model updates")
try modelEncoder.encode(model)
self.objectWillChange.send()
}

/// Updates the model document with any changed values in the Automerge document.
func getModelUpdates() throws {
Logger.syncflow.debug("Loading model updates")
Logger.syncflow.debug("APPSYNC: Loading model updates")
model = try modelDecoder.decode(MeetingNotesModel.self)
}

Expand Down
10 changes: 7 additions & 3 deletions MeetingNotes/Views/PeerSyncView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,16 @@ struct PeerSyncView: View {
listenerState = state
})
.task {
// NOTE: this task gets invoked on _every_ re-appearance of the view - kind of the async
// equivalent of .onAppear() {} closure structure.
//
// The result is this bit if repeatedly redundant, but covers the case where the app is
// first coming online and a default "???" value should be set whatever the inline default
// from the library can provide. Since this is an @AppStorage() setup, if there's a configured
// setting, this won't get hit and we're just waiting cycles with the check.
if nameToDisplay == "???" {
// no user default is setup, so load a default value from the library
nameToDisplay = await peerToPeer.peerName
} else {
// overrides the library default name
await peerToPeer.setName(nameToDisplay)
}
}
}
Expand Down

0 comments on commit cd59173

Please sign in to comment.