Skip to content

Expanded Flux Architecture Docs #1046

Description

@Android789515

Problem

Currently the docs for flux architecture are minimal. I would say they are even incomplete.

Image

Trying to actually implement this has some ownership problems:

Dispatcher owns the state. Naturally that means the app can't own the state. But the get state method will be implemented on the state struct itself. But if the dispatcher owns the state, how do you get the state? You'd instead need to pass a reference to the state. But if you do you run into the issue of locking it if you reference mutably:

let store = Store::new();
// Need a mutable reference to state
let mut dispatcher = Dispatcher::new(&mut store);

// Cannot immutably reference if there is a mutable reference
while store.get_state().run() {
    terminal.draw(|frame| {
        let state_info = Paragraph::new(format!("{:#?}", store.get_state()));

        frame.render_widget(state_info, frame.area());
    })?;

    if crossterm::event::read()?.is_key_press() {
        dispatcher.dispatch(some_action);
    }
}

Solution

Right now the docs don't match the descriptions of the architecture. The vanilla flux architecture is very tricky to implement in Rust due to the nature of registering store callbacks (which receive the action payload and update the store's state) in the dispatcher. If I figure out a viable way of doing vanilla flux without a library I can consider contributing my approach.

Alternatives

I can use a library like state-flux-mini or redux-rs. Easiest alternative.

Manage state immutably (not required by flux) which is how Redux works. Right now the docs have state being mutated. If one does not care about recreating state each update then this will be the easiest alternative that doesn't use a library.

The dispatcher could have the get_state method instead. Although, that isn't really its job.

Wrap the store in a reference counted Mutex. This way the store will only lock once it needs to update its state.

Are you willing to contribute an implementation?

  • I am willing to open a PR implementing this.
  • I can try to implement it, but I will need guidance.
  • I am not able to implement this right now.

Additional context

I like the flux architecture from working with React. I'm currently implementing it in an app I'm working on with Ratatui. There is one app example mentioned as implementing the Flux architecture, but it doesn't really follow it. It doesn't have a dispatcher and all the update logic is in the store itself.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions