-
Notifications
You must be signed in to change notification settings - Fork 111
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
Unneeded RwLock #1
Comments
Yep, it's kind of "cache" here, I'll see RefCell later. |
@hgzimmerman It seems that lazy_static does not support RefCell due to thread safety:
|
Look into https://doc.rust-lang.org/std/macro.thread_local.html instead |
@hgzimmerman I tried thread_local! {
pub static TOKEN: RefCell<Option<String>> = RefCell::new(None);
} of course I have to move the initialization of TOKEN to App component's create. But I'm thinking that yew supports multi-threading & concurrency, it's totally possible that we might need to use TOKEN in web workers someday or other multi-threading senarios, so I think we'd better use lazy_static for now, and leave it as a demonstration for multi-threading support. |
or a ok, a related issue: yewstack/yew#576 |
Leave it alone for multi-threading demonstration, closing this |
Is your feature request related to a problem? Please describe.
I think there is a RwLock that isn't needed here: https://github.com/jetli/rust-yew-realworld-example-app/blob/master/crates/conduit-wasm/src/agent.rs#L23
Describe the solution you'd like
I think you could use a RefCell instead.
Additional context
Because your app is only running in a single "thread", there isn't a reason to use an RwLock.
It appears that you are only using it to enable interior mutability, which can be provided by a RefCell instead.
In fact you don't need to keep the token in memory and instead just get/set the token directly via the bowser's storage API, but I understand that you could want to avoid that in order to improve performance by a small amount.
The text was updated successfully, but these errors were encountered: