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

Avoid unnecessary syncs in push-only syncmode #818

Merged
merged 1 commit into from
May 23, 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: 4 additions & 0 deletions src/client/attachment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export class Attachment<T, P extends Indexable> {
return false;
}

if (this.syncMode === SyncMode.RealtimePushOnly) {
return this.doc.hasLocalChanges();
}

return (
this.syncMode !== SyncMode.Manual &&
(this.doc.hasLocalChanges() || this.remoteChangeEventReceived)
hackerwins marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
57 changes: 57 additions & 0 deletions test/integration/client_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import yorkie, {
DocEventType,
DocumentSyncStatus,
Tree,
Text,
} from '@yorkie-js-sdk/src/yorkie';
import { EventCollector } from '@yorkie-js-sdk/test/helper/helper';
import {
Expand Down Expand Up @@ -608,4 +609,60 @@ describe.sequential('Client', function () {
await c1.deactivate();
await c2.deactivate();
});

it('Should avoid unnecessary syncs in push-only mode', async function ({
task,
}) {
const c1 = new yorkie.Client(testRPCAddr);
const c2 = new yorkie.Client(testRPCAddr);
await c1.activate();
await c2.activate();

const docKey = toDocKey(`${task.name}-${new Date().getTime()}`);
const d1 = new yorkie.Document<{ t: Text }>(docKey);
const d2 = new yorkie.Document<{ t: Text }>(docKey);
await c1.attach(d1);
await c2.attach(d2);

const eventCollectorD1 = new EventCollector();
const eventCollectorD2 = new EventCollector();
const unsub1 = d1.subscribe('sync', (event) => {
eventCollectorD1.add(event.value);
});
const unsub2 = d2.subscribe('sync', (event) => {
eventCollectorD2.add(event.value);
});

d1.update((root) => {
root.t = new Text();
root.t.edit(0, 0, 'a');
});
await eventCollectorD2.waitAndVerifyNthEvent(1, DocumentSyncStatus.Synced);

assert.equal(d1.getRoot().t.toString(), 'a');
assert.equal(d2.getRoot().t.toString(), 'a');

eventCollectorD1.reset();
c1.changeSyncMode(d1, SyncMode.RealtimePushOnly);
d2.update((root) => {
root.t.edit(1, 1, 'b');
});
await eventCollectorD2.waitAndVerifyNthEvent(2, DocumentSyncStatus.Synced);
d2.update((root) => {
root.t.edit(2, 2, 'c');
});
await eventCollectorD2.waitAndVerifyNthEvent(3, DocumentSyncStatus.Synced);

assert.equal(eventCollectorD1.getLength(), 0);
c1.changeSyncMode(d1, SyncMode.Realtime);
await eventCollectorD1.waitAndVerifyNthEvent(1, DocumentSyncStatus.Synced);

assert.equal(d1.getRoot().t.toString(), 'abc');
assert.equal(d2.getRoot().t.toString(), 'abc');

unsub1();
unsub2();
await c1.deactivate();
await c2.deactivate();
});
});
hackerwins marked this conversation as resolved.
Show resolved Hide resolved
Loading