-
Notifications
You must be signed in to change notification settings - Fork 145
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
[Help] How to maintain state in Gloo? #99
Comments
I haven't used gloo in a while, but would an AtomicUsize work?
…On Thu, Nov 28, 2019, 18:57 robert-snakard ***@***.***> wrote:
Hey all, if there's a better place to ask my question please redirect me.
How do I maintain state in Gloo? eg
fn main() {
let mut count = 0;
Interval::new(1000, || {
console.log_1(format!("{} seconds have passed"), count);
count += 1;
});
}
This doesn't work because the interval outlives the main function. A
global variable doesn't work because 'static muts are unsafe. I end up
with some complicated static Arc<Mutex<u32>> that I really don't want. Is
there a better way to maintain state?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#99?email_source=notifications&email_token=AAYD2MY5RDS4AA2BE5AF2F3QWBLH7A5CNFSM4JSZOCGKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4H4YXNTA>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAYD2M2D7HUSQVZVLLEEGODQWBLH7ANCNFSM4JSZOCGA>
.
|
It would if this was all I was doing. I have a State struct however and the usize was just used to make a simple example. I guess I'm looking for some way to block on Interval until I receive a signal? So to expand on my example:
I'm imagining the |
@robert-snakard For your original code, you can just use a For state in general, you would use For your specific use case, it sounds like what you want is a Stream, which gloo has native support for: use gloo_timers::future::IntervalStream;
use futures::stream::StreamExt;
use wasm_bindgen_futures::spawn_local;
fn main() {
spawn_local(async {
let mut count = 0;
IntervalStream::new(1000).take_while(move |_| {
count += 1;
return count < 60;
}).for_each(|value| {
// Put your code here...
}).await;
});
} If you just want to wait for a certain number of seconds, you should use use gloo_timers::future::TimeoutFuture;
use wasm_bindgen_futures::spawn_local;
fn main() {
spawn_local(async {
TimeoutFuture::new(60_000).await;
});
} The only problem is that gloo currently supports the old style of Streams, but there is a PR for upgrading to the new style. |
Awesome, thanks a lot @Pauan |
@Pauan I'm having troubles running your Complete code: extern crate gloo;
extern crate wasm_bindgen;
use gloo::timers::future::TimeoutFuture;
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::spawn_local;
#[wasm_bindgen]
pub fn main() {
spawn_local(async {
TimeoutFuture::new(100).await;
});
} And this is what I get in console:
|
Okay, I've found a workaround: #96 (comment). Related to rustwasm/wasm-bindgen#1046. |
@heilhead I'm not getting that error. What browser are you running the code in? |
@Pauan Chrome@78. But as pointed out somewhere, the error occurs only if you build your code with webpack (in my case it's webpack + @wasm-tool/wasm-pack-plugin). I've ended up with something like this as a workaround in my fork (to work both in window/webpack, and web worker contexts): https://github.com/heilhead/gloo/blob/future-factories/crates/timers/src/sys.rs#L35-L70 |
@heilhead Okay, I was able to reproduce it with Webpack. It seems to be some sort of bug with Webpack. I'll fix it by using the |
@heilhead It should be fixed in version |
Hey all, if there's a better place to ask my question please redirect me. How do I maintain state in Gloo? eg
This doesn't work because the interval outlives the main function. A global variable doesn't work because
'static mut
s are unsafe. I end up with some complicatedstatic Arc<Mutex<u32>>
that I really don't want. Is there a better way to maintain state?The text was updated successfully, but these errors were encountered: