Skip to content

Commit

Permalink
Do not expect full state in the "leaves" array
Browse files Browse the repository at this point in the history
If there's an update to the state then we always have a "join". If it's
just leave, then the state doesn't have to change, and we can use
previously known state.
  • Loading branch information
indrekj committed Nov 27, 2024
1 parent d33abf4 commit cb0f84e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
4 changes: 2 additions & 2 deletions assets/js/phoenix/presence.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export default class Presence {
if (changes[key]) {
changes[key].leftMetas = leftPresence.metas;
} else {
changes[key] = {joinedMetas: [], leftMetas: leftPresence.metas, update: leftPresence}
changes[key] = {joinedMetas: [], leftMetas: leftPresence.metas, update: {}}
}
})

Expand All @@ -207,7 +207,7 @@ export default class Presence {
const refsToRemove = leftMetas.map(m => m.phx_ref)
const oldPresence = state[key];

const newPresence = {metas: oldPresence ? oldPresence.metas : []};
const newPresence = oldPresence ? { ...oldPresence } : {metas: []}
newPresence.metas = newPresence.metas
.filter(m => joinedRefs.indexOf(m.phx_ref) === -1)
.concat(joinedMetas)
Expand Down
37 changes: 31 additions & 6 deletions assets/test/presence_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ let fixtures = {
return {u1: {metas: [{id: 1, phx_ref: "1.2"}]}}
},
leaves(){
return {u2: {metas: [{id: 2, phx_ref: "2"}]}}
return {u2: {metas: [{phx_ref: "2"}]}}
},
state(){
return {
Expand Down Expand Up @@ -183,7 +183,7 @@ describe("synchronizeDiff", () => {
let state = {
u1: {metas: [{id: 1, phx_ref: "1"}, {id: 1, phx_ref: "1.2"}]}
}
Presence.synchronizeDiff(state, {joins: {}, leaves: {u1: {metas: [{id: 1, phx_ref: "1"}]}}})
Presence.synchronizeDiff(state, {joins: {}, leaves: {u1: {metas: [{phx_ref: "1"}]}}})

assert.deepEqual(state, {
u1: {metas: [{id: 1, phx_ref: "1.2"}]},
Expand All @@ -198,7 +198,7 @@ describe("synchronizeDiff", () => {
}
let update2 = {
joins: {u1: {metas: [{id: 1, phx_ref_prev: 1, phx_ref: 1.1}]}},
leaves: {u1: {metas: [{id: 1, phx_ref: 1}]}}
leaves: {u1: {metas: [{phx_ref: 1}]}}
}

let stateAfterUpdate1 = {metas: [{id: 1, phx_ref: 1}, {id: 2, phx_ref: 2}]}
Expand All @@ -223,7 +223,7 @@ describe("synchronizeDiff", () => {
}
let update2 = {
joins: {},
leaves: {u1: {metas: [{id: 1, phx_ref: 1}]}}
leaves: {u1: {metas: [{phx_ref: 1}]}}
}

let stateAfterUpdate1 = {metas: [{id: 1, phx_ref: 1}, {id: 2, phx_ref: 2}]}
Expand All @@ -248,7 +248,7 @@ describe("synchronizeDiff", () => {
}
let update2 = {
joins: {u1: {foo: 'baz', metas: [{id: 1, phx_ref_prev: 1, phx_ref: 1.1}]}},
leaves: {u1: {foo: 'bar', metas: [{id: 1, phx_ref: 1}]}}
leaves: {u1: {metas: [{phx_ref: 1}]}}
}

let stateAfterUpdate1 = {foo: 'bar', metas: [{id: 1, phx_ref: 1}]}
Expand All @@ -264,6 +264,31 @@ describe("synchronizeDiff", () => {
Presence.synchronizeDiff(state, update1)
Presence.synchronizeDiff(state, update2, onChange)
});

it("uses the latest known state on leave when there are no joins", done => {
let state = {}
let update1 = {
joins: {u1: {foo: 'bar', metas: [{id: 1, phx_ref: 1}, {id: 2, phx_ref: 2}]}},
leaves: {}
}
let update2 = {
joins: {},
leaves: {u1: {metas: [{phx_ref: 1}]}}
}

let stateAfterUpdate1 = {foo: 'bar', metas: [{id: 1, phx_ref: 1}, {id: 2, phx_ref: 2}]}
let expectedFinalState = {foo: 'bar', metas: [{id: 2, phx_ref: 2}]}

let onChange = (key, oldPresence, newPresence) => {
assert.deepEqual(key, "u1");
assert.deepEqual(oldPresence, stateAfterUpdate1)
assert.deepEqual(newPresence, expectedFinalState)
done();
};

Presence.synchronizeDiff(state, update1)
Presence.synchronizeDiff(state, update2, onChange)
});
})

describe("syncDiff", () => {
Expand All @@ -290,7 +315,7 @@ describe("syncDiff", () => {
let state = {
u1: {metas: [{id: 1, phx_ref: "1"}, {id: 1, phx_ref: "1.2"}]}
}
state = Presence.syncDiff(state, {joins: {}, leaves: {u1: {metas: [{id: 1, phx_ref: "1"}]}}})
state = Presence.syncDiff(state, {joins: {}, leaves: {u1: {metas: [{phx_ref: "1"}]}}})

assert.deepEqual(state, {
u1: {metas: [{id: 1, phx_ref: "1.2"}]},
Expand Down

0 comments on commit cb0f84e

Please sign in to comment.