Skip to content

Commit

Permalink
feat(client): broadcast proxy (#622)
Browse files Browse the repository at this point in the history
  • Loading branch information
pluvrt authored May 24, 2024
1 parent e587c18 commit 06c67be
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
24 changes: 24 additions & 0 deletions .changeset/brown-pandas-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
"@pluv/client": patch
---

Added broadcast proxy as a new way to broadcast events.

```ts
// backend

const router = io.router({
SEND_MESSAGE: io.procedure
.input(z.object({ message: z.string() }))
.broadcast(({ message }) => ({ RECEIVE_MESSAGE: { message } })),
});

// frontend
const client = createClient(/* ... */);

// Both of the examples below are equivalent.

client.broadcast("SEND_MESSAGE", { message: "Hello world~!" });

client.broadcast.SEND_MESSAGE({ message: "Hello world~!" });
```
23 changes: 17 additions & 6 deletions packages/client/src/PluvRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,25 @@ export class PluvRoom<
return this._state.webSocket;
}

public broadcast<TEvent extends keyof InferIOInput<TIO>>(event: TEvent, data: Id<InferIOInput<TIO>[TEvent]>): void {
if (!this._state.webSocket) return;
if (this._state.connection.state !== ConnectionState.Open) return;
public broadcast = new Proxy(
<TEvent extends keyof InferIOInput<TIO>>(event: TEvent, data: Id<InferIOInput<TIO>[TEvent]>): void => {
if (!this._state.webSocket) return;
if (this._state.connection.state !== ConnectionState.Open) return;

const type = event.toString();
const type = event.toString();

this._sendMessage({ data, type });
}
this._sendMessage({ data, type });
},
{
get(fn, prop) {
return (data: Id<InferIOInput<TIO>[any]>): void => {
return fn(prop, data);
};
},
},
) as (<TEvent extends keyof InferIOInput<TIO>>(event: TEvent, data: Id<InferIOInput<TIO>[TEvent]>) => void) & {
[event in keyof InferIOInput<TIO>]: (data: Id<InferIOInput<TIO>[event]>) => void;
};

public canRedo = (): boolean => {
return !!this._crdtManager.doc.canRedo();
Expand Down

0 comments on commit 06c67be

Please sign in to comment.