Skip to content

Commit

Permalink
Revise the codes
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerwins committed Jan 14, 2025
1 parent 841c444 commit 097af47
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 41 deletions.
6 changes: 3 additions & 3 deletions packages/devtools/src/devtools/contexts/YorkieSource.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { Code, YorkieError } from '@yorkie-js-sdk/src/util/error';
const DocKeyContext = createContext<string>(null);
const YorkieDocContext = createContext(null);
const ReplayDocEventsContext = createContext<{
events: Array<Devtools.EventsForDocReplay>;
events: Array<Devtools.DocEventsForReplay>;
hidePresenceEvents: boolean;
setHidePresenceEvents: Dispatch<SetStateAction<boolean>>;
}>(null);
Expand All @@ -44,7 +44,7 @@ export function YorkieSourceProvider({ children }: Props) {
const [currentDocKey, setCurrentDocKey] = useState<string>('');
const [doc, setDoc] = useState(null);
const [replayDocEvents, setReplayDocEvents] = useState<
Array<Devtools.EventsForDocReplay>
Array<Devtools.DocEventsForReplay>
>([]);

// filter out presence events
Expand Down Expand Up @@ -146,7 +146,7 @@ export enum ReplayDocEventType {
}

export const getReplayDocEventType = (
event: Devtools.EventsForDocReplay,
event: Devtools.DocEventsForReplay,
): ReplayDocEventType => {
for (const docEvent of event) {
if (
Expand Down
2 changes: 1 addition & 1 deletion packages/devtools/src/devtools/panel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const Panel = () => {
filteredEventIndex++;
}

doc.applyEventsForDocReplay(originalEvents[eventIndex].event);
doc.applyDocEventsForReplay(originalEvents[eventIndex].event);
eventIndex++;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/devtools/src/devtools/tabs/History.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {

const SLIDER_MARK_WIDTH = 24;

const getEventInfo = (event: Devtools.EventsForDocReplay) => {
const getEventInfo = (event: Devtools.DocEventsForReplay) => {
const info = [];
for (const docEvent of event) {
if (
Expand Down
8 changes: 4 additions & 4 deletions packages/sdk/src/devtools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { Document, Indexable } from '@yorkie-js-sdk/src/yorkie';
import { logger } from '@yorkie-js-sdk/src/util/logger';
import type * as DevTools from './protocol';
import { EventSourceDevPanel, EventSourceSDK } from './protocol';
import { EventsForDocReplay, isEventsForDocReplay } from './types';
import { DocEventsForReplay, isDocEventsForReplay } from './types';

type DevtoolsStatus = 'connected' | 'disconnected' | 'synced';
let devtoolsStatus: DevtoolsStatus = 'disconnected';
Expand All @@ -29,10 +29,10 @@ const unsubsByDocKey = new Map<string, Array<() => void>>();
* (time-traveling feature) in Devtools. Later, external storage such as
* IndexedDB will be used.
*/
const replayEventsByDocKey = new Map<string, Array<EventsForDocReplay>>();
const replayEventsByDocKey = new Map<string, Array<DocEventsForReplay>>();
declare global {
interface Window {
replayEventsByDocKey: Map<string, Array<EventsForDocReplay>>;
replayEventsByDocKey: Map<string, Array<DocEventsForReplay>>;
}
}
if (typeof window !== 'undefined') {
Expand Down Expand Up @@ -77,7 +77,7 @@ export function setupDevtools<T, P extends Indexable>(

replayEventsByDocKey.set(doc.getKey(), []);
const unsub = doc.subscribe('all', (event) => {
if (!isEventsForDocReplay(event)) {
if (!isDocEventsForReplay(event)) {
return;
}

Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/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 { EventsForDocReplay } from './types';
import { DocEventsForReplay } from './types';

/**
* `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<EventsForDocReplay>;
events: Array<DocEventsForReplay>;
}
/**
* Sent whenever the document is changed.
*/
| {
msg: 'doc::sync::partial';
docKey: string;
event: EventsForDocReplay;
event: DocEventsForReplay;
};

export type FullPanelToSDKMessage = PanelToSDKMessage & {
Expand Down
22 changes: 11 additions & 11 deletions packages/sdk/src/devtools/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ export type TreeNodeInfo = {
};

/**
* `EventForDocReplay` is an event used to replay a document.
* `DocEventForReplay` is an event used to replay a document.
*/
export type EventForDocReplay<
export type DocEventForReplay<
P extends Indexable = Indexable,
T = OperationInfo,
> =
Expand All @@ -117,16 +117,16 @@ export type EventForDocReplay<
| PresenceChangedEvent<P>;

/**
* `EventsForDocReplay` is a list of events used to replay a document.
* `DocEventsForReplay` is a list of events used to replay a document.
*/
export type EventsForDocReplay = Array<EventForDocReplay>;
export type DocEventsForReplay = Array<DocEventForReplay>;

/**
* `isEventForDocReplay` checks if an event can be used to replay a document.
* `isDocEventForReplay` checks if an event can be used to replay a document.
*/
export function isEventForDocReplay(
export function isDocEventForReplay(
event: DocEvent,
): event is EventForDocReplay {
): event is DocEventForReplay {
const typesForDocReplay = [
DocEventType.StatusChanged,
DocEventType.Snapshot,
Expand All @@ -142,10 +142,10 @@ export function isEventForDocReplay(
}

/**
* `isEventsForDocReplay` checks if a list of events can be used to replay a document.
* `isDocEventsForReplay` checks if a list of events can be used to replay a document.
*/
export function isEventsForDocReplay(
export function isDocEventsForReplay(
events: Array<DocEvent>,
): events is EventsForDocReplay {
return events.every(isEventForDocReplay);
): events is DocEventsForReplay {
return events.every(isDocEventForReplay);
}
32 changes: 15 additions & 17 deletions packages/sdk/src/document/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,10 @@ export type DocEvent<P extends Indexable = Indexable, T = OperationInfo> =
| AuthErrorEvent;

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

/**
* @internal
Expand Down Expand Up @@ -449,7 +447,7 @@ type DocEventCallbackMap<P extends Indexable> = {
broadcast: NextFn<BroadcastEvent>;
'local-broadcast': NextFn<LocalBroadcastEvent>;
'auth-error': NextFn<AuthErrorEvent>;
all: NextFn<TransactionEvent<P>>;
all: NextFn<DocEvents<P>>;
};
export type DocEventTopic = keyof DocEventCallbackMap<never>;
export type DocEventCallback<P extends Indexable> =
Expand Down Expand Up @@ -633,8 +631,8 @@ export class Document<T, P extends Indexable = Indexable> {
presences: Map<ActorID, P>;
};

private eventStream: Observable<TransactionEvent<P>>;
private eventStreamObserver!: Observer<TransactionEvent<P>>;
private eventStream: Observable<DocEvents<P>>;
private eventStreamObserver!: Observer<DocEvents<P>>;

/**
* `onlineClients` is a set of client IDs that are currently online.
Expand Down Expand Up @@ -673,7 +671,7 @@ export class Document<T, P extends Indexable = Indexable> {
this.checkpoint = InitialCheckpoint;
this.localChanges = [];

this.eventStream = createObservable<TransactionEvent<P>>((observer) => {
this.eventStream = createObservable<DocEvents<P>>((observer) => {
this.eventStreamObserver = observer;
});

Expand Down Expand Up @@ -771,7 +769,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 event: TransactionEvent<P> = [];
const event: DocEvents<P> = [];
if (opInfos.length > 0) {
event.push({
type: DocEventType.LocalChange,
Expand Down Expand Up @@ -1168,7 +1166,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(event: TransactionEvent<P>) {
public publish(event: DocEvents<P>) {
if (this.eventStreamObserver) {
this.eventStreamObserver.next(event);
}
Expand Down Expand Up @@ -1522,7 +1520,7 @@ export class Document<T, P extends Indexable = Indexable> {
this.ensureClone();
change.execute(this.clone!.root, this.clone!.presences, source);

const event: TransactionEvent<P> = [];
const event: DocEvents<P> = [];
const actorID = change.getID().getActorID();
if (change.hasPresenceChange() && this.onlineClients.has(actorID)) {
const presenceChange = change.getPresenceChange()!;
Expand Down Expand Up @@ -1718,9 +1716,9 @@ export class Document<T, P extends Indexable = Indexable> {
}

/**
* `applyEventForDocReplay` applies the given event into this document.
* `applyDocEventForReplay` applies the given event into this document.
*/
public applyEventForDocReplay(event: Devtools.EventForDocReplay<P>) {
public applyDocEventForReplay(event: Devtools.DocEventForReplay<P>) {
if (event.type === DocEventType.StatusChanged) {
this.applyStatus(event.value.status);
if (event.value.status === DocStatus.Attached) {
Expand Down Expand Up @@ -1784,9 +1782,9 @@ export class Document<T, P extends Indexable = Indexable> {
/**
* `applyEventsForDocReplay` applies the given events into this document.
*/
public applyEventsForDocReplay(event: Array<Devtools.EventForDocReplay<P>>) {
public applyDocEventsForReplay(event: Array<Devtools.DocEventForReplay<P>>) {
for (const docEvent of event) {
this.applyEventForDocReplay(docEvent);
this.applyDocEventForReplay(docEvent);
}
}

Expand Down Expand Up @@ -2034,7 +2032,7 @@ export class Document<T, P extends Indexable = Indexable> {
this.localChanges.push(change);
this.changeID = change.getID();
const actorID = this.changeID.getActorID();
const event: TransactionEvent<P> = [];
const event: DocEvents<P> = [];
if (opInfos.length > 0) {
event.push({
type: DocEventType.LocalChange,
Expand Down Expand Up @@ -2133,7 +2131,7 @@ export class Document<T, P extends Indexable = Indexable> {
this.localChanges.push(change);
this.changeID = change.getID();
const actorID = this.changeID.getActorID();
const event: TransactionEvent<P> = [];
const event: DocEvents<P> = [];
if (opInfos.length > 0) {
event.push({
type: DocEventType.LocalChange,
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/yorkie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export {
type Indexable,
type Json,
type DocEvent,
type TransactionEvent,
type DocEvents,
Document,
type ChangeInfo,
} from '@yorkie-js-sdk/src/document/document';
Expand Down

0 comments on commit 097af47

Please sign in to comment.