ataractic
ataractic•17mo ago

Getting values from Deno KV in a Web Worker always return empty array despite working correctly o...

Getting values from Deno KV in a Web Worker always return empty array despite working correctly outside the worker context (in the parent app). What can cause this behavior?
No description
14 Replies
fro.profesional
fro.profesional•17mo ago
Hmmm its reading from same db right? 🤔
ataractic
ataracticOP•17mo ago
yes
Fifth-Normal-Form
Fifth-Normal-Form•17mo ago
What does getAllJobs() look like?
ataractic
ataracticOP•17mo ago
export async function getAllJobs() {
return await getValues<Job>({ prefix: ["jobs"] });
}
export async function getAllJobs() {
return await getValues<Job>({ prefix: ["jobs"] });
}
Fifth-Normal-Form
Fifth-Normal-Form•17mo ago
I don't think I can help. I don't know what getValues<job> does. I assume at some point it's calling kv.list? Is the code in a public repo? Can we have a look?
ataractic
ataracticOP•17mo ago
yes sorry, here is getValues()
async function getValues<T>(
selector: Deno.KvListSelector,
options?: Deno.KvListOptions,
) {
const values = [];
const iter = kv.list<T>(selector, options);
for await (const { value } of iter) values.push(value);
return values;
}
async function getValues<T>(
selector: Deno.KvListSelector,
options?: Deno.KvListOptions,
) {
const values = [];
const iter = kv.list<T>(selector, options);
for await (const { value } of iter) values.push(value);
return values;
}
i use a lot of code from saaskit to help me
Fifth-Normal-Form
Fifth-Normal-Form•17mo ago
I have no experience with saaskit; sorry. Hopefully someone with experience will jump in to help you.
ataractic
ataracticOP•17mo ago
thanks you for trying :)
cknight
cknight•17mo ago
Have you tried Deno.openKv() in your worker?
fro.profesional
fro.profesional•16mo ago
That will create another db
iuioiua
iuioiua•16mo ago
Sidenote: I'd actually recommend instead using collectValues(). It allows you to use iter.cursor once the values are collected.
ataractic
ataracticOP•16mo ago
i will do it once it can at least work, thanks for the tip! i went with another approach, using message events to send job data both ways between the main thread to the worker, and it works! however i'm pretty sure it impacts performance pretty badly as compared to the first idea.
iuioiua
iuioiua•16mo ago
Actually, check SaaSKit out once v1 is released. We’ll have some noteworthy bits of the KV implementation worth looking at 👀
ataractic
ataracticOP•16mo ago
sure!

Did you find this page helpful?