Skip to content

Commit

Permalink
emit event when unsyncedChanges changes, fixes #548 (#574)
Browse files Browse the repository at this point in the history
* emit event when unsyncedChanges changes, fixes #548

* reset unsyncedChanges on synced event
  • Loading branch information
janthurau authored Apr 5, 2023
1 parent e6b973c commit c4efcf2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 11 additions & 2 deletions packages/provider/src/HocuspocusProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,15 @@ export class HocuspocusProvider extends EventEmitter {
return this.configuration.awareness
}

get hasUnsyncedChanges() {
get hasUnsyncedChanges(): boolean {
return this.unsyncedChanges > 0
}

updateUnsyncedChanges(unsyncedChanges = 0) {
this.unsyncedChanges += unsyncedChanges
this.emit('unsyncedChanges', this.unsyncedChanges)
}

forceSync() {
this.send(SyncStepOneMessage, { document: this.document, documentName: this.configuration.name })
}
Expand Down Expand Up @@ -263,7 +268,7 @@ export class HocuspocusProvider extends EventEmitter {
return
}

this.unsyncedChanges += 1
this.updateUnsyncedChanges(1)
this.send(UpdateMessage, { update, documentName: this.configuration.name }, true)
}

Expand All @@ -286,6 +291,10 @@ export class HocuspocusProvider extends EventEmitter {
return
}

if (state && this.unsyncedChanges > 0) {
this.updateUnsyncedChanges(-1 * this.unsyncedChanges)
}

this.isSynced = state
this.emit('synced', { state })
this.emit('sync', { state })
Expand Down
2 changes: 1 addition & 1 deletion packages/provider/src/MessageReceiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class MessageReceiver {

if (syncMessageType === messageYjsUpdate || syncMessageType === messageYjsSyncStep2) {
if (provider.unsyncedChanges > 0) {
provider.unsyncedChanges -= 1
provider.updateUnsyncedChanges(-1)
}
}
}
Expand Down

0 comments on commit c4efcf2

Please sign in to comment.