Coder Social home page Coder Social logo

Comments (4)

surma avatar surma commented on June 11, 2024

“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.

ivancuric avatar ivancuric commented on June 11, 2024

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:

  1. Is it ok to call expose multiple times in the same worker?
  2. Is it ok to call wrap on a worker multiple times?
  3. How would I add a finalizer on a function (or object) which was declared outside expose()?
  4. The parameter for the expose() is obj: 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?
  5. Are functions only supposed to be wrapped with expose when being passed as a callback to another thread?
  6. I'm getting an error Uncaught TypeError: rawValue.apply is not a function at callback when calling an exposed function.
  7. Is objectToExpose considered an exposed object? If so, how would one run finalizers on it?
  8. 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 inside expose(), wouldn't it make more sense for a finalizer to be an optional callback argument on it?

from comlink.

surma avatar surma commented on June 11, 2024
  1. Is it ok to call expose multiple times in the same worker?

No

  1. Is it ok to call wrap on a worker multiple times?

No

  1. How would I add a finalizer on a function (or object) which was declared outside expose()?
function f() { ... }
f[Comlink.finalizer] = () => ...
Comlink.expose(f);
  1. The parameter for the expose() is obj: 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.

  1. 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().

  1. 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?

  1. 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?

  1. 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 inside expose(), 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.

ivancuric avatar ivancuric commented on June 11, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.