Skip to content

Commit 193ea20

Browse files
committed
chore: better types
1 parent 9375494 commit 193ea20

File tree

6 files changed

+60
-34
lines changed

6 files changed

+60
-34
lines changed

packages/extension-database/src/Database.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import type {
22
Extension,
3+
fetchPayload,
34
onChangePayload,
45
onLoadDocumentPayload,
6+
onStoreDocumentPayload,
57
storePayload,
6-
fetchPayload,
78
} from "@hocuspocus/server";
89
import * as Y from "yjs";
910

@@ -52,7 +53,7 @@ export class Database implements Extension {
5253
/**
5354
* Store new updates in the database.
5455
*/
55-
async onStoreDocument(data: onChangePayload) {
56+
async onStoreDocument(data: onStoreDocumentPayload) {
5657
await this.configuration.store({
5758
...data,
5859
state: Buffer.from(Y.encodeStateAsUpdate(data.document)),

packages/extension-logger/src/Logger.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import type {
33
onChangePayload,
44
onConfigurePayload,
55
onConnectPayload,
6-
onLoadDocumentPayload,
76
onDestroyPayload,
87
onDisconnectPayload,
8+
onLoadDocumentPayload,
99
onRequestPayload,
10+
onStoreDocumentPayload,
1011
onUpgradePayload,
1112
} from "@hocuspocus/server";
1213

@@ -112,7 +113,7 @@ export class Logger implements Extension {
112113
}
113114
}
114115

115-
async onStoreDocument(data: onDisconnectPayload) {
116+
async onStoreDocument(data: onStoreDocumentPayload) {
116117
if (this.configuration.onStoreDocument) {
117118
this.log(`Store "${data.documentName}".`);
118119
}

packages/extension-redis/src/Redis.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ export class Redis implements Extension {
281281
*/
282282
async afterStoreDocument({
283283
documentName,
284-
socketId,
284+
lastTransactionOrigin,
285285
}: afterStoreDocumentPayload) {
286286
const lockKey = this.lockKey(documentName);
287287
const lock = this.locks.get(lockKey);
@@ -296,9 +296,13 @@ export class Redis implements Extension {
296296
this.locks.delete(lockKey);
297297
}
298298
}
299-
// if the change was initiated by a directConnection, we need to delay this hook to make sure sync can finish first.
299+
300+
// if the change was initiated by a directConnection (or otherwise local source), we need to delay this hook to make sure sync can finish first.
300301
// for provider connections, this usually happens in the onDisconnect hook
301-
if (socketId === "server") {
302+
if (
303+
isTransactionOrigin(lastTransactionOrigin) &&
304+
lastTransactionOrigin.source === "local"
305+
) {
302306
const pending = this.pendingAfterStoreDocumentResolves.get(documentName);
303307

304308
if (pending) {

packages/server/src/DirectConnection.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,14 @@ export class DirectConnection<Context = any>
5151
this.document,
5252
{
5353
clientsCount: this.document.getConnectionsCount(),
54-
context: this.context,
54+
lastContext: this.context,
55+
lastTransactionOrigin: {
56+
source: "local",
57+
context: this.context,
58+
} satisfies LocalTransactionOrigin,
5559
document: this.document,
5660
documentName: this.document.name,
5761
instance: this.instance,
58-
requestHeaders: {},
59-
requestParameters: new URLSearchParams(),
60-
socketId: "server",
6162
},
6263
true,
6364
);

packages/server/src/Hocuspocus.ts

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -264,26 +264,36 @@ export class Hocuspocus<Context = any> {
264264
: {}
265265
: {};
266266

267-
const hookPayload: onChangePayload | onStoreDocumentPayload = {
267+
const changePayload: onChangePayload = {
268268
instance: this,
269269
clientsCount: document.getConnectionsCount(),
270-
context,
271270
document,
272271
documentName: document.name,
273272
requestHeaders: request?.headers ?? {},
274273
requestParameters: getParameters(request),
275274
socketId: connection?.socketId ?? "",
276275
update,
277276
transactionOrigin: origin,
277+
connection: connection,
278+
context,
278279
};
279280

280-
this.hooks("onChange", hookPayload);
281+
this.hooks("onChange", changePayload);
281282

282283
if (shouldSkipStoreHooks(origin)) {
283284
return;
284285
}
285286

286-
this.storeDocumentHooks(document, hookPayload);
287+
const storePayload: onStoreDocumentPayload = {
288+
instance: this,
289+
clientsCount: document.getConnectionsCount(),
290+
document,
291+
lastContext: context,
292+
lastTransactionOrigin: origin,
293+
documentName: document.name,
294+
};
295+
296+
this.storeDocumentHooks(document, storePayload);
287297
}
288298

289299
/**
@@ -408,14 +418,24 @@ export class Hocuspocus<Context = any> {
408418
},
409419
);
410420

411-
document.awareness.on("update", (update: AwarenessUpdate) => {
412-
this.hooks("onAwarenessUpdate", {
413-
...hookPayload,
414-
...update,
415-
awareness: document.awareness,
416-
states: awarenessStatesToArray(document.awareness.getStates()),
417-
});
418-
});
421+
document.awareness.on(
422+
"update",
423+
(update: AwarenessUpdate, origin: unknown) => {
424+
this.hooks("onAwarenessUpdate", {
425+
document,
426+
documentName,
427+
instance: this,
428+
...update,
429+
transactionOrigin: origin,
430+
connection:
431+
isTransactionOrigin(origin) && origin.source === "connection"
432+
? origin.connection
433+
: undefined,
434+
awareness: document.awareness,
435+
states: awarenessStatesToArray(document.awareness.getStates()),
436+
});
437+
},
438+
);
419439

420440
return document;
421441
}
@@ -460,6 +480,7 @@ export class Hocuspocus<Context = any> {
460480
hooks<T extends HookName>(
461481
name: T,
462482
payload: HookPayloadByName<Context>[T],
483+
// biome-ignore lint/complexity/noBannedTypes: <explanation>
463484
callback: Function | null = null,
464485
): Promise<any> {
465486
const { extensions } = this.configuration;

packages/server/src/types.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ export interface onChangePayload<Context = any> {
301301
update: Uint8Array;
302302
socketId: string;
303303
transactionOrigin: unknown;
304+
connection?: Connection<Context>;
304305
}
305306

306307
export interface beforeHandleMessagePayload<Context = any> {
@@ -346,27 +347,23 @@ export interface beforeBroadcastStatelessPayload {
346347

347348
export interface onStoreDocumentPayload<Context = any> {
348349
clientsCount: number;
349-
context: Context;
350350
document: Document;
351+
lastContext: Context;
352+
lastTransactionOrigin: unknown;
351353
documentName: string;
352354
instance: Hocuspocus;
353-
requestHeaders: IncomingHttpHeaders;
354-
requestParameters: URLSearchParams;
355-
socketId: string;
356-
transactionOrigin?: unknown;
357355
}
358356

359357
// eslint-disable-next-line @typescript-eslint/no-empty-interface, @typescript-eslint/no-empty-object-type
360-
export interface afterStoreDocumentPayload<Context = any> extends onStoreDocumentPayload<Context> {}
358+
export interface afterStoreDocumentPayload<Context = any>
359+
extends onStoreDocumentPayload<Context> {}
361360

362361
export interface onAwarenessUpdatePayload<Context = any> {
363-
context: Context;
364362
document: Document;
365363
documentName: string;
366364
instance: Hocuspocus;
367-
requestHeaders: IncomingHttpHeaders;
368-
requestParameters: URLSearchParams;
369-
socketId: string;
365+
transactionOrigin: unknown;
366+
connection?: Connection<Context>;
370367
added: number[];
371368
updated: number[];
372369
removed: number[];
@@ -387,7 +384,8 @@ export interface fetchPayload<Context = any> {
387384
connectionConfig: ConnectionConfiguration;
388385
}
389386

390-
export interface storePayload<Context = any> extends onStoreDocumentPayload<Context> {
387+
export interface storePayload<Context = any>
388+
extends onStoreDocumentPayload<Context> {
391389
state: Buffer;
392390
}
393391

0 commit comments

Comments
 (0)