Skip to content

Commit

Permalink
Remove all peers from PeersChangedValue
Browse files Browse the repository at this point in the history
Users can get all peers using client.getPeersByDocKey(doc.getKey()),
so it is sufficient to only have peers(changedPeers info).
  • Loading branch information
chacha912 committed Mar 6, 2023
1 parent 5b8c80b commit 1838159
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 119 deletions.
23 changes: 14 additions & 9 deletions public/counter.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,27 @@
const counterIncreaseButton = document.getElementById('increaseButton');
const counterDecreaseButton = document.getElementById('decreaseButton');

function displayPeers(peers, myClientID) {
const usernames = [];
for (const { clientID, presence } of peers) {
usernames.push(
clientID === myClientID ? `<b>${clientID}</b>` : clientID,
);
}
peersHolder.innerHTML = JSON.stringify(usernames);
}

async function main() {
try {
// 01. create client with RPCAddr(envoy) then activate it.
const client = new yorkie.Client('http://localhost:8080');
client.subscribe(network.statusListener(statusHolder));
client.subscribe((event) => {
if (event.type === 'peers-changed') {
const usernames = [];
const myClientID = client.getID();
const peers = event.value.peers[doc.getKey()];
for (const { clientID, presence } of peers) {
usernames.push(
clientID === myClientID ? `<b>${clientID}</b>` : clientID,
);
}
peersHolder.innerHTML = JSON.stringify(usernames);
displayPeers(
client.getPeersByDocKey(doc.getKey()),
client.getID(),
);
}
});
await client.activate();
Expand Down
19 changes: 12 additions & 7 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@
textLogHolder.innerText = doc.getRoot().content.getStructureAsString();
}

function displayPeers(peers, clientID) {
const clientIDs = peers.map((peer) => peer.clientID);
peersHolder.innerHTML = JSON.stringify(clientIDs).replace(
clientID,
`<b>${clientID}</b>`,
);
function displayPeers(peers, myClientID) {
const usernames = [];
for (const { clientID, presence } of peers) {
usernames.push(
clientID === myClientID ? `<b>${clientID}</b>` : clientID,
);
}
peersHolder.innerHTML = JSON.stringify(usernames);
}

// https://github.com/codemirror/CodeMirror/pull/5619
Expand Down Expand Up @@ -138,7 +140,10 @@
client.subscribe(network.statusListener(statusHolder));
client.subscribe((event) => {
if (event.type === 'peers-changed') {
displayPeers(event.value.peers[doc.getKey()], client.getID());
displayPeers(
client.getPeersByDocKey(doc.getKey()),
client.getID(),
);
}
});
// 01-3. activate client
Expand Down
21 changes: 6 additions & 15 deletions public/quill.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,13 @@
client.subscribe((event) => {
if (event.type !== 'peers-changed') return;

const { type, changedPeers, peers } = event.value;
const quillChangedPeers = changedPeers[doc.getKey()];
quillPeers = peers[doc.getKey()];

switch (type) {
case 'unwatched': {
cursors.removeCursor(
quillChangedPeers.map((peer) => peer.presence.username),
);
break;
}
default: {
displayPeers(quillPeers, client.getID());
break;
}
const { type, peers } = event.value;
if (type === 'unwatched') {
cursors.removeCursor(
peers[doc.getKey()].map((peer) => peer.presence.username),
);
}
displayPeers(client.getPeersByDocKey(doc.getKey()), client.getID());
});

// 02. create a document then attach it into the client.
Expand Down
39 changes: 7 additions & 32 deletions src/core/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ export enum DocumentSyncResultType {
*/
export type PeersChangedValue<P> = {
type: 'initialization' | 'watched' | 'unwatched' | 'presence-changed';
changedPeers: Record<DocumentKey, Array<{ clientID: ActorID; presence: P }>>;
peers: Record<DocumentKey, Array<{ clientID: ActorID; presence: P }>>;
};

Expand Down Expand Up @@ -592,8 +591,7 @@ export class Client<P = Indexable> implements Observable<ClientEvent<P>> {
type: ClientEventType.PeersChanged,
value: {
type: 'presence-changed',
changedPeers,
peers: this.getPeers(),
peers: changedPeers,
},
});
}
Expand Down Expand Up @@ -669,25 +667,6 @@ export class Client<P = Indexable> implements Observable<ClientEvent<P>> {
return this.attachmentMap.get(docKey)!.peerPresenceMap!.get(clientID)!.data;
}

private getPeers(): Record<
DocumentKey,
Array<{ clientID: ActorID; presence: P }>
> {
const peersAll: Record<
DocumentKey,
Array<{ clientID: ActorID; presence: P }>
> = {};

for (const [docKey, attachment] of this.attachmentMap) {
const peers: Array<{ clientID: ActorID; presence: P }> = [];
for (const [clientID, presenceInfo] of attachment!.peerPresenceMap!) {
peers.push({ clientID, presence: presenceInfo.data });
}
peersAll[docKey] = peers;
}
return peersAll;
}

/**
* `getPeersByDocKey` returns the peers of the given document.
*/
Expand Down Expand Up @@ -830,19 +809,18 @@ export class Client<P = Indexable> implements Observable<ClientEvent<P>> {
}
});

