Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In case of several elements within the same widget, callbacks seem to loose specificity #95

Open
m-albert opened this issue Jul 17, 2024 · 1 comment

Comments

@m-albert
Copy link

I'm playing around with kaibu widgets in the context of a collaborative annotation tool and I'm loving it!

Also I stumbled upon what might be a bug: It seems that when having several elements as part of the same widget, elements can call the callbacks of other elements.

For example, when taking this example code from https://kaibu.org/docs/#/api?id=add_widgetoptions:

from imjoy import api

class ImJoyPlugin():
    async def setup(self):
        pass

    async def run(self, ctx):
        viewer = await api.createWindow(src="https://kaibu.org/#/app")

        async def say_hello():
            await api.alert('Hello!')

        async def select_mode(mode):
            await api.alert("Selected mode: " + mode)

        await viewer.add_widget(
            {
                "_rintf": True,
                "name": "Control",
                "type": "control",
                "elements": [
                    {
                        "type": "button",
                        "label": "Say Hello",
                        "callback": say_hello,
                    },
                    {
                        "type": "dropdown",
                        "label": "Mode",
                        "options": ["Mode A", "Mode B"],
                        "callback": select_mode,
                    },
                ],
            })

api.export(ImJoyPlugin())

and pressing the button "Say Hello", no alert is shown. Instead, the java console reports the following, which suggests that select_mode was called instead of say_hello:

Error: Error: Traceback (most recent call last):
             ^^^^^^^^^^^^^^^^^^^^^^^
TypeError: ImJoyPlugin.run.<locals>.select_mode() missing 1 required positional argument: 'mode'
    at c._decode (rpc.js:787:19)
    at c._decode (rpc.js:831:35)
    at c._unwrap (rpc.js:863:23)
    at rpc.js:260:29
    at Connection._fire (utils.js:334:11)
    at Connection.handleEvent (pluginIframe.js:64:14)

I'm getting the same problem in other contexts. A workaround that works for me consists in placing buttons in separate widgets.

Thanks!

@m-albert
Copy link
Author

m-albert commented Aug 5, 2024

Coming back to this issue, I found that assigning _rintf=False removed the specificity problem. Debugging this a bit further I found that assigning different _rintf to the different elements leads to the expected behaviour.

Maybe sth goes wrong during encoding or decoding of the remote functions, e.g. functions are serialised identically?

This plugin definition works:

from imjoy import api

class ImJoyPlugin():
    async def setup(self):
        pass

    async def run(self, ctx):
        viewer = await api.createWindow(src="https://kaibu.org/#/app")

        async def say_hello():
            await api.alert('Hello!')

        async def select_mode(mode):
            await api.alert("Selected mode: " + mode)

        await viewer.add_widget(
            {
                # "_rintf": True,
                "name": "Control",
                "type": "control",
                "elements": [
                    {
                        "type": "button",
                        "_rintf": "rintf1",
                        "label": "Say Hello",
                        "callback": say_hello,
                    },
                    {
                        "type": "dropdown",
                        "_rintf": "rintf2",
                        "label": "Mode",
                        "options": ["Mode A", "Mode B"],
                        "callback": select_mode,
                    },
                ],
            })

api.export(ImJoyPlugin())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant