Files
outline/server/queues/processors/debouncer.ts
Tom Moor 15b1069bcc chore: Move to Typescript (#2783)
This PR moves the entire project to Typescript. Due to the ~1000 ignores this will lead to a messy codebase for a while, but the churn is worth it – all of those ignore comments are places that were never type-safe previously.

closes #1282
2021-11-29 06:40:55 -08:00

39 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { Document } from "@server/models";
import { globalEventQueue } from "../../queues";
import { Event } from "../../types";
export default class DebounceProcessor {
async on(event: Event) {
switch (event.name) {
case "documents.update": {
globalEventQueue.add(
{ ...event, name: "documents.update.delayed" },
{
delay: 5 * 60 * 1000,
}
);
break;
}
case "documents.update.delayed": {
const document = await Document.findByPk(event.documentId, {
fields: ["updatedAt"],
});
// If the document has been deleted then prevent further processing
if (!document) return;
// If the document has been updated since we initially queued the delayed
// event then abort, there must be another updated event in the queue
// this functions as a simple distributed debounce.
if (document.updatedAt > new Date(event.createdAt)) return;
globalEventQueue.add({ ...event, name: "documents.update.debounced" });
break;
}
default:
}
}
}