Code Monkey home page Code Monkey logo

Comments (7)

daniele-zurico avatar daniele-zurico commented on September 26, 2024

What I would to achieve is:

  • the iframe do login and set the token
  • the parent observe for the message received and get it.

In other word what I would avoid to do is:

public ngAfterViewInit(): void {
        this.main().then((ifr: any) => {
            setInterval(() => {
                const api = Comlink.proxy(ifr.contentWindow);
                (api as any).CommunicationChannel.getInstance().then((_instance) => {
                    _instance.getUserToken().then(res => console.log(res));
                });
            }, 1000);
        });
    }

    private main(): Promise<any> {
        const ifr = document.querySelector('iframe');
        return new Promise((resolve) => ifr.onload = () => {
            resolve(ifr);
        });
    }

from comlink.

surma avatar surma commented on September 26, 2024

Ah, good catch. I was aware but forgot to document it in the README.

As of now, function parameters need to be either structurally cloneable or transferable. Functions are neither. Therefore your callback won’t work. I am working on a good solution, but it’s not possible as of now.

(If you wanna hack for a bit: You could use Comlink in both directions. Expose your callback to the iframe and hook it up to the Observable.

from comlink.

surma avatar surma commented on September 26, 2024

I updated the README. Thanks for giving Comlink a spin! @daniele-zurico. I’ll get to this issue pretty quickly I reckon – it’s a central use-case.

I want to ask one thing of you, tho: Could you reduce your test cases in the future? I really can’t do much with snippets that use Angular and potentially other libraries or frameworks. The fewer the dependencies and SLOCs, the better :)

from comlink.

daniele-zurico avatar daniele-zurico commented on September 26, 2024

Oh sure.
I was trying to put entire snippet to make my code as much as possible easy to understand for you showing the part involved.
My plan is to create a post to show how to use this library and honestly I’m almost done with the code.
I just need to find the time to write the post and I think that will be useful for everyone because honestly I think that in the doc is not so easy to understand ( maybe is just me ).
However if you prefer, when the post is ready, you can link to it

from comlink.

surma avatar surma commented on September 26, 2024

This has been fixed in 2191276 (included in Comlink v2.1.0)

You only need to make sure you wrap your callbacks in proxyValue.

Just keep in mind that these callbacks will end up being asynchronous functions on the other end, I so I am not sure if RxJS will handle this correctly by default. Enjoy!

from comlink.

adrianromanko avatar adrianromanko commented on September 26, 2024

Running some tests with comlink...I tried passing an Observable object to the child iframe and then in the child I subscribed to that observable. Calling .subscribe(...) produced an error.

Parent:
await instance.proxyValues(Comlink.proxyValue({
preferences$: this._getGlobalPreferencesObservable()
}));
Child iFrame:
await preferences$.subscribe(v => ... );

Error:
image

from comlink.

surma avatar surma commented on September 26, 2024

Your callback needs to be wrapped into proxyValue.

await preferences$.subscribe(Comlink.proxyValue(v => ... ));

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.