Comments (4)
“exposed object” is the object that you pass to expose()
. So if you do this:
Comlink.expose({
a: 1,
b: "lol",
[Comlink.finalizer]() {
console.log("Finalized");
}
});
the finalizer will be called when the proxy on the other side is garbage collected or released.
from comlink.
I've never tried defining it inside expose
.
If the types are structured in such a way that it only recognizes it inside expose()
's parameters, that means you can't construct an object (or function!) on its own which you'll later expose with a finalizer, without TS complaining.
In this kinda contrived example I've tried exposing a single function and a nested object and multiple questions came up:
- Is it ok to call
expose
multiple times in the same worker? - Is it ok to call
wrap
on a worker multiple times? - How would I add a finalizer on a function (or object) which was declared outside
expose()
? - The parameter for the
expose()
isobj: any
kinda implies that you can call it on objects, but the docs show otherwise, when using it for wrapping callbacks. While functions are technically objects in JS, do you think this could be communicated somehow better in JSdocs/naming? - Are functions only supposed to be wrapped with
expose
when being passed as a callback to another thread? - I'm getting an error
Uncaught TypeError: rawValue.apply is not a function at callback
when calling an exposed function. - Is
objectToExpose
considered an exposed object? If so, how would one run finalizers on it? - Is there any use for
Comlink.finalizer
being exported? If its only use case is internal logic and the only place in which it could be applied is insideexpose()
, wouldn't it make more sense for a finalizer to be an optional callback argument on it?
from comlink.
- Is it ok to call
expose
multiple times in the same worker?
No
- Is it ok to call
wrap
on a worker multiple times?
No
- How would I add a finalizer on a function (or object) which was declared outside
expose()
?
function f() { ... }
f[Comlink.finalizer] = () => ...
Comlink.expose(f);
- The parameter for the
expose()
isobj: any
kinda implies that you can call it on objects, but the docs show otherwise, when using it for wrapping callbacks. While functions are technically objects in JS, do you think this could be communicated somehow better in JSdocs/naming?
No, any
is literally anything, not just Objects. And that’s true, Comlink — for the must part — doesn’t care what value you are trying to expose.
- Are functions only supposed to be wrapped with
expose
when being passed as a callback to another thread?
expose()
is for when you want to expose a value to an endpoint. If you want to pass a callback, you have to use proxy()
.
- I'm getting an error
Uncaught TypeError: rawValue.apply is not a function at callback
when calling an exposed function.
Can you make a small repro case and file a bug?
- Is
objectToExpose
considered an exposed object? If so, how would one run finalizers on it?
Just like I showed in my previous comment. Or am I misunderstanding you?
- Is there any use for
Comlink.finalizer
being exported? If its only use case is internal logic and the only place in which it could be applied is insideexpose()
, wouldn't it make more sense for a finalizer to be an optional callback argument on it?
The finalizer isn’t exposed. I used a Symbol()
for that reason. It is inaccessible from any other realm. An option would work too, but I think the finalizer is part of the object/type, rather than an option.
from comlink.
Ooof, seems that the formatting/reply got a bit jumbled.
For 1. and 2. – this should be mentioned in the docs, and a mechanism should be implemented that would warn the user if they somehow manage to use expose
or wrap
multiple times on the same worker.
For 6. – the repro is here. The issue is that I used expose
to expose a function, and even using expose
twice in the same file. It actually worked but it errors as well.
For 8. — having an option on expose
which would set a symbol if needed would probably make it a lot easier to work with TypeScript, as it's currently bugging out since expose
is basically just a function that triggers a side effect. There is no return value, type assertion or anything else that the user is able to debug, forcing you to declare it inside expose
.
It's also not clear if the finalizer can be mutated after being passed to expose
.
from comlink.
Related Issues (20)
- How to pass an object with both methods and properties as a parameter to a worker? HOT 1
- Finalizers, proxy and two-way communication
- Calling `expose` in an async scope? HOT 3
- "Proxy" is a loaded term in the docs. Should be named `Remote` when referring to symbol methods HOT 1
- Possible to get reference to original object from a proxy? HOT 2
- Proxy not inferred as a promise when it's a nested inside a proxied object as a property value HOT 4
- Web Worker Hangs if Imported File Contains Top-Level `Await` HOT 6
- Service worker example: stops working when SW is suspended HOT 9
- Set operation is not awaitable HOT 2
- Possible to communicate between web workers? HOT 1
- How to transfer result buffers HOT 2
- [feat] DeasyncEndpoint HOT 2
- Move from Karma to Playwright? HOT 1
- Do I need to use transfer inside a proxy? HOT 1
- Add support for async transferHandle serializer/deserializer HOT 2
- Significant performance optimizations possible in `requestResponseMessage` HOT 4
- Push notifications from web worker HOT 5
- Memory leak when terminating worker with ongoing call HOT 2
- Worker Pool for similar tasks HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from comlink.