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

Is it possible to sync the frontend model changes with the model in the Deno kernel? #582

Open
gergelyszerovay opened this issue May 17, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@gergelyszerovay
Copy link

Hi,

I tried the following, and expected that the model in Deno will be updated and the change:value event handler will be called:

image

I ran the first code cell, then clicked on the button a few times, then ran the second code cell.

If I execute model.set('value', 13); from a code cell, the event handler is triggered:

image

Is there a way to subscribe to the frontend model changes from the code in the Deno kernel? Thanks.


Full source code:

import { widget } from "jsr:@anywidget/deno";

let model = await widget({
    state: { value: 0 },
    imports: `
import React from "https://esm.sh/react@18";
import ReactDOM from "https://esm.sh/react-dom@18";

import { createRender, useModelState } from "https://esm.sh/@anywidget/react";
`,
    render: ({ model, el }) => {
        function Counter() {
        	let [value, setValue] = useModelState("value");
        	return <button onClick={() => setValue(value + 1)}>count is {value}</button>;
        }

        return createRender(Counter)({model, el})
	}
});

let v;
model.on("change:value", (x) => {
    console.log(x)
    v = x.detail;
})

model

// ---

console.log(model);
console.log(v);
@manzt
Copy link
Owner

manzt commented May 19, 2024

Thanks for opening the issue! I believe that Deno's jupyter integration only supports broadcasting messages to the frontend (via Deno.jupyter.broadcast) at the moment, but unfortunately not receiving messages. If this API were to be added we could support the bidirectional communication in jsr:@anywidget/deno.

cc: @rgbkrk

@rgbkrk
Copy link
Collaborator

rgbkrk commented May 19, 2024

Once denoland/deno#23826 is in, it will be pretty smooth to work on the comms receiver.

@manzt
Copy link
Owner

manzt commented May 19, 2024

slick!!! thanks for all your work on runtimelib

@manzt manzt added the enhancement New feature or request label Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants