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

Move Presence from Client to Document #574

Merged
merged 7 commits into from
Jul 18, 2023
Merged

Move Presence from Client to Document #574

merged 7 commits into from
Jul 18, 2023

Conversation

chacha912
Copy link
Contributor

@chacha912 chacha912 commented Jul 18, 2023

What this PR does / why we need it?

Move Presence from Client to Document

Why?

yorkie-team/yorkie#442

  • Presence cannot be separated if a client attaches two different documents.
  • We can use transactions by combining the document and the presence. Let's see an example of using presence in text selection and drawing.

initial presence

Instead of providing initial presence when creating the client,
the initial presence is now passed when creating the document.

  • As-is:
client = new yorkie.Client('http://localhost:8080', {
  presence: {
    color: getRandomColor(),
  },
});
  • To-be:
const doc = new yorkie.Document('docKey');
await client.attach(doc1, {
  initialPresence: {
    color: getRandomColor(),
  },
});

subscribe peersChanged event

The peers-changed event has been moved from the client to the document.

  • As-is:
client.subscribe((event) => {
  if (event.type === 'peers-changed') {
    // event.value = {
    //   type: 'initialized' | 'watched' | 'unwatched' | 'presence-changed';
    //   peers: Record<DocumentKey, Array<{ clientID: ActorID; presence: P }>>;
    // }
  }
});
  • To-be:
doc.subscribe('others', (event) => {
  // event = {
  //     type: 'watched' | 'unwatched' | 'presence-changed';
  //     peer: { clientID: ActorID; presence: P };
  // }
});

doc.getPeers()

Instead of retrieving peers from the client's perspective,
you can now get peers from the document.

  • As-is:
client.getPeersByDocKey(doc.getKey()) // Array<{ clientID: ActorID; presence: P }>
  • To-be:
doc.getPresences() // Array<{ clientID: ActorID; presence: P }>

What are the relevant tickets?

Related yorkie-team/yorkie#542, yorkie-team/yorkie#582

Checklist

  • Added relevant tests or not required
  • Didn't break anything

@codecov
Copy link

codecov bot commented Jul 18, 2023

Codecov Report

Merging #574 (8c2b65f) into main (cf9287a) will increase coverage by 0.26%.
The diff coverage is 97.66%.

@@            Coverage Diff             @@
##             main     #574      +/-   ##
==========================================
+ Coverage   87.93%   88.19%   +0.26%     
==========================================
  Files          79       81       +2     
  Lines        7807     7830      +23     
  Branches      780      799      +19     
==========================================
+ Hits         6865     6906      +41     
+ Misses        627      610      -17     
+ Partials      315      314       -1     
Impacted Files Coverage Δ
src/client/attachment.ts 72.72% <ø> (-5.06%) ⬇️
src/yorkie.ts 100.00% <ø> (ø)
test/integration/tree_test.ts 96.10% <ø> (ø)
src/document/presence/presence.ts 83.33% <83.33%> (ø)
src/api/converter.ts 91.49% <86.84%> (-0.62%) ⬇️
src/document/change/change.ts 88.00% <90.00%> (-0.24%) ⬇️
src/document/document.ts 84.78% <97.26%> (+5.34%) ⬆️
src/client/client.ts 77.16% <100.00%> (+3.62%) ⬆️
src/document/change/change_pack.ts 100.00% <100.00%> (ø)
src/document/change/context.ts 100.00% <100.00%> (ø)
... and 12 more

... and 1 file with indirect coverage changes

@hackerwins hackerwins self-requested a review July 18, 2023 10:32
Copy link
Member

@hackerwins hackerwins left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants