Skip to content

Commit

Permalink
fix(sync): prevent duplicate pushes
Browse files Browse the repository at this point in the history
Document changes trigger awareness updates.
Wait 50ms before sending them in a push request
so they can be combined.

Signed-off-by: Max <[email protected]>
  • Loading branch information
max-nextcloud committed Jul 28, 2023
1 parent b7baceb commit 7412c4e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 20 deletions.
9 changes: 0 additions & 9 deletions src/components/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,6 @@ export default {
},
created() {
this.$ydoc = new Doc()
this.$ydoc.on('update', this.onYjsUpdate)
this.$providers = []
this.$editor = null
this.$syncService = null
Expand Down Expand Up @@ -508,7 +507,6 @@ export default {
: (session?.guestName || t('text', 'Guest')),
color: session?.color,
clientId: this.$ydoc.clientID,
lastUpdate: Date.now(),
},
}),
Keymap.configure({
Expand Down Expand Up @@ -644,13 +642,6 @@ export default {
this.emit('delete-image-node', imageUrl)
},
onYjsUpdate(_update, origin) {
if (origin.key === 'y-sync$') {
// Update timestamp of own cursor
this.$editor.commands.updateSelf()
}
},
async close() {
if (this.currentSession && this.$syncService) {
try {
Expand Down
19 changes: 11 additions & 8 deletions src/extensions/CollaborationCursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ function showCursorLabel(clientId) {
}, 50)
}

function getTimestamp() {

Check warning on line 23 in src/extensions/CollaborationCursor.js

View workflow job for this annotation

GitHub Actions / eslint

Missing JSDoc comment
return Math.floor(Date.now() / 1000)
}

const CollaborationCursor = TiptapCollaborationCursor.extend({
addOptions() {
return {
Expand All @@ -28,7 +32,7 @@ const CollaborationCursor = TiptapCollaborationCursor.extend({
name: null,
clientId: null,
color: null,
lastUpdate: null,
lastUpdate: getTimestamp(),
},
render: user => {
const cursor = document.createElement('span')
Expand Down Expand Up @@ -61,13 +65,12 @@ const CollaborationCursor = TiptapCollaborationCursor.extend({
})
},

addCommands() {
return {
...this.parent(),
updateSelf: () => ({ editor }) => {
const attributes = { ...this.options.user, lastUpdate: Date.now() }
return editor.commands.updateUser(attributes)
},
// Flag own cursor as active on undoable changes to the document state
onTransaction({ transaction }) {
const { updated, meta } = transaction
if (updated && (meta.addToHistory ?? true) && !meta.pointer) {
this.options.user.lastUpdate = getTimestamp()
this.options.provider.awareness.setLocalStateField('user', this.options.user)
}
},
})
Expand Down
3 changes: 0 additions & 3 deletions src/services/SyncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,6 @@ class SyncService {
if (this.#sendIntervalId) {
return
}
if (this.connection && !this.sending) {
return this._sendSteps(getSendable)
}
// If already sending, retry every 200ms.
return new Promise((resolve, reject) => {
this.#sendIntervalId = setInterval(() => {
Expand Down

0 comments on commit 7412c4e

Please sign in to comment.