19 Replies
There is no
QueueEvent
type on "@cloudflare/workers-types": "^4.20230321.0",
any help?IIRC, Queues aren’t compatible with Service Worker syntax
is there any reason for that?
https://developers.cloudflare.com/workers/runtime-apis/add-event-listener/#properties
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
In the addEventListener documentation, it says to use the QueueEvent type.
Am I misunderstanding something?
Um…
That may or may not be a typo?
Let me check
Oh, just noticed the queue section underneath
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.Looks like the types only support Module Syntax
But I cannot bind any
Environment variables
in global scope when I'm using Module syntaxWhich 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
oh I see
Thank you
for quick reply
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
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
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 …
yea I mean.. I cannot push to store bc I cannot access
env
because any first Request comes yetOh, you mean on initialization?
yes
I'm initialize db connection in global scope like this:
the function
isProd
, isLocal
is trying to access environment variables
on first initializationYou can do that directly in the function that calls it. Basically, initialize the DB on the first request, and then leave it open
ok I see
Thanks!