add clock vector to awareness protocol
This commit is contained in:
@@ -11,6 +11,7 @@ const messageUsersStateChanged = 0
|
||||
/**
|
||||
* @typedef {Object} UserStateUpdate
|
||||
* @property {number} UserStateUpdate.userID
|
||||
* @property {number} UserStateUpdate.clock
|
||||
* @property {Object} UserStateUpdate.state
|
||||
*/
|
||||
|
||||
@@ -23,8 +24,9 @@ export const writeUsersStateChange = (encoder, stateUpdates) => {
|
||||
encoding.writeVarUint(encoder, messageUsersStateChanged)
|
||||
encoding.writeVarUint(encoder, len)
|
||||
for (let i = 0; i < len; i++) {
|
||||
const {userID, state} = stateUpdates[i]
|
||||
const {userID, state, clock} = stateUpdates[i]
|
||||
encoding.writeVarUint(encoder, userID)
|
||||
encoding.writeVarUint(encoder, clock)
|
||||
encoding.writeVarString(encoder, JSON.stringify(state))
|
||||
}
|
||||
}
|
||||
@@ -36,20 +38,25 @@ export const readUsersStateChange = (decoder, y) => {
|
||||
const len = decoding.readVarUint(decoder)
|
||||
for (let i = 0; i < len; i++) {
|
||||
const userID = decoding.readVarUint(decoder)
|
||||
const clock = decoding.readVarUint(decoder)
|
||||
const state = JSON.parse(decoding.readVarString(decoder))
|
||||
if (userID !== y.userID) {
|
||||
const uClock = y.awarenessClock.get(userID) || 0
|
||||
y.awarenessClock.set(userID, clock)
|
||||
if (state === null) {
|
||||
if (y.awareness.has(userID)) {
|
||||
// only write if clock increases. cannot overwrite
|
||||
if (y.awareness.has(userID) && uClock < clock) {
|
||||
y.awareness.delete(userID)
|
||||
removed.push(userID)
|
||||
}
|
||||
} else {
|
||||
} else if (uClock <= clock) { // allow to overwrite (e.g. when client was on, then offline)
|
||||
if (y.awareness.has(userID)) {
|
||||
updated.push(userID)
|
||||
} else {
|
||||
added.push(userID)
|
||||
}
|
||||
y.awareness.set(userID, state)
|
||||
y.awarenessClock.set(userID, clock)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -63,6 +70,7 @@ export const readUsersStateChange = (decoder, y) => {
|
||||
/**
|
||||
* @param {decoding.Decoder} decoder
|
||||
* @param {encoding.Encoder} encoder
|
||||
* @return {Array<UserStateUpdate>}
|
||||
*/
|
||||
export const forwardUsersStateChange = (decoder, encoder) => {
|
||||
const len = decoding.readVarUint(decoder)
|
||||
@@ -71,10 +79,12 @@ export const forwardUsersStateChange = (decoder, encoder) => {
|
||||
encoding.writeVarUint(encoder, len)
|
||||
for (let i = 0; i < len; i++) {
|
||||
const userID = decoding.readVarUint(decoder)
|
||||
const clock = decoding.readVarUint(decoder)
|
||||
const state = decoding.readVarString(decoder)
|
||||
encoding.writeVarUint(encoder, userID)
|
||||
encoding.writeVarUint(encoder, clock)
|
||||
encoding.writeVarString(encoder, state)
|
||||
updates.push({userID, state: JSON.parse(state)})
|
||||
updates.push({userID, state: JSON.parse(state), clock})
|
||||
}
|
||||
return updates
|
||||
}
|
||||
@@ -95,6 +105,7 @@ export const readAwarenessMessage = (decoder, y) => {
|
||||
* @typedef {Object} UserState
|
||||
* @property {number} UserState.userID
|
||||
* @property {any} UserState.state
|
||||
* @property {number} UserState.clock
|
||||
*/
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user