diff options
author | Luca Casonato <hello@lcas.dev> | 2023-12-05 14:21:46 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-05 21:21:46 +0800 |
commit | 74e39a927c63e789fec1c8f1817812920079229d (patch) | |
tree | fa38e32c700865b25710f491d551086733d58d5f /cli/tests | |
parent | a24d3e8763bc48b69936db9231efb76766914303 (diff) |
feat(unstable): kv.watch() (#21147)
This commit adds support for a new `kv.watch()` method that allows
watching for changes to a key-value pair. This is useful for cases
where you want to be notified when a key-value pair changes, but
don't want to have to poll for changes.
---------
Co-authored-by: losfair <zhy20000919@hotmail.com>
Diffstat (limited to 'cli/tests')
-rw-r--r-- | cli/tests/unit/kv_test.ts | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/cli/tests/unit/kv_test.ts b/cli/tests/unit/kv_test.ts index 73c85dd5c..68b3c4013 100644 --- a/cli/tests/unit/kv_test.ts +++ b/cli/tests/unit/kv_test.ts @@ -2137,3 +2137,47 @@ Deno.test( // calling [Symbol.dispose] after manual close is a no-op }, ); + +dbTest("key watch", async (db) => { + const changeHistory: Deno.KvEntryMaybe<number>[] = []; + const watcher: ReadableStream<Deno.KvEntryMaybe<number>[]> = db.watch< + number[] + >([["key"]]); + + const reader = watcher.getReader(); + const expectedChanges = 2; + + const work = (async () => { + for (let i = 0; i < expectedChanges; i++) { + const message = await reader.read(); + if (message.done) { + throw new Error("Unexpected end of stream"); + } + changeHistory.push(message.value[0]); + } + + await reader.cancel(); + })(); + + while (changeHistory.length !== 1) { + await sleep(100); + } + assertEquals(changeHistory[0], { + key: ["key"], + value: null, + versionstamp: null, + }); + + const { versionstamp } = await db.set(["key"], 1); + while (changeHistory.length as number !== 2) { + await sleep(100); + } + assertEquals(changeHistory[1], { + key: ["key"], + value: 1, + versionstamp, + }); + + await work; + await reader.cancel(); +}); |