Comments (6)
I've put this fix to good use (copying the state when I need it rather than on every update). Thanks for taking the time to explain, and for addressing this request.
from reducer.
Sorry, just realized I used Subscription
without any context. I got that from redux-rs, it's the function signature that takes a reference to the State
.
from reducer.
The reason why Store
doesn't provide direct access to the inner state at the moment, is because I felt it would encourage anti-patterns that deviate from a truly reactive design, but I'd love to be given a counter example, would you mind sharing more details about your use-case?
Better would be a
get_state(callback: Subscription<State>)
function on theStore
so I don't have to copy the state.
Wouldn't it be sufficient for Store<State, Reactor>
to expose a get_state(&self) -> &State
method? Better yet, we could consider implementing Deref<Target = State> for Store<State, Reactor>
so that &Store
may seamlessly be used where &State
is expected.
It would also be splendid if I could register more than one reactor. For now I'm maintaining my own collection of listeners.
This should be possible out-of-the-box, tuples, arrays and slices of reactors are themselves reactors.
The same is true for Reducer
s by the way.
from reducer.
Certainly, a simplified get_state()
would be fine. As for a use-case, in the event of my application, there is a background thread that's generating state changes. At some point the front-end will send a query to display the current state. I don't want to wait for a reactor update if the background job takes a while to emit an action.
Thanks for the tip on the reactors and reducers, I'll look into using that strategy.
Update: now I remember why I wanted the callback to get the state: I'm using lazy_static
and a Mutex
to hold the application state. I don't think I can return a reference to the state to the caller when there's a mutex involved, but then again my Rust skills are not great.
from reducer.
Sorry for the late response, I've somehow missed your update.
now I remember why I wanted the callback to get the state: I'm using lazy_static and a Mutex to hold the application state. I don't think I can return a reference to the state to the caller when there's a mutex involved, but then again my Rust skills are not great.
This shouldn't be of any concern, it is always be possible to write a method that gets a &'a S
out of &'a Store<S, R>
, regardless of what S
is.
For example, If S
is MyState(Mutex<SomeData>)
, you'll get a &MyState
back. You're then free to lock the mutex to get access to SomeData
if you like.
Does that make sense?
from reducer.
Ah, right, thanks for pointing that out. It adds a step for the caller, but it's an alternative to copying, which is what I'm doing now.
from reducer.
Related Issues (16)
- Error building on macOS 10.14.2 HOT 2
- RUSTSEC-2020-0095: difference is unmaintained HOT 2
- RUSTSEC-2020-0077: memmap is unmaintained HOT 3
- RUSTSEC-2020-0016: `net2` crate has been deprecated; use `socket2` instead HOT 3
- RUSTSEC-2020-0020: `stb_truetype` crate has been deprecated; use `ttf-parser` instead HOT 3
- RUSTSEC-2021-0119: Out-of-bounds write in nix::unistd::getgrouplist HOT 1
- RUSTSEC-2021-0119: Out-of-bounds write in nix::unistd::getgrouplist HOT 1
- Flux pattern link is outdated HOT 1
- RUSTSEC-2022-0048: xml-rs is Unmaintained HOT 1
- RUSTSEC-2021-0140: rusttype is Unmaintained HOT 1
- RUSTSEC-2021-0140: rusttype is Unmaintained HOT 1
- RUSTSEC-2021-0140: rusttype is Unmaintained HOT 1
- Conrod example broken on macOS HOT 1
- Action composition? HOT 3
- Consider making this project no_std + alloc 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 reducer.