const changedPeers: Record<
const peers: Record<
DocumentKey,
Array<{ clientID: ActorID; presence: P }>
> = {};
for (const key of keys) {
changedPeers[key] = this.getPeersByDocKey(key);
peers[key] = this.getPeersByDocKey(key);
}
this.eventStreamObserver.next({
type: ClientEventType.PeersChanged,
value: {
type: 'initialization',
changedPeers,
peers: this.getPeers(),
peers,
},
});
return;
Expand Down Expand Up @@ -876,15 +854,14 @@ export class Client<P = Indexable> implements Observable<ClientEvent<P>> {
type: ClientEventType.PeersChanged,
value: {
type: 'watched',
changedPeers: {
peers: {
[docKey]: [
{
clientID: publisher,
presence: this.getPeerPresence(docKey, publisher),
},
],
},
peers: this.getPeers(),
},
});
break;
Expand All @@ -895,15 +872,14 @@ export class Client<P = Indexable> implements Observable<ClientEvent<P>> {
type: ClientEventType.PeersChanged,
value: {
type: 'unwatched',
changedPeers: {
peers: {
[docKey]: [
{
clientID: publisher,
presence,
},
],
},
peers: this.getPeers(),
},
});
break;
Expand All @@ -920,15 +896,14 @@ export class Client<P = Indexable> implements Observable<ClientEvent<P>> {
type: ClientEventType.PeersChanged,
value: {
type: 'presence-changed',
changedPeers: {
peers: {
[docKey]: [
{
clientID: publisher,
presence: this.getPeerPresence(docKey, publisher),
},
],
},
peers: this.getPeers(),
},
});
break;
Expand Down
59 changes: 3 additions & 56 deletions test/integration/client_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,6 @@ describe('Client', function () {
type: ClientEventType.PeersChanged,
value: {
type: 'initialization',
changedPeers: {
[docKey1]: [{ clientID: c1ID, presence: { ...c1Presence } }],
},
peers: {
[docKey1]: [{ clientID: c1ID, presence: { ...c1Presence } }],
},
Expand All @@ -364,12 +361,6 @@ describe('Client', function () {
type: ClientEventType.PeersChanged,
value: {
type: 'initialization',
changedPeers: {
[docKey1]: [
{ clientID: c1ID, presence: { ...c1Presence } },
{ clientID: c2ID, presence: { ...c2Presence } },
],
},
peers: {
[docKey1]: [
{ clientID: c1ID, presence: { ...c1Presence } },
Expand All @@ -391,14 +382,8 @@ describe('Client', function () {
type: ClientEventType.PeersChanged,
value: {
type: 'watched',
changedPeers: {
[docKey1]: [{ clientID: c2ID, presence: { ...c2Presence } }],
},
peers: {
[docKey1]: [
{ clientID: c1ID, presence: { ...c1Presence } },
{ clientID: c2ID, presence: { ...c2Presence } },
],
[docKey1]: [{ clientID: c2ID, presence: { ...c2Presence } }],
},
},
});
Expand All @@ -417,15 +402,9 @@ describe('Client', function () {
type: ClientEventType.PeersChanged,
value: {
type: 'presence-changed',
changedPeers: {
[docKey1]: [
{ clientID: c1ID, presence: { ...c1Presence, name: 'z' } },
],
},
peers: {
[docKey1]: [
{ clientID: c1ID, presence: { ...c1Presence, name: 'z' } },
{ clientID: c2ID, presence: { ...c2Presence } },
],
},
},
Expand All @@ -443,15 +422,9 @@ describe('Client', function () {
type: ClientEventType.PeersChanged,
value: {
type: 'presence-changed',
changedPeers: {
[docKey1]: [
{ clientID: c1ID, presence: { ...c1Presence, name: 'z' } },
],
},
peers: {
[docKey1]: [
{ clientID: c1ID, presence: { ...c1Presence, name: 'z' } },
{ clientID: c2ID, presence: { ...c2Presence } },
],
},
},
Expand Down Expand Up @@ -479,15 +452,6 @@ describe('Client', function () {
type: ClientEventType.PeersChanged,
value: {
type: 'initialization',
changedPeers: {
[docKey1]: [
{ clientID: c1ID, presence: { ...c1Presence, name: 'z' } },
{ clientID: c2ID, presence: { ...c2Presence } },
],
[docKey2]: [
{ clientID: c1ID, presence: { ...c1Presence, name: 'z' } },
],
},
peers: {
[docKey1]: [
{ clientID: c1ID, presence: { ...c1Presence, name: 'z' } },
Expand All @@ -512,29 +476,20 @@ describe('Client', function () {
type: ClientEventType.PeersChanged,
value: {
type: 'unwatched',
changedPeers: {
peers: {
[docKey1]: [
{ clientID: c1ID, presence: { ...c1Presence, name: 'z' } },
],
},
peers: {
[docKey1]: [{ clientID: c2ID, presence: { ...c2Presence } }],
},
},
});
pushEvent(c2ExpectedEvents, {
type: ClientEventType.PeersChanged,
value: {
type: 'watched',
changedPeers: {
[docKey1]: [
{ clientID: c1ID, presence: { ...c1Presence, name: 'z' } },
],
},
peers: {
[docKey1]: [
{ clientID: c1ID, presence: { ...c1Presence, name: 'z' } },
{ clientID: c2ID, presence: { ...c2Presence } },
],
},
},
Expand Down Expand Up @@ -562,11 +517,6 @@ describe('Client', function () {
type: ClientEventType.PeersChanged,
value: {
type: 'initialization',
changedPeers: {
[docKey2]: [
{ clientID: c1ID, presence: { ...c1Presence, name: 'z' } },
],
},
peers: {
[docKey2]: [
{ clientID: c1ID, presence: { ...c1Presence, name: 'z' } },
Expand All @@ -587,14 +537,11 @@ describe('Client', function () {
type: ClientEventType.PeersChanged,
value: {
type: 'unwatched',
changedPeers: {
peers: {
[docKey1]: [
{ clientID: c1ID, presence: { ...c1Presence, name: 'z' } },
],
},
peers: {
[docKey1]: [{ clientID: c2ID, presence: { ...c2Presence } }],
},
},
});
await waitStubCallCount(stub2, 7);
Expand Down

0 comments on commit 1838159

Please sign in to comment.