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

Suggestion: Provide standard implementations for persisted stores #26

Closed
lpotthast opened this issue Apr 2, 2022 · 3 comments · Fixed by #27
Closed

Suggestion: Provide standard implementations for persisted stores #26

lpotthast opened this issue Apr 2, 2022 · 3 comments · Fixed by #27
Labels
suggestion Requesting a new feature

Comments

@lpotthast
Copy link

lpotthast commented Apr 2, 2022

If we want a store with persistence, going by the current state of the Readme, we always need to implement even the (sane) default implementation that simply deserializes on load and serializes on every change ourself. Something like this:
(I changed it slightly, so that it logs an error instead of panicking. Think this is fine here. Don't know what we could otherwise do with the error..)

#[derive(Default, Clone, PartialEq, Debug, Serialize, Deserialize)]
pub struct Theme {
    pub is_dark_mode: bool,
}

impl Store for Theme {
    fn new() -> Self {
        yewdux::storage::load(yewdux::storage::Area::Local)
            .map_err(|error| error!("Unable to load state due to StorageError: {}", error))
            .ok()
            .flatten()
            .unwrap_or_default()
    }

    fn changed(&mut self) {
        if let Some(error) = yewdux::storage::save(self, yewdux::storage::Area::Local).err() {
            error!("Unable to save state due to StorageError: {}", error)
        }
    }
}

I would assume that, at many times, nothing more sophisticated is required and therefore a default (or standard) implementation for the Store trait that provides just something like this should be worth it.
Havin the Store macro already, two additional macros named LocalPersistedStore and SessionPersistedStore would do it, letting the user choose the yewdux::storage::Area being used.
With a note at the documentation/readme that anything else can and must be implemented manually.

@intendednull
Copy link
Owner

intendednull commented Apr 3, 2022

Yes definitely want an auto impl, maybe something like #[derive(Store(storage = Area::Local))]

Honestly don't have much experience with building macros so this may take some time to implement. Coming as soon as I can figure it out!

@intendednull intendednull added the suggestion Requesting a new feature label Apr 4, 2022
@intendednull
Copy link
Owner

Turns out args like that aren't possible, so maybe something like

#[derive(Store, ..)]
#[store(storage = Area::Local)]

@intendednull
Copy link
Owner

intendednull commented Apr 5, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
suggestion Requesting a new feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants