Skip to content

Commit

Permalink
Replace HistoryChangePack with DocEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
chacha912 committed Apr 17, 2024
1 parent 73a480c commit d91ec0e
Show file tree
Hide file tree
Showing 8 changed files with 438 additions and 470 deletions.
59 changes: 17 additions & 42 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@ import {
Document,
DocumentKey,
DocumentStatus,
WatchStreamType,
Indexable,
} from '@yorkie-js-sdk/src/document/document';
import { createAuthInterceptor } from '@yorkie-js-sdk/src/client/auth_interceptor';
import { createMetricInterceptor } from '@yorkie-js-sdk/src/client/metric_interceptor';
import { Indexable, DocEventType } from '@yorkie-js-sdk/src/document/document';

/**
* `SyncMode` defines synchronization modes for the PushPullChanges API.
Expand Down Expand Up @@ -460,10 +459,7 @@ export class Client implements Observable<ClientEvent> {
return doc;
}

doc.applyStatus({
type: DocumentStatus.Attached,
value: { actorID: this.id! },
});
doc.applyStatus(DocumentStatus.Attached);
this.attachmentMap.set(
doc.getKey(),
new Attachment(
Expand Down Expand Up @@ -529,9 +525,7 @@ export class Client implements Observable<ClientEvent> {
const pack = converter.fromChangePack<P>(res.changePack!);
doc.applyChangePack(pack);
if (doc.getStatus() !== DocumentStatus.Removed) {
doc.applyStatus({
type: DocumentStatus.Detached,
});
doc.applyStatus(DocumentStatus.Detached);
}
this.detachInternal(doc.getKey());

Expand Down Expand Up @@ -814,42 +808,23 @@ export class Client implements Observable<ClientEvent> {
attachment: Attachment<T, P>,
resp: WatchDocumentResponse,
) {
if (resp.body.case === 'initialization') {
attachment.doc.applyWatchStream({
type: WatchStreamType.Initialization,
value: { clientIDs: resp.body.value.clientIds },
if (
resp.body.case === 'event' &&
resp.body.value.type === PbDocEventType.DOCUMENT_CHANGED
) {
attachment.remoteChangeEventReceived = true;

// TODO(chacha): We need to remove the following event propagation
// logic after removing `client.subscribe`.
this.eventStreamObserver.next({
type: ClientEventType.DocumentChanged,
value: [attachment.doc.getKey()],
});

return;
} else if (resp.body.case === 'event') {
const pbWatchEvent = resp.body.value;
const eventType = pbWatchEvent.type;
const publisher = pbWatchEvent.publisher;
switch (eventType) {
case PbDocEventType.DOCUMENT_CHANGED:
attachment.remoteChangeEventReceived = true;

// TODO(chacha): We need to remove the following event propagation
// logic after removing `client.subscribe`.
this.eventStreamObserver.next({
type: ClientEventType.DocumentChanged,
value: [attachment.doc.getKey()],
});
break;
case PbDocEventType.DOCUMENT_WATCHED:
attachment.doc.applyWatchStream({
type: WatchStreamType.DocEvent,
value: { type: DocEventType.Watched, publisher },
});
break;
case PbDocEventType.DOCUMENT_UNWATCHED: {
attachment.doc.applyWatchStream({
type: WatchStreamType.DocEvent,
value: { type: DocEventType.Unwatched, publisher },
});
break;
}
}
}

attachment.doc.applyWatchStream(resp);
}

private detachInternal(docKey: DocumentKey) {
Expand Down
6 changes: 3 additions & 3 deletions src/devtools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ function startSync<T, P extends Indexable>(doc: Document<T, P>): void {
sendToPanel({
msg: 'doc::sync::full',
docKey: doc.getKey(),
changes: doc.getHistoryChanges(),
events: doc.getDocEvents(),
});

const unsub = doc.subscribeForTest((event) => {
const unsub = doc.subscribe('all', (event) => {
sendToPanel({
msg: 'doc::sync::partial',
docKey: doc.getKey(),
changes: event.value,
event,
});
});

Expand Down
8 changes: 4 additions & 4 deletions src/devtools/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import type { HistoryChangePack } from '@yorkie-js-sdk/src/document/document';
import type { DocEvent } from '@yorkie-js-sdk/src/document/document';

/**
* `EventSourceDevPanel` is the name of the source representing messages
Expand Down Expand Up @@ -76,15 +76,15 @@ export type SDKToPanelMessage =
| {
msg: 'doc::sync::full';
docKey: string;
changes: Array<HistoryChangePack>;
events: Array<Array<DocEvent>>;
}
/**
* Sent whenever the document is updated.
* Sent whenever the document is changed.
*/
| {
msg: 'doc::sync::partial';
docKey: string;
changes: Array<HistoryChangePack>;
event: Array<DocEvent>;
};

export type FullPanelToSDKMessage = PanelToSDKMessage & {
Expand Down
33 changes: 30 additions & 3 deletions src/document/change/change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from '@yorkie-js-sdk/src/document/operation/operation';
import { CRDTRoot } from '@yorkie-js-sdk/src/document/crdt/root';
import { ChangeID } from '@yorkie-js-sdk/src/document/change/change_id';
import { ChangePayload, Indexable } from '@yorkie-js-sdk/src/document/document';
import { Indexable } from '@yorkie-js-sdk/src/document/document';
import { converter } from '@yorkie-js-sdk/src/api/converter';
import { HistoryOperation } from '@yorkie-js-sdk/src/document/history';
import {
Expand All @@ -31,6 +31,16 @@ import {
} from '@yorkie-js-sdk/src/document/presence/presence';
import { deepcopy } from '@yorkie-js-sdk/src/util/object';

export type ChangeStruct = {
changeID: string;
message?: string;
operations?: Array<string>;
presenceChange?: {
type: PresenceChangeType;
presence?: object; // TODO(chacha912): Specify the type accurately.
};
};

/**
* `Change` represents a unit of modification in the document.
*/
Expand Down Expand Up @@ -191,9 +201,9 @@ export class Change<P extends Indexable> {
}

/**
* `toChangePayload` returns the ChangePayload of this change.
* `toStruct` returns the structure of this change.
*/
public toChangePayload(): ChangePayload {
public toStruct(): ChangeStruct {
return {
changeID: converter.bytesToHex(
converter.toChangeID(this.getID()).toBinary(),
Expand All @@ -205,4 +215,21 @@ export class Change<P extends Indexable> {
presenceChange: this.getPresenceChange(),
};
}

/**
* `fromStruct` creates a instance of Change from the struct.
*/
public static fromStruct<P extends Indexable>(
struct: ChangeStruct,
): Change<P> {
const { changeID, operations, presenceChange, message } = struct;
return Change.create<P>({
id: converter.bytesToChangeID(converter.hexToBytes(changeID)),
operations: operations?.map((op) => {
return converter.bytesToOperation(converter.hexToBytes(op));
}),
presenceChange: presenceChange as any,
message,
});
}
}
Loading

0 comments on commit d91ec0e

Please sign in to comment.