Skip to content

Commit

Permalink
Clean up codes
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerwins committed Nov 7, 2024
1 parent 80b2bbb commit d986ef3
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 91 deletions.
32 changes: 16 additions & 16 deletions packages/sdk/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ import { uuid } from '@yorkie-js-sdk/src/util/uuid';
import { Attachment, WatchStream } from '@yorkie-js-sdk/src/client/attachment';
import {
Document,
DocumentKey,
DocumentStatus,
DocKey,
DocStatus,
Indexable,
DocEventType,
StreamConnectionStatus,
DocumentSyncStatus,
DocSyncStatus,
} from '@yorkie-js-sdk/src/document/document';
import { OpSource } from '@yorkie-js-sdk/src/document/operation/operation';
import { createAuthInterceptor } from '@yorkie-js-sdk/src/client/auth_interceptor';
Expand Down Expand Up @@ -186,7 +186,7 @@ export class Client {
private id?: ActorID;
private key: string;
private status: ClientStatus;
private attachmentMap: Map<DocumentKey, Attachment<unknown, any>>;
private attachmentMap: Map<DocKey, Attachment<unknown, any>>;

private apiKey: string;
private authTokenInjector?: (reason?: string) => Promise<string>;
Expand Down Expand Up @@ -320,7 +320,7 @@ export class Client {
`${this.key} is not active`,
);
}
if (doc.getStatus() !== DocumentStatus.Detached) {
if (doc.getStatus() !== DocStatus.Detached) {
throw new YorkieError(
Code.ErrDocumentNotDetached,
`${doc.getKey()} is not detached`,
Expand Down Expand Up @@ -358,11 +358,11 @@ export class Client {
.then(async (res) => {
const pack = converter.fromChangePack<P>(res.changePack!);
doc.applyChangePack(pack);
if (doc.getStatus() === DocumentStatus.Removed) {
if (doc.getStatus() === DocStatus.Removed) {
return doc;
}

doc.applyStatus(DocumentStatus.Attached);
doc.applyStatus(DocStatus.Attached);
this.attachmentMap.set(
doc.getKey(),
new Attachment(
Expand Down Expand Up @@ -446,8 +446,8 @@ export class Client {
.then((res) => {
const pack = converter.fromChangePack<P>(res.changePack!);
doc.applyChangePack(pack);
if (doc.getStatus() !== DocumentStatus.Removed) {
doc.applyStatus(DocumentStatus.Detached);
if (doc.getStatus() !== DocStatus.Removed) {
doc.applyStatus(DocStatus.Detached);
}
this.detachInternal(doc.getKey());

Expand Down Expand Up @@ -646,7 +646,7 @@ export class Client {
* `broadcast` broadcasts the given payload to the given topic.
*/
public broadcast(
docKey: DocumentKey,
docKey: DocKey,
topic: string,
payload: Json,
options?: BroadcastOptions,
Expand Down Expand Up @@ -792,7 +792,7 @@ export class Client {
* `runWatchLoop` runs the watch loop for the given document. The watch loop
* listens to the events of the given document from the server.
*/
private async runWatchLoop(docKey: DocumentKey): Promise<void> {
private async runWatchLoop(docKey: DocKey): Promise<void> {
const attachment = this.attachmentMap.get(docKey);
if (!attachment) {
throw new YorkieError(
Expand Down Expand Up @@ -913,11 +913,11 @@ export class Client {

for (const [key, attachment] of this.attachmentMap) {
this.detachInternal(key);
attachment.doc.applyStatus(DocumentStatus.Detached);
attachment.doc.applyStatus(DocStatus.Detached);
}
}

private detachInternal(docKey: DocumentKey) {
private detachInternal(docKey: DocKey) {
// NOTE(hackerwins): If attachment is not found, it means that the document
// has been already detached by another routine.
// This can happen when detach or remove is called while the watch loop is
Expand Down Expand Up @@ -966,12 +966,12 @@ export class Client {
attachment.doc.publish([
{
type: DocEventType.SyncStatusChanged,
value: DocumentSyncStatus.Synced,
value: DocSyncStatus.Synced,
},
]);
// NOTE(chacha912): If a document has been removed, watchStream should
// be disconnected to not receive an event for that document.
if (doc.getStatus() === DocumentStatus.Removed) {
if (doc.getStatus() === DocStatus.Removed) {
this.detachInternal(doc.getKey());
}

Expand All @@ -988,7 +988,7 @@ export class Client {
doc.publish([
{
type: DocEventType.SyncStatusChanged,
value: DocumentSyncStatus.SyncFailed,
value: DocSyncStatus.SyncFailed,
},
]);
logger.error(`[PP] c:"${this.getKey()}" err :`, err);
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/document/change/change_pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class ChangePack<P extends Indexable> {
}

/**
* `getKey` returns the document key of this pack.
* `getDocumentKey` returns the document key of this pack.
*/
public getDocumentKey(): string {
return this.documentKey;
Expand Down
49 changes: 25 additions & 24 deletions packages/sdk/src/document/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ export interface DocumentOptions {
}

/**
* `DocumentStatus` represents the status of the document.
* `DocStatus` represents the status of the document.
* @public
*/
export enum DocumentStatus {
export enum DocStatus {
/**
* Detached means that the document is not attached to the client.
* The actor of the ticket is created without being assigned.
Expand Down Expand Up @@ -254,9 +254,9 @@ export interface StatusChangedEvent extends BaseDocEvent {
type: DocEventType.StatusChanged;
source: OpSource;
value:
| { status: DocumentStatus.Attached; actorID: string }
| { status: DocumentStatus.Detached }
| { status: DocumentStatus.Removed };
| { status: DocStatus.Attached; actorID: string }
| { status: DocStatus.Detached }
| { status: DocStatus.Removed };
}

/**
Expand Down Expand Up @@ -288,10 +288,10 @@ export interface ConnectionChangedEvent extends BaseDocEvent {
}

/**
* `DocumentSyncStatus` represents the result of synchronizing the document with the server.
* `DocSyncStatus` represents the result of synchronizing the document with the server.
* @public
*/
export enum DocumentSyncStatus {
export enum DocSyncStatus {
/**
* `Synced` means that document synced successfully.
*/
Expand All @@ -312,7 +312,7 @@ export interface SyncStatusChangedEvent extends BaseDocEvent {
* enum {@link DocEventType}.SyncStatusChanged
*/
type: DocEventType.SyncStatusChanged;
value: DocumentSyncStatus;
value: DocSyncStatus;
}

/**
Expand Down Expand Up @@ -474,10 +474,10 @@ type JsonObject = { [key: string]: Json | undefined };
export type Indexable = Record<string, Json>;

/**
* `DocumentKey` represents the key of the document.
* `DocKey` represents the key of the document.
* @public
*/
export type DocumentKey = string;
export type DocKey = string;

/**
* `OperationInfoOfElement` represents the type of the operation info of the given element.
Expand Down Expand Up @@ -619,8 +619,8 @@ type PathOf<TDocument, Depth extends number = 10> = PathOfInternal<
* @public
*/
export class Document<T, P extends Indexable = Indexable> {
private key: DocumentKey;
private status: DocumentStatus;
private key: DocKey;
private status: DocStatus;
private opts: DocumentOptions;

private changeID: ChangeID;
Expand Down Expand Up @@ -666,7 +666,7 @@ export class Document<T, P extends Indexable = Indexable> {
this.opts = opts || {};

this.key = key;
this.status = DocumentStatus.Detached;
this.status = DocStatus.Detached;
this.root = CRDTRoot.create();

this.changeID = InitialChangeID;
Expand Down Expand Up @@ -699,7 +699,7 @@ export class Document<T, P extends Indexable = Indexable> {
updater: (root: JSONObject<T>, presence: Presence<P>) => void,
message?: string,
): void {
if (this.getStatus() === DocumentStatus.Removed) {
if (this.getStatus() === DocStatus.Removed) {
throw new YorkieError(Code.ErrDocumentRemoved, `${this.key} is removed`);
}

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

// 05. Update the status.
if (pack.getIsRemoved()) {
this.applyStatus(DocumentStatus.Removed);
this.applyStatus(DocStatus.Removed);
}

if (logger.isEnabled(LogLevel.Trivial)) {
Expand Down Expand Up @@ -1340,7 +1340,7 @@ export class Document<T, P extends Indexable = Indexable> {
/**
* `getStatus` returns the status of this document.
*/
public getStatus(): DocumentStatus {
public getStatus(): DocStatus {
return this.status;
}

Expand Down Expand Up @@ -1700,20 +1700,19 @@ export class Document<T, P extends Indexable = Indexable> {
/**
* `applyStatus` applies the document status into this document.
*/
public applyStatus(status: DocumentStatus) {
public applyStatus(status: DocStatus) {
this.status = status;

if (status === DocumentStatus.Detached) {
if (status === DocStatus.Detached) {
this.setActor(InitialActorID);
}

this.publish([
{
source:
status === DocumentStatus.Removed ? OpSource.Remote : OpSource.Local,
source: status === DocStatus.Removed ? OpSource.Remote : OpSource.Local,
type: DocEventType.StatusChanged,
value:
status === DocumentStatus.Attached
status === DocStatus.Attached
? { status, actorID: this.changeID.getActorID() }
: { status },
},
Expand All @@ -1726,7 +1725,7 @@ export class Document<T, P extends Indexable = Indexable> {
public applyDocEvent(event: DocEvent<P>) {
if (event.type === DocEventType.StatusChanged) {
this.applyStatus(event.value.status);
if (event.value.status === DocumentStatus.Attached) {
if (event.value.status === DocStatus.Attached) {
this.setActor(event.value.actorID);
}
return;
Expand All @@ -1735,7 +1734,9 @@ export class Document<T, P extends Indexable = Indexable> {
if (event.type === DocEventType.Snapshot) {
const { snapshot, serverSeq, snapshotVector } = event.value;
if (!snapshot) return;
// TODO(hackerwins): We need to check the clientSeq of the snapshot.

// TODO(hackerwins): We need to check version vector and lamport clock
// of the snapshot is valid.
this.applySnapshot(
BigInt(serverSeq),
converter.hexToVersionVector(snapshotVector),
Expand Down Expand Up @@ -1862,7 +1863,7 @@ export class Document<T, P extends Indexable = Indexable> {
public getMyPresence(): P {
// TODO(chacha912): After resolving the presence initialization issue,
// remove default presence.(#608)
if (this.status !== DocumentStatus.Attached) {
if (this.status !== DocStatus.Attached) {
return {} as P;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/yorkie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ export {
type PresenceChangedEvent,
type InitializedEvent,
StreamConnectionStatus,
DocumentSyncStatus,
DocumentStatus,
DocSyncStatus,
DocStatus,
type Indexable,
type DocEvent,
type TransactionEvent,
Expand Down
8 changes: 4 additions & 4 deletions packages/sdk/test/bench/document.bench.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Document, JSONArray } from '@yorkie-js-sdk/src/yorkie';
import { InitialCheckpoint } from '@yorkie-js-sdk/src/document/change/checkpoint';
import { DocumentStatus } from '@yorkie-js-sdk/src/document/document';
import { DocStatus } from '@yorkie-js-sdk/src/document/document';
import { describe, bench, assert } from 'vitest';
import { MaxVersionVector } from '../helper/helper';

Expand Down Expand Up @@ -56,9 +56,9 @@ describe('Document', () => {

bench('status', () => {
const doc = new Document<{ text: JSONArray<string> }>(`test-doc`);
assert.equal(doc.getStatus(), DocumentStatus.Detached);
doc.applyStatus(DocumentStatus.Attached);
assert.equal(doc.getStatus(), DocumentStatus.Attached);
assert.equal(doc.getStatus(), DocStatus.Detached);
doc.applyStatus(DocStatus.Attached);
assert.equal(doc.getStatus(), DocStatus.Attached);
});

bench('equals', () => {
Expand Down
Loading

0 comments on commit d986ef3

Please sign in to comment.