Skip to content

Commit

Permalink
update awareness protocol to broadcast local states properly
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Aug 1, 2019
1 parent f296bc5 commit e2059cd
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 23 deletions.
31 changes: 20 additions & 11 deletions awareness.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class Awareness extends Observable {
this.meta = new Map()
this._checkInterval = setInterval(() => {
const now = time.getUnixTime()
if (this.getLocalState() !== null && outdatedTimeout / 2 <= now - /** @type {{lastUpdated:number}} */ (this.meta.get(doc.clientID)).lastUpdated) {
if (this.getLocalState() !== null && (outdatedTimeout / 2 <= now - /** @type {{lastUpdated:number}} */ (this.meta.get(doc.clientID)).lastUpdated)) {
// renew local clock
this.setLocalState(this.getLocalState())
}
Expand All @@ -62,7 +62,7 @@ export class Awareness extends Observable {
*/
const remove = []
this.meta.forEach((meta, clientid) => {
if (outdatedTimeout <= now - meta.lastUpdated) {
if (outdatedTimeout <= now - meta.lastUpdated && this.states.has(clientid)) {
remove.push(clientid)
}
})
Expand All @@ -73,6 +73,7 @@ export class Awareness extends Observable {
doc.on('destroy', () => {
this.destroy()
})
this.setLocalState({})
}
destroy () {
clearInterval(this._checkInterval)
Expand Down Expand Up @@ -146,9 +147,10 @@ export const removeAwarenessStates = (awareness, clients, origin) => {
awareness.states.delete(clientID)
if (clientID === awareness.doc.clientID) {
const curMeta = /** @type {MetaClientState} */ (awareness.meta.get(clientID))
curMeta.clock++
curMeta.lastUpdated = time.getUnixTime()
awareness.meta.set(clientID, curMeta)
awareness.meta.set(clientID, {
clock: curMeta.clock + 1,
lastUpdated: time.getUnixTime()
})
}
removed.push(clientID)
}
Expand All @@ -163,13 +165,13 @@ export const removeAwarenessStates = (awareness, clients, origin) => {
* @param {Array<number>} clients
* @return {Uint8Array}
*/
export const encodeAwarenessUpdate = (awareness, clients) => {
export const encodeAwarenessUpdate = (awareness, clients, states = awareness.states) => {
const len = clients.length
const encoder = encoding.createEncoder()
encoding.writeVarUint(encoder, len)
for (let i = 0; i < len; i++) {
const clientID = clients[i]
const state = awareness.states.get(clientID) || null
const state = states.get(clientID) || null
const clock = /** @type {MetaClientState} */ (awareness.meta.get(clientID)).clock
encoding.writeVarUint(encoder, clientID)
encoding.writeVarUint(encoder, clock)
Expand All @@ -192,13 +194,20 @@ export const applyAwarenessUpdate = (awareness, update, origin) => {
const len = decoding.readVarUint(decoder)
for (let i = 0; i < len; i++) {
const clientID = decoding.readVarUint(decoder)
const clock = decoding.readVarUint(decoder)
let clock = decoding.readVarUint(decoder)
const state = JSON.parse(decoding.readVarString(decoder))
const clientMeta = awareness.meta.get(clientID)
const uClock = clientMeta === undefined ? 0 : clientMeta.clock
if (uClock < clock || (uClock === clock && state === null && awareness.states.has(clientID))) {
const currClock = clientMeta === undefined ? 0 : clientMeta.clock
if (currClock < clock || (currClock === clock && state === null && awareness.states.has(clientID))) {
if (state === null) {
awareness.states.delete(clientID)
// never let a remote client remove this local state
if (clientID === awareness.doc.clientID && awareness.getLocalState() != null) {
// remote client removed the local state. Do not remote state. Broadcast a message indicating
// that this client still exists by increasing the clock
clock++
} else {
awareness.states.delete(clientID)
}
} else {
awareness.states.set(clientID, state)
}
Expand Down
20 changes: 10 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"lib0": "0.0.5"
},
"devDependencies": {
"yjs": "13.0.0-83",
"yjs": "13.0.0-94",
"rollup": "^1.1.2",
"rollup-cli": "^1.0.9",
"standard": "^12.0.1"
Expand Down
2 changes: 1 addition & 1 deletion sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const writeSyncStep1 = (encoder, doc) => {
/**
* @param {encoding.Encoder} encoder
* @param {Y.Doc} doc
* @param {Uint8Array} encodedStateVector
* @param {Uint8Array|undefined} encodedStateVector
*/
export const writeSyncStep2 = (encoder, doc, encodedStateVector) => {
encoding.writeVarUint(encoder, messageYjsSyncStep2)
Expand Down

0 comments on commit e2059cd

Please sign in to comment.