Skip to content

Commit

Permalink
Optimize presence sync by only cloning diffs instead of full state. C…
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismccord committed Jun 29, 2021
1 parent 64d578c commit 4c7fa21
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
4 changes: 2 additions & 2 deletions assets/js/phoenix/presence.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ export default class Presence {
*
* @returns {Presence}
*/
static syncDiff(currentState, {joins, leaves}, onJoin, onLeave){
let state = this.clone(currentState)
static syncDiff(state, diff, onJoin, onLeave){
let {joins, leaves} = this.clone(diff)
if(!onJoin){ onJoin = function (){ } }
if(!onLeave){ onLeave = function (){ } }

Expand Down
27 changes: 13 additions & 14 deletions assets/test/presence_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import assert from "assert"

import {Presence} from "../js/phoenix"

let clone = (obj) => { return JSON.parse(JSON.stringify(obj)) }
let clone = (obj) => {
let cloned = JSON.parse(JSON.stringify(obj))
Object.entries(obj).map(([key, val]) => {
if(val === undefined){ cloned[key] = undefined }
})
return cloned
}

let fixtures = {
joins(){
Expand Down Expand Up @@ -60,9 +66,6 @@ describe("syncState", function(){
let onLeave = (key, current, leftPres) => {
left[key] = {current: current, leftPres: leftPres}
}
let stateBefore = clone(state)
Presence.syncState(state, newState, onJoin, onLeave)
assert.deepEqual(state, stateBefore)

state = Presence.syncState(state, newState, onJoin, onLeave)
assert.deepEqual(state, newState)
Expand All @@ -82,12 +85,12 @@ describe("syncState", function(){
let joined = {}
let left = {}
let onJoin = (key, current, newPres) => {
joined[key] = {current: current, newPres: newPres}
joined[key] = clone({current: current, newPres: newPres})
}
let onLeave = (key, current, leftPres) => {
left[key] = {current: current, leftPres: leftPres}
left[key] = clone({current: current, leftPres: leftPres})
}
state = Presence.syncState(state, newState, onJoin, onLeave)
state = Presence.syncState(state, clone(newState), onJoin, onLeave)
assert.deepEqual(state, newState)
assert.deepEqual(joined, {
u3: {current: {metas: [{id: 3, phx_ref: "3"}]},
Expand All @@ -100,11 +103,7 @@ describe("syncState", function(){
describe("syncDiff", function(){
it("syncs empty state", function(){
let joins = {u1: {metas: [{id: 1, phx_ref: "1"}]}}
let state = {}
Presence.syncDiff(state, {joins: joins, leaves: {}})
assert.deepEqual(state, {})

state = Presence.syncDiff(state, {
let state = Presence.syncDiff({}, {
joins: joins,
leaves: {}
})
Expand Down Expand Up @@ -182,10 +181,10 @@ describe("instance", function(){
let onLeaves = []

presence.onJoin((id, current, newPres) => {
onJoins.push({id, current, newPres})
onJoins.push(clone({id, current, newPres}))
})
presence.onLeave((id, current, leftPres) => {
onLeaves.push({id, current, leftPres})
onLeaves.push(clone({id, current, leftPres}))
})

// new connection
Expand Down
Loading

0 comments on commit 4c7fa21

Please sign in to comment.