From b0d310cc095ee026cfae0287fdcb7c22c5f23de5 Mon Sep 17 00:00:00 2001 From: Yourim Cha Date: Thu, 18 Apr 2024 14:26:41 +0900 Subject: [PATCH] Define type `Array` as TransactionDocEvents --- src/devtools/protocol.ts | 6 +++--- src/document/document.ts | 43 ++++++++++++++++++++++++++++------------ src/yorkie.ts | 1 + 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/src/devtools/protocol.ts b/src/devtools/protocol.ts index eceb8dba7..aedef782b 100644 --- a/src/devtools/protocol.ts +++ b/src/devtools/protocol.ts @@ -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 @@ -76,7 +76,7 @@ export type SDKToPanelMessage = | { msg: 'doc::sync::full'; docKey: string; - events: Array>; + events: Array; } /** * Sent whenever the document is changed. @@ -84,7 +84,7 @@ export type SDKToPanelMessage = | { msg: 'doc::sync::partial'; docKey: string; - event: Array; + event: TransactionDocEvents; }; export type FullPanelToSDKMessage = PanelToSDKMessage & { diff --git a/src/document/document.ts b/src/document/document.ts index 06aa77e9e..d7039787b 100644 --- a/src/document/document.ts +++ b/src/document/document.ts @@ -182,6 +182,14 @@ export type DocEvent

= | UnwatchedEvent

| PresenceChangedEvent

; +/** + * `TransactionDocEvents` represents document events that occur within + * a single transaction (e.g., doc.update). + */ +export type TransactionDocEvents

= Array< + DocEvent

+>; + /** * @internal */ @@ -461,8 +469,8 @@ export class Document { presences: Map; }; - private eventStream: Observable>>; - private eventStreamObserver!: Observer>>; + private eventStream: Observable>; + private eventStreamObserver!: Observer>; /** * `onlineClients` is a set of client IDs that are currently online. @@ -495,7 +503,7 @@ export class Document { * (time-traveling feature) in Devtools. Later, external storage such as * IndexedDB will be used. */ - public docEvents: Array>>; + public docEvents: Array>; constructor(key: string, opts?: DocumentOptions) { this.opts = opts || {}; @@ -510,7 +518,7 @@ export class Document { this.checkpoint = InitialCheckpoint; this.localChanges = []; - this.eventStream = createObservable>>((observer) => { + this.eventStream = createObservable>((observer) => { this.eventStreamObserver = observer; }); @@ -609,7 +617,7 @@ export class Document { // 03. Publish the document change event. // NOTE(chacha912): Check opInfos, which represent the actually executed operations. - const events: Array> = []; + const events: TransactionDocEvents

= []; const rawChange = change.toStruct(); if (opInfos.length > 0) { events.push({ @@ -701,7 +709,7 @@ export class Document { */ public subscribe( type: 'all', - next: NextFn>>, + next: NextFn>, error?: ErrorFn, complete?: CompleteFn, ): Unsubscribe; @@ -716,7 +724,7 @@ export class Document { arg2?: | NextFn> | NextFn> - | NextFn>> + | NextFn> | ErrorFn, arg3?: ErrorFn | CompleteFn, arg4?: CompleteFn, @@ -797,7 +805,7 @@ export class Document { ); } if (arg1 === 'all') { - const callback = arg2 as NextFn>>; + const callback = arg2 as NextFn>; return this.eventStream.subscribe(callback, arg3, arg4); } const target = arg1; @@ -864,7 +872,7 @@ export class Document { * `publish` triggers an event in this document, which can be received by * callback functions from document.subscribe(). */ - public publish(events: Array>) { + public publish(events: TransactionDocEvents

) { if (this.opts.enableDevtools) { this.docEvents.push(events); } @@ -1095,7 +1103,7 @@ export class Document { /** * `getDocEvents` returns all events of this document. */ - public getDocEvents(): Array>> { + public getDocEvents(): Array> { return this.docEvents; } @@ -1189,7 +1197,7 @@ export class Document { this.ensureClone(); change.execute(this.clone!.root, this.clone!.presences, source); - const events: Array> = []; + const events: TransactionDocEvents

= []; const actorID = change.getID().getActorID(); if (change.hasPresenceChange() && this.onlineClients.has(actorID)) { const presenceChange = change.getPresenceChange()!; @@ -1418,6 +1426,15 @@ export class Document { } } + /** + * `applyTransactionDocEvents` applies the TransactionDocEvents into this document. + */ + public applyTransactionDocEvents(events: TransactionDocEvents

) { + for (const event of events) { + this.applyDocEvent(event); + } + } + /** * `getValueByPath` returns the JSONElement corresponding to the given path. */ @@ -1626,7 +1643,7 @@ export class Document { this.localChanges.push(change); this.changeID = change.getID(); const actorID = this.changeID.getActorID(); - const events: Array> = []; + const events: TransactionDocEvents

= []; const rawChange = change.toStruct(); if (opInfos.length > 0) { events.push({ @@ -1718,7 +1735,7 @@ export class Document { this.localChanges.push(change); this.changeID = change.getID(); const actorID = this.changeID.getActorID(); - const events: Array> = []; + const events: TransactionDocEvents

= []; const rawChange = change.toStruct(); if (opInfos.length > 0) { events.push({ diff --git a/src/yorkie.ts b/src/yorkie.ts index 17cfe293b..6ee1dc50d 100644 --- a/src/yorkie.ts +++ b/src/yorkie.ts @@ -43,6 +43,7 @@ export { RemoteChangeEvent, Indexable, DocEvent, + TransactionDocEvents, Document, ChangeInfo, } from '@yorkie-js-sdk/src/document/document';