Comments (5)
This is a bug in the framework - I'll update once fixed.
from seed.
This will work in the next version:
fn view(state: seed::App<Msg, Model>, model: Model) -> El<Msg> {
div![
did_mount(move |_| {
let state2 = state.clone();
let callback = move || {
state2.update(Msg::Increment);
};
seed::set_interval(Box::new(callback), 1000);
}),
button![
simple_ev("click", Msg::Increment),
format!("Hello, World × {}", model.val)
]
]
}
My intent is to make things like this easy by providing a high-level API. The raw wasm-bindgen code your sample was based on's not something I'd like anyone to have to use.
from seed.
I got this working with a refactoring of your view func:
fn view(state: seed::App<Msg, Model>, model: Model) -> El<Msg> {
div![
did_mount(move |_| {
let state2 = state.clone();
let callback = Closure::wrap(Box::new(move || {
state2.update(Msg::Increment);
}) as Box<dyn Fn()>);
let window = web_sys::window().unwrap();
window
.set_interval_with_callback_and_timeout_and_arguments_0(
// Note this method call, which uses `as_ref()` to get a `JsValue`
// from our `Closure` which is then converted to a `&Function`
// using the `JsCast::unchecked_ref` function.
callback.as_ref().unchecked_ref(),
1_000,
)
.unwrap();
callback.forget();
}),
button![
simple_ev("click", Msg::Increment),
format!("Hello, World × {}", model.val)
]
]
}
The key differences are moving the callback into the did_mount closure, using a clone of state to avoid closure/moving problems, and forgetting the callback. I'm not sure if the best approach here is to make a change in the library to facilitate doing things like this, or to update the docs.
from seed.
@David-OConnor Oh awesome! Thanks for looking into this. 👍 I was unaware of the forget()
function.
from seed.
Published in v0.2.2
from seed.
Related Issues (20)
- 1. Create a new issue in the Seed repo with the name and copy-paste this checklist into it (also add blockers and additional tasks, if exist).
- Maintenance & Future of Seed HOT 8
- v0.9.1: browser::json::Error is inaccessible
- Deserializing large numbers fail HOT 11
- Websocket example does not work HOT 2
- how to integrate with another wasm ?
- Websocket API changed from 0.8 to 0.9 HOT 5
- DOM node is not attached to ElRef under some conditions HOT 6
- Does not build with wasm-bindgen 0.2.81 HOT 3
- Closures need types HOT 3
- Release v0.9.2 HOT 3
- Seed 0.8 accepting the newest wasm-bindgen HOT 3
- [0.8.x] WebSocket and too early dropped closure HOT 1
- support `beforeinput` events HOT 1
- Seed doesn't compile with `default-features = false` HOT 2
- checkbox not working HOT 2
- Release v0.10.0 HOT 3
- Cannot use subs (subs::UrlChanged) HOT 2
- Error: unresolved import in wasm_bindgen_initialize HOT 1
- Website defaced by indonesian (?) script-kiddie HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from seed.