Blazored / LocalStorage

A library to provide access to local storage in Blazor applications
https://blazored.github.io/LocalStorage/
MIT License
1.23k stars 117 forks source link

`SetItem` (async) doesn't appear to trigger `window.onstorage` #7

Closed chucker closed 5 years ago

chucker commented 5 years ago

Given a component with this:

    private bool DarkMode { get; set; }

    public async void ToggleDarkMode(UIChangeEventArgs e)
    {
        await _SetDarkMode(!DarkMode);
    }

    private async Task _SetDarkMode(bool darkMode)
    {
        DarkMode = darkMode;

        await localStorage.SetItem("DarkMode", DarkMode);

        StateHasChanged();

        Console.WriteLine($"DarkMode is now: {DarkMode}");
    }

And an index.html snippet like this:

    <script type="text/javascript">
window.onstorage = function (ev) {
    console.log(ev.key);
};
    </script>

So I'm wondering if Chrome does something else to 'commit' the value (there doesn't appear to be a function like refresh() or save()), or if this is perhaps a side effect of the code running in an async or wasm context.

chrissainty commented 5 years ago

As per the docs the onstorage event only fires when storage is updated from another document.

For example, open the app in two tabs in Chome and make a change to localstorage on tab 1, the event will fire in tab 2.

I'm going to go ahead and close this as there doesn't seem to be a problem with Blazored.LocalStorage.