From fd9d6baea311d9b227b130749647a86838ba2ccc Mon Sep 17 00:00:00 2001 From: Igor Zinkovsky Date: Tue, 13 Jun 2023 17:49:57 -0700 Subject: feat(kv) queue implementation (#19459) Extend the unstable `Deno.Kv` API to support queues. --- cli/tsc/dts/lib.deno.unstable.d.ts | 59 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) (limited to 'cli/tsc') diff --git a/cli/tsc/dts/lib.deno.unstable.d.ts b/cli/tsc/dts/lib.deno.unstable.d.ts index 27d3af4cd..8f11adfff 100644 --- a/cli/tsc/dts/lib.deno.unstable.d.ts +++ b/cli/tsc/dts/lib.deno.unstable.d.ts @@ -1914,6 +1914,14 @@ declare namespace Deno { * checks pass during the commit. */ delete(key: KvKey): this; + /** + * Add to the operation a mutation that enqueues a value into the queue + * if all checks pass during the commit. + */ + enqueue( + value: unknown, + options?: { delay?: number; keysIfUndelivered?: Deno.KvKey[] }, + ): this; /** * Commit the operation to the KV store. Returns a value indicating whether * checks passed and mutations were performed. If the operation failed @@ -2087,6 +2095,57 @@ declare namespace Deno { options?: KvListOptions, ): KvListIterator; + /** + * Add a value into the database queue to be delivered to the queue + * listener via {@linkcode Deno.Kv.listenQueue}. + * + * ```ts + * const db = await Deno.openKv(); + * await db.enqueue("bar"); + * ``` + * + * The `delay` option can be used to specify the delay (in milliseconds) + * of the value delivery. The default delay is 0, which means immediate + * delivery. + * + * ```ts + * const db = await Deno.openKv(); + * await db.enqueue("bar", { delay: 60000 }); + * ``` + * + * The `keysIfUndelivered` option can be used to specify the keys to + * be set if the value is not successfully delivered to the queue + * listener after several attempts. The values are set to the value of + * the queued message. + * + * ```ts + * const db = await Deno.openKv(); + * await db.enqueue("bar", { keysIfUndelivered: [["foo", "bar"]] }); + * ``` + */ + enqueue( + value: unknown, + options?: { delay?: number; keysIfUndelivered?: Deno.KvKey[] }, + ): Promise; + + /** + * Listen for queue values to be delivered from the database queue, which + * were enqueued with {@linkcode Deno.Kv.enqueue}. The provided handler + * callback is invoked on every dequeued value. A failed callback + * invocation is automatically retried multiple times until it succeeds + * or until the maximum number of retries is reached. + * + * ```ts + * const db = await Deno.openKv(); + * db.listenQueue(async (msg: unknown) => { + * await db.set(["foo"], msg); + * }); + * ``` + */ + listenQueue( + handler: (value: unknown) => Promise | void, + ): Promise; + /** * Create a new {@linkcode Deno.AtomicOperation} object which can be used to * perform an atomic transaction on the database. This does not perform any -- cgit v1.2.3