Topics for Deno KV Queues
Use Deno KV queues with topics.
This module calls kv.listenQueue
internally, so you cannot call
kv.listenQueue
yourself or use any modules that call kv.listenQueue
. It is
not recommended to use kv.enqueue
directly because this module will ignore
those items.
Quick Start
import { connectTopicQueue } from "jsr:@mieszko/topics";
const { enqueue, listenQueue } = await connectTopicQueue();
// Call listenQueue on the top level.
listenQueue("my-queue", async (data) => {
console.log(data);
});
enqueue("my-queue", { example: "Hello!" });
Advanced Usage
import { connectTopicQueue } from "jsr:@mieszko/topics";
// Connect to any KV store by passing a KV connection to connectTopicQueue.
const kv = await Deno.openKv(":memory:");
// If kv.listenQueue freezes your build process, you can disable it here.
const disableListenQueue = Deno.env.has("FRESH_IS_BUILDING");
const { enqueue, listenQueue, close } = await connectTopicQueue(
kv,
disableListenQueue,
);
type MyData = {
example: string;
};
listenQueue<MyData>(["my", "queue"], async (data) => {
console.log(data.example);
});
// You can optionally pass an atomic instance to enqueue.
const atomic = kv.atomic();
// If you call enqueue before listenQueue, an error will be thrown.
enqueue<MyData>(
["my", "queue"],
{ example: "Hello, World!" },
{ backOffSchedule: [5000, 10_000] }, // Optional
atomic, // Optional
);
await atomic.commit();
// Close the connection when you are done.
close();