Skip to content

Commit

Permalink
implement modifyAwarenessState
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Jul 6, 2020
1 parent 98e894f commit 77a378d
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions awareness.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class Awareness extends Observable {
clearInterval(this._checkInterval)
}
/**
* @return {Object<string,Object>|null}
* @return {Object<string,any>|null}
*/
getLocalState () {
return this.states.get(this.doc.clientID) || null
Expand Down Expand Up @@ -125,7 +125,7 @@ export class Awareness extends Observable {
}
/**
* @param {string} field
* @param {Object} value
* @param {any} value
*/
setLocalStateField (field, value) {
const state = this.getLocalState()
Expand Down Expand Up @@ -192,6 +192,33 @@ export const encodeAwarenessUpdate = (awareness, clients, states = awareness.sta
return encoding.toUint8Array(encoder)
}

/**
* Modify the content of an awareness update before re-encoding it to an awareness update.
*
* This might be useful when you have a central server that wants to ensure that clients
* cant hijack somebody elses identity.
*
* @param {Uint8Array} update
* @param {function(any):any} modify
* @return {Uint8Array}
*/
export const modifyAwarenessUpdate = (update, modify) => {
const decoder = decoding.createDecoder(update)
const encoder = encoding.createEncoder()
const len = decoding.readVarUint(decoder)
encoding.writeVarUint(encoder, len)
for (let i = 0; i < len; i++) {
const clientID = decoding.readVarUint(decoder)
const clock = decoding.readVarUint(decoder)
const state = JSON.parse(decoding.readVarString(decoder))
const modifiedState = modify(state)
encoding.writeVarUint(encoder, clientID)
encoding.writeVarUint(encoder, clock)
encoding.writeVarString(encoder, JSON.stringify(modifiedState))
}
return encoding.toUint8Array(encoder)
}

/**
* @param {Awareness} awareness
* @param {Uint8Array} update
Expand Down

0 comments on commit 77a378d

Please sign in to comment.