tidalcycles / strudel

Web-based environment for live coding algorithmic patterns, incorporating a faithful port of TidalCycles to JavaScript
https://strudel.cc/
GNU Affero General Public License v3.0
579 stars 105 forks source link

Use sessionStorage for viewingPatternData and activePattern #1091

Closed kasparsj closed 1 month ago

kasparsj commented 1 month ago

localStorage, which is shared between tabs, was being used for these, and although it wasn't [live] synchronised between tabs, it would e.g. restore to the last saved data (accross all tabs) upon refresh/reload/new window.

sessionStorage is separate for each tab, but persists tab data across refreshes.

this should fix #1049 as it's tracking the data about which pattern is active/selected for each tab separately.

felixroos commented 1 month ago

ah yes thanks for spotting that! one idea that might simplify the implementation: maybe it's possible to use a regular nanostores atom and update a session storage field on change (using .listen). The default value can be the session storage field.

felixroos commented 1 month ago

i meant this:

const initialActivePattern = sessionStorage.getItem('activePattern');
const $activePattern = atom('activePattern', initialActivePattern);
$activePattern.listen((value) => sessionStorage.setItem('activePattern', value));

does that make sense?

kasparsj commented 1 month ago

I see, my idea was to keep the api same as persistentAtom and base the implementation on this : https://github.com/nanostores/persistent/pull/30 (no idea why it wasn't merged)

felixroos commented 1 month ago

I see, my idea was to keep the api same as persistentAtom and base the implementation on this : https://github.com/nanostores/persistent/pull/30 (no idea why it wasn't merged)

atom is also a Store type (WritableAtom) which works for the part of the API that we use (set, get and passing it to useStore) so I don't see why we need more complexity here

kasparsj commented 1 month ago

please check now...

felixroos commented 1 month ago

thanks, this now looks good. In the future viewingPatternData might be refactored a little bit to not need parsing on every render (PatternsTab > parseJSON), but I guess that can be done in a separate PR. Also I wonder if a browser that doesn't support sessionStorage can even run the rest of strudel :P

yaxu commented 1 month ago

Is this ready to merge?

felixroos commented 1 month ago

Is this ready to merge?

yup! thanks @kasparsj