Skip to content

Commit

Permalink
Refactor by removing unnecessary broadcastEventHanlders
Browse files Browse the repository at this point in the history
  • Loading branch information
gwbaik9717 committed Aug 28, 2024
1 parent 69650c0 commit 86035b8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 36 deletions.
32 changes: 4 additions & 28 deletions packages/sdk/src/document/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ type DocEventCallbackMap<P extends Indexable> = {
connection: NextFn<ConnectionChangedEvent>;
status: NextFn<StatusChangedEvent>;
sync: NextFn<SyncStatusChangedEvent>;
broadcast: (topic: string, payload: any) => void;
broadcast: (payload: any) => void;
all: NextFn<TransactionEvent<P>>;
};
export type DocEventTopic = keyof DocEventCallbackMap<never>;
Expand Down Expand Up @@ -617,15 +617,6 @@ export class Document<T, P extends Indexable = Indexable> {
*/
private isUpdating: boolean;

/**
* `broadcastEventHandlers` is a map of broadcast event handlers.
* The key is the topic of the broadcast event, and the value is the handler.
*/
private broadcastEventHandlers: Map<
string,
DocEventCallbackMap<P>['broadcast']
>;

private client?: Client;

constructor(key: string, opts?: DocumentOptions) {
Expand Down Expand Up @@ -655,8 +646,6 @@ export class Document<T, P extends Indexable = Indexable> {
redo: this.redo.bind(this),
};

this.broadcastEventHandlers = new Map();

setupDevtools(this);
}

Expand Down Expand Up @@ -1085,23 +1074,18 @@ export class Document<T, P extends Indexable = Indexable> {
const { topic } = arg1 as BroadcastSubscribePair;
const handler = arg2 as DocEventCallbackMap<P>['broadcast'];
const error = arg3 as ErrorFn;
this.broadcastEventHandlers.set(topic, handler);
const unsubscribe = this.eventStream.subscribe((event) => {

return this.eventStream.subscribe((event) => {
for (const docEvent of event) {
if (docEvent.type !== DocEventType.Broadcast) {
continue;
}

if (docEvent.value.topic === topic) {
handler(topic, docEvent.value.payload);
handler(docEvent.value.payload);
}
}
}, error);

return () => {
unsubscribe();
this.broadcastEventHandlers.delete(topic);
};
}
}
throw new YorkieError(Code.ErrInvalidArgument, `"${arg1}" is not a valid`);
Expand Down Expand Up @@ -1684,14 +1668,6 @@ export class Document<T, P extends Indexable = Indexable> {
const { clientID, presence } = event.value;
this.presences.set(clientID, presence);
}

if (event.type === DocEventType.Broadcast) {
const { topic, payload } = event.value;
const handler = this.broadcastEventHandlers.get(topic);
if (handler) {
handler(topic, payload);
}
}
}

/**
Expand Down
16 changes: 8 additions & 8 deletions packages/sdk/test/integration/client_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -907,18 +907,18 @@ it('Should trigger the handler for a subscribed broadcast event', async ({
}) => {
await withTwoClientsAndDocuments<{ t: Text }>(
async (c1, d1, c2, d2) => {
const eventCollector = new EventCollector<[string, any]>();
const eventCollector = new EventCollector<[any]>();
const broadcastTopic = 'test';
const unsubscribe = d2.subscribe(
{ type: 'broadcast', topic: broadcastTopic },
(topic, payload) => {
eventCollector.add([topic, payload]);
(payload) => {
eventCollector.add([payload]);
},
);

const payload = { a: 1, b: '2' };
await d1.broadcast(broadcastTopic, payload);
await eventCollector.waitAndVerifyNthEvent(1, [broadcastTopic, payload]);
await eventCollector.waitAndVerifyNthEvent(1, [payload]);

unsubscribe();
},
Expand Down Expand Up @@ -964,19 +964,19 @@ it('Should not trigger the handler for a broadcast event after unsubscribing', a
async (c1, d1, c2, d2) => {
const spy = vi.fn();

const eventCollector = new EventCollector<[string, any]>();
const eventCollector = new EventCollector<[any]>();
const broadcastTopic = 'test';
const unsubscribe = d2.subscribe(
{ type: 'broadcast', topic: broadcastTopic },
(topic, payload) => {
(payload) => {
spy();
eventCollector.add([topic, payload]);
eventCollector.add([payload]);
},
);

const payload = { a: 1, b: '2' };
await d1.broadcast(broadcastTopic, payload);
await eventCollector.waitAndVerifyNthEvent(1, [broadcastTopic, payload]);
await eventCollector.waitAndVerifyNthEvent(1, [payload]);

unsubscribe();

Expand Down

0 comments on commit 86035b8

Please sign in to comment.