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

Do not expect full state in the "leaves" array #10

Merged
merged 1 commit into from
Nov 28, 2024
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
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
Loading