Skip to content

Commit

Permalink
Apply Json type to Broadcast API and Devtools
Browse files Browse the repository at this point in the history
  • Loading branch information
gwbaik9717 committed Sep 8, 2024
1 parent 3806420 commit 7ad07da
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 19 deletions.
3 changes: 2 additions & 1 deletion packages/sdk/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { OpSource } from '@yorkie-js-sdk/src/document/operation/operation';
import { createAuthInterceptor } from '@yorkie-js-sdk/src/client/auth_interceptor';
import { createMetricInterceptor } from '@yorkie-js-sdk/src/client/metric_interceptor';
import { validateSerializable } from '../util/validator';
import { Json } from '@yorkie-js-sdk/src/document/document';

/**
* `SyncMode` defines synchronization modes for the PushPullChanges API.
Expand Down Expand Up @@ -607,7 +608,7 @@ export class Client {
public broadcast(
docKey: DocumentKey,
topic: string,
payload: any,
payload: Json,
): Promise<void> {
if (!this.isActive()) {
throw new YorkieError(
Expand Down
15 changes: 1 addition & 14 deletions packages/sdk/src/devtools/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,7 @@
import type { PrimitiveValue } from '@yorkie-js-sdk/src/document/crdt/primitive';
import type { CRDTTreePosStruct } from '@yorkie-js-sdk/src/document/crdt/tree';
import { CounterValue } from '@yorkie-js-sdk/src/document/crdt/counter';

/**
* `Json` represents a JSON value.
*
* TODO(hackerwins): We need to replace `Indexable` with `Json`.
*/
export type Json =
| string
| number
| boolean
// eslint-disable-next-line @typescript-eslint/ban-types
| null
| { [key: string]: Json }
| Array<Json>;
import { Json } from '@yorkie-js-sdk/src/document/document';

/**
* `Client` represents a client value in devtools.
Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/src/document/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ export interface PresenceChangedEvent<P extends Indexable>

export interface BroadcastEvent extends BaseDocEvent {
type: DocEventType.Broadcast;
value: { clientID: ActorID; topic: string; payload: any };
value: { clientID: ActorID; topic: string; payload: Json };
error?: ErrorFn;
}

Expand Down Expand Up @@ -420,7 +420,7 @@ export type DocEventTopic = keyof DocEventCallbackMap<never>;
export type DocEventCallback<P extends Indexable> =
DocEventCallbackMap<P>[DocEventTopic];

type Json = JsonScalar | JsonArray | JsonObject;
export type Json = JsonScalar | JsonArray | JsonObject;

// eslint-disable-next-line @typescript-eslint/ban-types
type JsonScalar = string | number | boolean | null;
Expand Down Expand Up @@ -2065,7 +2065,7 @@ export class Document<T, P extends Indexable = Indexable> {
/**
* `broadcast` the payload to the given topic.
*/
public broadcast(topic: string, payload: any, error?: ErrorFn) {
public broadcast(topic: string, payload: Json, error?: ErrorFn) {
const broadcastEvent: LocalBroadcastEvent = {
type: DocEventType.LocalBroadcast,
value: { topic, payload },
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/util/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* `validateSerializable` returns whether the given value is serializable or not.
*/
export const validateSerializable = (value: any): boolean => {
export const validateSerializable = (value: unknown): boolean => {
try {
const serialized = JSON.stringify(value);

Expand Down
2 changes: 2 additions & 0 deletions packages/sdk/test/integration/client_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,8 @@ describe.sequential('Client', function () {
eventCollector.add(error.message);
};

// @ts-ignore
// Disable type checking for testing purposes
doc.broadcast(broadcastTopic, payload, errorHandler);

await eventCollector.waitAndVerifyNthEvent(1, broadcastErrMessage);
Expand Down

0 comments on commit 7ad07da

Please sign in to comment.