Code Monkey home page Code Monkey logo

Comments (6)

nlfiedler avatar nlfiedler commented on August 19, 2024 1

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.

nlfiedler avatar nlfiedler commented on August 19, 2024

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.

brunocodutra avatar brunocodutra commented on August 19, 2024

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 the Store 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 Reducers by the way.

from reducer.

nlfiedler avatar nlfiedler commented on August 19, 2024

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.

brunocodutra avatar brunocodutra commented on August 19, 2024

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.

nlfiedler avatar nlfiedler commented on August 19, 2024

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)

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.