Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow a consumer to switch between JSON and binary encoding for awareness #27

Merged
merged 2 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 58 additions & 7 deletions awareness.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,54 @@ export class Awareness extends Observable {
}
}

/**
* A state encoder that serializes state into JSON.
*
* This is the default strategy used in this package.
*/
export class DefaultAwarenessStateEncoder {
/**
* Encode an awareness state entry
* @param {encoding.Encoder} encoder
* @param {any} update
*/
encodeState (encoder, update) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be static methods. Then there is no need to create an instance of *AwarenessStateEncoder

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, can do.

encoding.writeVarString(encoder, JSON.stringify(update))
}

/**
* Decode an awareness state entry
* @param {decoding.Decoder} decoder
* @returns {string}
*/
decodeState (decoder) {
return JSON.parse(decoding.readVarString(decoder))
}
}

/**
* A state encoder that serializes state into a binary format.
*/
export class BinaryAwarenessStateEncoder {
/**
* Encode an awareness state entry
* @param {encoding.Encoder} encoder
* @param {any} update
*/
encodeState (encoder, update) {
encoding.writeAny(encoder, update)
}

/**
* Decode an awareness state entry
* @param {decoding.Decoder} decoder
* @returns {any}
*/
decodeState (decoder) {
return decoding.readAny(decoder)
}
}

/**
* Mark (remote) clients as inactive and remove them from the list of active peers.
* This change will be propagated to remote clients.
Expand Down Expand Up @@ -189,9 +237,10 @@ export const removeAwarenessStates = (awareness, clients, origin) => {
/**
* @param {Awareness} awareness
* @param {Array<number>} clients
* @param {DefaultAwarenessStateEncoder|BinaryAwarenessStateEncoder} stateEncoder The encoder to use for encoding and decoding each state entry
* @return {Uint8Array}
*/
export const encodeAwarenessUpdate = (awareness, clients, states = awareness.states) => {
export const encodeAwarenessUpdate = (awareness, clients, states = awareness.states, stateEncoder = new DefaultAwarenessStateEncoder()) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should not be necessary to create a new instance of anything here. *AwarenessEncoder should only have static methods.

const len = clients.length
const encoder = encoding.createEncoder()
encoding.writeVarUint(encoder, len)
Expand All @@ -201,7 +250,7 @@ export const encodeAwarenessUpdate = (awareness, clients, states = awareness.sta
const clock = /** @type {MetaClientState} */ (awareness.meta.get(clientID)).clock
encoding.writeVarUint(encoder, clientID)
encoding.writeVarUint(encoder, clock)
encoding.writeVarString(encoder, JSON.stringify(state))
stateEncoder.encodeState(encoder, state)
}
return encoding.toUint8Array(encoder)
}
Expand All @@ -214,21 +263,22 @@ export const encodeAwarenessUpdate = (awareness, clients, states = awareness.sta
*
* @param {Uint8Array} update
* @param {function(any):any} modify
* @param {DefaultAwarenessStateEncoder|BinaryAwarenessStateEncoder} stateEncoder The encoder to use for encoding and decoding each state entry
* @return {Uint8Array}
*/
export const modifyAwarenessUpdate = (update, modify) => {
export const modifyAwarenessUpdate = (update, modify, stateEncoder = new DefaultAwarenessStateEncoder()) => {
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 state = stateEncoder.decodeState(decoder)
const modifiedState = modify(state)
encoding.writeVarUint(encoder, clientID)
encoding.writeVarUint(encoder, clock)
encoding.writeVarString(encoder, JSON.stringify(modifiedState))
stateEncoder.encodeState(encoder, modifiedState)
}
return encoding.toUint8Array(encoder)
}
Expand All @@ -237,8 +287,9 @@ export const modifyAwarenessUpdate = (update, modify) => {
* @param {Awareness} awareness
* @param {Uint8Array} update
* @param {any} origin This will be added to the emitted change event
* @param {DefaultAwarenessStateEncoder|BinaryAwarenessStateEncoder} stateEncoder The encoder to use for encoding each state entry
*/
export const applyAwarenessUpdate = (awareness, update, origin) => {
export const applyAwarenessUpdate = (awareness, update, origin, stateEncoder = new DefaultAwarenessStateEncoder()) => {
const decoder = decoding.createDecoder(update)
const timestamp = time.getUnixTime()
const added = []
Expand All @@ -249,7 +300,7 @@ export const applyAwarenessUpdate = (awareness, update, origin) => {
for (let i = 0; i < len; i++) {
const clientID = decoding.readVarUint(decoder)
let clock = decoding.readVarUint(decoder)
const state = JSON.parse(decoding.readVarString(decoder))
const state = stateEncoder.decodeState(decoder)
const clientMeta = awareness.meta.get(clientID)
const prevState = awareness.states.get(clientID)
const currClock = clientMeta === undefined ? 0 : clientMeta.clock
Expand Down
13 changes: 9 additions & 4 deletions awareness.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ import * as t from 'lib0/testing'
import * as awareness from './awareness'

/**
* @param {t.TestCase} tc
* @param {awareness.DefaultAwarenessStateEncoder|awareness.BinaryAwarenessStateEncoder} stateEncoder
* @return {function(t.TestCase): void}
*/
export const testAwareness = tc => {
const testAwarenessWithEncoding = (stateEncoder = new awareness.DefaultAwarenessStateEncoder()) => tc => {
const doc1 = new Y.Doc()
doc1.clientID = 0
const doc2 = new Y.Doc()
doc2.clientID = 1
const aw1 = new awareness.Awareness(doc1)
const aw2 = new awareness.Awareness(doc2)
aw1.on('update', /** @param {any} p */ ({ added, updated, removed }) => {
const enc = awareness.encodeAwarenessUpdate(aw1, added.concat(updated).concat(removed))
awareness.applyAwarenessUpdate(aw2, enc, 'custom')
const enc = awareness.encodeAwarenessUpdate(aw1, added.concat(updated).concat(removed), aw1.states, stateEncoder)
awareness.applyAwarenessUpdate(aw2, enc, 'custom', stateEncoder)
})
let lastChangeLocal = /** @type {any} */ (null)
aw1.on('change', /** @param {any} change */ change => {
Expand Down Expand Up @@ -51,3 +52,7 @@ export const testAwareness = tc => {
t.compare(aw1.getStates().get(0), undefined)
t.compare(lastChangeLocal, lastChange)
}

export const testAwareness = testAwarenessWithEncoding()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is the same as the third test and can be removed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll augment this test a bit, I'd like to add a test to ensure the default behaviour is using the default encoder.

export const testAwarenessWithBinary = testAwarenessWithEncoding(new awareness.BinaryAwarenessStateEncoder())
export const testAwarenessWithDefault = testAwarenessWithEncoding(new awareness.DefaultAwarenessStateEncoder())