Skip to content
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

Refine global state API + TodoMVC #36

Merged
merged 21 commits into from
Aug 22, 2022
Merged

Conversation

JunichiSugiura
Copy link
Member

@JunichiSugiura JunichiSugiura commented Aug 15, 2022

close #31
ref #38

New API

This PR introduces global_state attribute macro. It replaces GlobalStateand GlobalStatePlugin derive macro.

fn main() {
    App::new()
        .add_plugin(DioxusPlugin::<GlobalState, (), ()>::new(Root))
        .add_plugin(GlobalStatePlugin)
        .add_startup_system(update_todo_list)
        .run();
}

#[global_state]
struct GlobalState {
    todo_list: Vec<UiTodo>,
    // more state if you like...
}

fn update_todo_list(
    mut events: EventReader<UpdateTodoList>,
    mut global_state: EventWriter<GlobalState>,

) {
    for e in events.iter() {
        let todo_list = vec![UiTodo::new()];
        
        // ↓ global_state macro automagically turns GlobalState struct into enum
        global_state.send(GlobalState::TodoList(todo_list));
    }
}

Checkout todomvc example for more.

cargo run --example todomvc

# check what's generated
cargo install expand # if not installed
cargo expand --example todomvc

Todo

  • Fix other examples
  • create_todo
  • remove_todo
  • toggle_done
  • change_title
  • toggle_all
  • change_filter (All | Active | Completed)
  • clear_completed

Not Todo

  • styling
  • save in localStorage

Sorry, something went wrong.

@vercel
Copy link

vercel bot commented Aug 15, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
bevy-dioxus ✅ Ready (Inspect) Visit Preview Aug 22, 2022 at 10:52AM (UTC)

@JunichiSugiura JunichiSugiura marked this pull request as draft August 15, 2022 20:19
@JunichiSugiura JunichiSugiura changed the title Refine global state Refine global state API Aug 16, 2022
@JunichiSugiura JunichiSugiura marked this pull request as ready for review August 22, 2022 10:53
@JunichiSugiura JunichiSugiura changed the title Refine global state API Refine global state API + TodoMVC Aug 22, 2022
@JunichiSugiura JunichiSugiura merged commit 8b76a03 into main Aug 22, 2022
@JunichiSugiura JunichiSugiura deleted the feat/#31-refine-global-state branch August 22, 2022 11:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refine global state API
1 participant