Skip to content

Commit

Permalink
Define type Array<DocEvent> as TransactionDocEvents
Browse files Browse the repository at this point in the history
  • Loading branch information
chacha912 committed Apr 18, 2024
1 parent 9d8fc62 commit b0d310c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
6 changes: 3 additions & 3 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 { DocEvent } from '@yorkie-js-sdk/src/document/document';
import type { TransactionDocEvents } 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;
events: Array<Array<DocEvent>>;
events: Array<TransactionDocEvents>;
}
/**
* Sent whenever the document is changed.
*/
| {
msg: 'doc::sync::partial';
docKey: string;
event: Array<DocEvent>;
event: TransactionDocEvents;
};

export type FullPanelToSDKMessage = PanelToSDKMessage & {
Expand Down
43 changes: 30 additions & 13 deletions src/document/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ export type DocEvent<P extends Indexable = Indexable, T = OperationInfo> =
| UnwatchedEvent<P>
| PresenceChangedEvent<P>;

/**
* `TransactionDocEvents` represents document events that occur within
* a single transaction (e.g., doc.update).
*/
export type TransactionDocEvents<P extends Indexable = Indexable> = Array<
DocEvent<P>
>;

/**
* @internal
*/
Expand Down Expand Up @@ -461,8 +469,8 @@ export class Document<T, P extends Indexable = Indexable> {
presences: Map<ActorID, P>;
};

private eventStream: Observable<Array<DocEvent<P>>>;
private eventStreamObserver!: Observer<Array<DocEvent<P>>>;
private eventStream: Observable<TransactionDocEvents<P>>;
private eventStreamObserver!: Observer<TransactionDocEvents<P>>;

/**
* `onlineClients` is a set of client IDs that are currently online.
Expand Down Expand Up @@ -495,7 +503,7 @@ export class Document<T, P extends Indexable = Indexable> {
* (time-traveling feature) in Devtools. Later, external storage such as
* IndexedDB will be used.
*/
public docEvents: Array<Array<DocEvent<P>>>;
public docEvents: Array<TransactionDocEvents<P>>;

constructor(key: string, opts?: DocumentOptions) {
this.opts = opts || {};
Expand All @@ -510,7 +518,7 @@ export class Document<T, P extends Indexable = Indexable> {
this.checkpoint = InitialCheckpoint;
this.localChanges = [];

this.eventStream = createObservable<Array<DocEvent<P>>>((observer) => {
this.eventStream = createObservable<TransactionDocEvents<P>>((observer) => {
this.eventStreamObserver = observer;
});

Expand Down Expand Up @@ -609,7 +617,7 @@ export class Document<T, P extends Indexable = Indexable> {

// 03. Publish the document change event.
// NOTE(chacha912): Check opInfos, which represent the actually executed operations.
const events: Array<DocEvent<P>> = [];
const events: TransactionDocEvents<P> = [];
const rawChange = change.toStruct();
if (opInfos.length > 0) {
events.push({
Expand Down Expand Up @@ -701,7 +709,7 @@ export class Document<T, P extends Indexable = Indexable> {
*/
public subscribe(
type: 'all',
next: NextFn<Array<DocEvent<P>>>,
next: NextFn<TransactionDocEvents<P>>,
error?: ErrorFn,
complete?: CompleteFn,
): Unsubscribe;
Expand All @@ -716,7 +724,7 @@ export class Document<T, P extends Indexable = Indexable> {
arg2?:
| NextFn<DocEvent<P, TOperationInfo>>
| NextFn<DocEvent<P>>
| NextFn<Array<DocEvent<P>>>
| NextFn<TransactionDocEvents<P>>
| ErrorFn,
arg3?: ErrorFn | CompleteFn,
arg4?: CompleteFn,
Expand Down Expand Up @@ -797,7 +805,7 @@ export class Document<T, P extends Indexable = Indexable> {
);
}
if (arg1 === 'all') {
const callback = arg2 as NextFn<Array<DocEvent<P>>>;
const callback = arg2 as NextFn<TransactionDocEvents<P>>;
return this.eventStream.subscribe(callback, arg3, arg4);
}
const target = arg1;
Expand Down Expand Up @@ -864,7 +872,7 @@ export class Document<T, P extends Indexable = Indexable> {
* `publish` triggers an event in this document, which can be received by
* callback functions from document.subscribe().
*/
public publish(events: Array<DocEvent<P>>) {
public publish(events: TransactionDocEvents<P>) {
if (this.opts.enableDevtools) {
this.docEvents.push(events);
}
Expand Down Expand Up @@ -1095,7 +1103,7 @@ export class Document<T, P extends Indexable = Indexable> {
/**
* `getDocEvents` returns all events of this document.
*/
public getDocEvents(): Array<Array<DocEvent<P>>> {
public getDocEvents(): Array<TransactionDocEvents<P>> {
return this.docEvents;
}

Expand Down Expand Up @@ -1189,7 +1197,7 @@ export class Document<T, P extends Indexable = Indexable> {
this.ensureClone();
change.execute(this.clone!.root, this.clone!.presences, source);

const events: Array<DocEvent<P>> = [];
const events: TransactionDocEvents<P> = [];
const actorID = change.getID().getActorID();
if (change.hasPresenceChange() && this.onlineClients.has(actorID)) {
const presenceChange = change.getPresenceChange()!;
Expand Down Expand Up @@ -1418,6 +1426,15 @@ export class Document<T, P extends Indexable = Indexable> {
}
}

/**
* `applyTransactionDocEvents` applies the TransactionDocEvents into this document.
*/
public applyTransactionDocEvents(events: TransactionDocEvents<P>) {
for (const event of events) {
this.applyDocEvent(event);
}
}

/**
* `getValueByPath` returns the JSONElement corresponding to the given path.
*/
Expand Down Expand Up @@ -1626,7 +1643,7 @@ export class Document<T, P extends Indexable = Indexable> {
this.localChanges.push(change);
this.changeID = change.getID();
const actorID = this.changeID.getActorID();
const events: Array<DocEvent<P>> = [];
const events: TransactionDocEvents<P> = [];
const rawChange = change.toStruct();
if (opInfos.length > 0) {
events.push({
Expand Down Expand Up @@ -1718,7 +1735,7 @@ export class Document<T, P extends Indexable = Indexable> {
this.localChanges.push(change);
this.changeID = change.getID();
const actorID = this.changeID.getActorID();
const events: Array<DocEvent<P>> = [];
const events: TransactionDocEvents<P> = [];
const rawChange = change.toStruct();
if (opInfos.length > 0) {
events.push({
Expand Down
1 change: 1 addition & 0 deletions src/yorkie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export {
RemoteChangeEvent,
Indexable,
DocEvent,
TransactionDocEvents,
Document,
ChangeInfo,
} from '@yorkie-js-sdk/src/document/document';
Expand Down

0 comments on commit b0d310c

Please sign in to comment.