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 before sending them in a push request so they can be combined.

Also make use of prosemirrors transactions to detect own changes
instead of listening to yjs updates.

Signed-off-by: Max <max@nextcloud.com>
  • Loading branch information
max-nextcloud committed Jul 28, 2023
1 parent b7baceb commit 928fe35
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 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
22 changes: 14 additions & 8 deletions src/extensions/CollaborationCursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ function showCursorLabel(clientId) {
}, 50)
}

/**
* Unix timestamp in seconds.
*/
function getTimestamp() {
return Math.floor(Date.now() / 1000)
}

const CollaborationCursor = TiptapCollaborationCursor.extend({
addOptions() {
return {
Expand All @@ -28,7 +35,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 +68,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
6 changes: 1 addition & 5 deletions src/services/SyncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,10 @@ class SyncService {
}

sendSteps(getSendable) {
// If already retrying, do nothing.
// If already waiting to send, do nothing.
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(() => {
if (this.connection && !this.sending) {
Expand Down

0 comments on commit 928fe35

Please sign in to comment.