Skip to content

Commit cccb9fc

Browse files
committed
[partykit] broadcastTransactionChanges
1 parent f134fb7 commit cccb9fc

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed

src/persisters/persister-partykit-server.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ export const loadStoreFromStorage = async (
8080
return [tables, values];
8181
};
8282

83+
export const broadcastTransactionChanges = async (
84+
server: TinyBasePartyKitServer,
85+
transactionChanges: TransactionChanges,
86+
without?: string[],
87+
): Promise<void> =>
88+
server.party.broadcast(
89+
construct(server.config.messagePrefix!, SET_CHANGES, transactionChanges),
90+
without,
91+
);
92+
8393
const saveStore = async (
8494
that: TinyBasePartyKitServer,
8595
transactionChanges: TransactionChanges,
@@ -235,20 +245,17 @@ export class TinyBasePartyKitServer implements TinyBasePartyKitServerDecl {
235245

236246
async onMessage(message: string, connection: Connection) {
237247
const {
238-
party: {storage, broadcast},
239248
config: {messagePrefix, storagePrefix},
240249
} = this;
241250
await ifNotUndefined(
242251
deconstruct(messagePrefix!, message, 1),
243252
async ([type, payload]) => {
244253
if (
245254
type == SET_CHANGES &&
246-
(await hasStoreInStorage(storage, storagePrefix))
255+
(await hasStoreInStorage(this.party.storage, storagePrefix))
247256
) {
248257
await saveStore(this, payload, false, connection);
249-
broadcast(construct(messagePrefix!, SET_CHANGES, payload), [
250-
connection.id,
251-
]);
258+
broadcastTransactionChanges(this, payload, [connection.id]);
252259
}
253260
},
254261
);

src/types/docs/persisters/persister-partykit-server.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,3 +527,21 @@
527527
* @since v4.4.1
528528
*/
529529
/// loadStoreFromStorage
530+
/**
531+
* The broadcastTransactionChanges function allows you to broadcast Store
532+
* changes to all the client connections of a TinyBasePartyKitServer.
533+
*
534+
* This is intended for specialist applications that require the ability to
535+
* update clients of a TinyBasePartyKitServer in their own custom ways.
536+
*
537+
* The function is asynchronous, so you should use the `await` keyword or handle
538+
* its completion as a promise.
539+
* @param server A reference to the TinyBasePartyKitServer object.
540+
* @param transactionChanges The Store changes to broadcast to the server's
541+
* clients.
542+
* @param without An optional array of client connection Ids to exclude from the
543+
* broadcast.
544+
* @category Connection
545+
* @since v4.5.1
546+
*/
547+
/// broadcastTransactionChanges

src/types/persisters/persister-partykit-server.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
Cell,
55
CellOrUndefined,
66
Tables,
7+
TransactionChanges,
78
Value,
89
ValueOrUndefined,
910
Values,
@@ -89,3 +90,10 @@ export function loadStoreFromStorage(
8990
storage: Storage,
9091
storagePrefix?: string,
9192
): Promise<[Tables, Values]>;
93+
94+
/// broadcastTransactionChanges
95+
export function broadcastTransactionChanges(
96+
server: TinyBasePartyKitServer,
97+
transactionChanges: TransactionChanges,
98+
without?: string[],
99+
): Promise<void>;

src/types/with-schemas/persisters/persister-partykit-server.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
NoValuesSchema,
88
OptionalSchemas,
99
Tables,
10+
TransactionChanges,
1011
Value,
1112
ValueOrUndefined,
1213
Values,
@@ -92,3 +93,10 @@ export function loadStoreFromStorage<Schemas extends OptionalSchemas>(
9293
storage: Storage,
9394
storagePrefix?: string,
9495
): Promise<[Tables<Schemas[0]>, Values<Schemas[1]>]>;
96+
97+
/// broadcastTransactionChanges
98+
export function broadcastTransactionChanges<Schemas extends OptionalSchemas>(
99+
server: TinyBasePartyKitServer,
100+
transactionChanges: TransactionChanges<Schemas>,
101+
without?: string[],
102+
): Promise<void>;

0 commit comments

Comments
 (0)