How to declare TypeScript types for service worker syntax?

-
19 Replies
ysm
ysmOP2y ago
There is no QueueEvent type on "@cloudflare/workers-types": "^4.20230321.0", any help?
Hello, I’m Allie!
IIRC, Queues aren’t compatible with Service Worker syntax
Hello, I’m Allie!
Service Worker syntax is maintained for backwards compatibility, but in general, new features/invocations will only be deployed to Module Syntax, as it is more efficient
ysm
ysmOP2y ago
In the addEventListener documentation, it says to use the QueueEvent type. Am I misunderstanding something?
Hello, I’m Allie!
Um… That may or may not be a typo? Let me check Oh, just noticed the queue section underneath
ysm
ysmOP2y ago
I used to use Module Syntax but suddenly I decided to use Service Worker Syntax because I want to use Environment Variables in globalThis scope.
Hello, I’m Allie!
Looks like the types only support Module Syntax
ysm
ysmOP2y ago
But I cannot bind any Environment variables in global scope when I'm using Module syntax
Hello, I’m Allie!
Which may or may not mean that the runtime doesn’t support them In general, we don’t recommend binding variables into the global scope(unless necessary for old dependencies), as it slows down execution And affects the reusability of an isolate
ysm
ysmOP2y ago
oh I see Thank you for quick reply
Hello, I’m Allie!
There is the AsyncLocalStorage API, which is sort of similar? In the root of your app, you place your environment variables into the storage, and then where you need it in other places, you can pull it back down
ysm
ysmOP2y ago
I cannot bind env variables to global scope when I'm using module syntax because env variables only provide by wrangler (when arrive Request) through env so on initial run (if any Request comes yet), I cannot access to env
Hello, I’m Allie!
IIRC, that’s what https://developers.cloudflare.com:2083/workers/runtime-apis/nodejs/asynclocalstorage/ is for. You push the environment into the store, and then you can pull it down somewhere else and use the bindings
AsyncLocalStorage · Cloudflare Workers docs
Cloudflare Workers provides an implemenation of a subset of the Node.js AsyncLocalStorage API for creating in-memory stores that remain coherent …
ysm
ysmOP2y ago
yea I mean.. I cannot push to store bc I cannot access env because any first Request comes yet
Hello, I’m Allie!
Oh, you mean on initialization?
ysm
ysmOP2y ago
yes I'm initialize db connection in global scope like this: the function isProd, isLocal is trying to access environment variables on first initialization
Hello, I’m Allie!
You can do that directly in the function that calls it. Basically, initialize the DB on the first request, and then leave it open
ysm
ysmOP2y ago
ok I see Thanks!
Want results from more Discord servers?
Add your server