Skip to content

Commit 6ab3de9

Browse files
committed
Make storeDocumentHooks throw errors instead of silently ignoring them.
Make debounce function asynchronous and improve usage Refactor the debounce function to return a Promise, enabling better handling in async workflows. Adjust related code to properly await hooks execution and error catching. Simplify logic in storeDocumentHooks for improved readability and maintainability.
1 parent 3c101fb commit 6ab3de9

2 files changed

Lines changed: 16 additions & 26 deletions

File tree

packages/server/src/Hocuspocus.ts

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -527,31 +527,17 @@ export class Hocuspocus {
527527
storeDocumentHooks(document: Document, hookPayload: onStoreDocumentPayload, immediately?: boolean) {
528528
return this.debouncer.debounce(
529529
`onStoreDocument-${document.name}`,
530-
() => {
531-
return this.hooks('onStoreDocument', hookPayload)
532-
.then(() => {
533-
this.hooks('afterStoreDocument', hookPayload).then(async () => {
534-
// Remove document from memory.
535-
536-
if (document.getConnectionsCount() > 0) {
537-
return
538-
}
539-
540-
await this.unloadDocument(document)
541-
})
542-
})
543-
.catch(error => {
544-
console.error('Caught error during storeDocumentHooks', error)
530+
async () => {
531+
await this.hooks('onStoreDocument', hookPayload)
532+
await this.hooks('afterStoreDocument', hookPayload)
545533

546-
if (error?.message) {
547-
throw error
548-
}
549-
})
534+
if (document.getConnectionsCount() === 0) {
535+
await this.unloadDocument(document)
536+
}
550537
},
551538
immediately ? 0 : this.configuration.debounce,
552539
this.configuration.maxDebounce,
553540
)
554-
555541
}
556542

557543
/**

packages/server/src/util/debounce.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export const useDebounce = () => {
55
func: Function
66
}> = new Map()
77

8-
const debounce = (
8+
const debounce = async (
99
id: string,
1010
func: Function,
1111
debounce: number,
@@ -31,14 +31,18 @@ export const useDebounce = () => {
3131
return run()
3232
}
3333

34-
timers.set(id, {
35-
start,
36-
timeout: setTimeout(run, debounce),
37-
func: run,
34+
return new Promise((resolve, reject) => {
35+
const runResolveReject = () => run().then(resolve).catch(reject)
36+
37+
timers.set(id, {
38+
start,
39+
timeout: setTimeout(runResolveReject, debounce),
40+
func: () => runResolveReject,
41+
})
3842
})
3943
}
4044

41-
const executeNow = (id: string) => {
45+
const executeNow = async (id: string) => {
4246
const old = timers.get(id)
4347
if (old) {
4448
clearTimeout(old.timeout)

0 commit comments

Comments
 (0)