Giter Club home page Giter Club logo

Comments (7)

AlexErrant avatar AlexErrant commented on September 23, 2024 1

Ahh, words are hard.

console.log is one type of "global" that's on window or webworker's self. In other words, you can call console.log anywhere. It is global by default.

The solid-js store you're talking about is global once you export or import it. That's actually a significant difference, since that export means it isn't global until you make it global. It is not global by default.

Also, now that I think about it, I don't think option 3 would work because the override wouldn't be scoped to the web-worker. Any console.log overrides would only apply to the console.log in user-land, not in web-worker-land.


I made a small demo of option 2 here. Note how JS now owns that global mutable state for storing the user's callback function

from fsrs-browser.

ishiko732 avatar ishiko732 commented on September 23, 2024

Sorry, I'm not very familiar with Rust. need @AlexErrant to take a look and see if it can be implemented.🥲🥲🥲

from fsrs-browser.

asukaminato0721 avatar asukaminato0721 commented on September 23, 2024

progress bar itself is simple

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/progress

<label for="train">Train progress:</label>

<progress id="train" max="100" value="70">70%</progress>

so the main point is get value for progress. Maybe need to write another wasm_bindgen function to get the value, then bind the value.

from fsrs-browser.

AlexErrant avatar AlexErrant commented on September 23, 2024

Unfortunately, I'm not seeing any obviously good ways of implementing this. I tried/thought of a few ways:

  1. Passing a Javascript function to Rust as a callback to put into a MetricsRenderer impl. Sadly, this means passing a closure, not a function pointer. This results in the following error message, which I'm unable to resolve:
Diagnostics:
1. `(dyn Fn(i32) + 'static)` cannot be shared between threads safely
   the trait `std::marker::Sync` is not implemented for `(dyn Fn(i32) + 'static)`
   required for `std::ptr::Unique<(dyn Fn(i32) + 'static)>` to implement `std::marker::Sync` [E0277]
2. `(dyn Fn(i32) + 'static)` cannot be sent between threads safely
   the trait `Send` is not implemented for `(dyn Fn(i32) + 'static)`
   required for `std::ptr::Unique<(dyn Fn(i32) + 'static)>` to implement `Send` [E0277]

I cannot pass a js_sys::Function directly either because it's not send/sync. Trying to call the function in a rayon spawned thread results in the same type of error. I then tried making the function global by putting it in a OnceLock, but again that errors. I don't think anything involving js_sys::Function is viable - though I really want it it to be. I also tried web_sys::MessagePort but it's also not send/sync.

  1. Notably this call using extern "C" works. But it only works for Javascript that fsrs-browser defines. Ideally, it would work with user-defined Javascript, since everyone will want to update their UI differently. It only works for fsrs-browser defined JS because of how wasm-bindgen handles JS snippets. JS snippets are copied to the package in a hashed manner, for example fsrs-browser/pkg/snippets/fsrs-browser-0a7bb84009d44696/js/foo.js. This hash means there's no stable path for users to use to set any callback functions.

A workaround would be passing the function to Rust, which would then set that callback function in some JS snippet for extern C to use. This... feels like quite a bad hack, since JS now owns that global mutable state for storing the user's callback function.

  1. Since calls to console.log work, users could override the console.log implementation in JS. Obviously this is a terrible solution.

I'd love to somehow make 1. work, but I'm running into the limits of my Rust skills. Are there any other things I could try? std::sync::mpsc also has the same limitations.

from fsrs-browser.

asukaminato0721 avatar asukaminato0721 commented on September 23, 2024

A naive thought, what if change console.log to setValue, since

calls to console.log work

somethings like

import { render } from "solid-js/web";
import { createSignal } from "solid-js";

function Counter() {
  const [count, setCount] = createSignal(0);

  setInterval(() => setCount(count() + 1), 1000); // setCount change to setProgress

  return <div>Count: {count()}</div>;
}

render(() => <Counter />, document.getElementById('app'));

https://www.solidjs.com/tutorial/introduction_signals?solved

from fsrs-browser.

AlexErrant avatar AlexErrant commented on September 23, 2024

The reason console.log works is because it's in the global scope. Unfortunately setCount is not in global scope.

I ended up going with another solution that uses (abuses?) shared memory. See the above PR for details.

from fsrs-browser.

asukaminato0721 avatar asukaminato0721 commented on September 23, 2024

setCount is not in global scope.

I don't know if it works.

For example, we can build a global reactive data store by creating a signal in a global scope, and exporting it for other modules to use:

https://www.solidjs.com/tutorial/stores_nocontext

from fsrs-browser.

Related Issues (10)

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.