simplify more

This commit is contained in:
Kaspars 2024-05-07 15:20:59 +02:00
parent 02ea4d1116
commit 33ebf08e81

View file

@ -21,39 +21,25 @@ export const patternFilterName = {
user: 'user', user: 'user',
}; };
export let sessionAtom = persistentAtom; const sessionAtom = (name, initial = undefined) => {
const storage = typeof sessionStorage !== 'undefined' ? sessionStorage : {};
if (typeof sessionStorage !== 'undefined') { const store = atom(typeof storage[name] !== 'undefined' ? storage[name] : initial);
const identity = (a) => a;
sessionAtom = (name, initial = undefined, opts = {}) => {
let encode = opts.encode || identity;
let decode = opts.decode || identity;
if (opts.listen) console.error('sessionAtom does not support "listen" option');
let store = atom(typeof sessionStorage[name] !== 'undefined' ? decode(sessionStorage[name]) : initial);
store.listen((newValue) => { store.listen((newValue) => {
if (typeof newValue === 'undefined') { if (typeof newValue === 'undefined') {
delete sessionStorage[name]; delete storage[name];
} else { } else {
sessionStorage[name] = encode(newValue); storage[name] = newValue;
} }
}); });
return store; return store;
}; };
}
export let $viewingPatternData = sessionAtom( export let $viewingPatternData = sessionAtom('viewingPatternData', {
'viewingPatternData',
{
id: '', id: '',
code: '', code: '',
collection: collectionName.user, collection: collectionName.user,
created_at: Date.now(), created_at: Date.now(),
}, });
{ listen: false },
);
export const getViewingPatternData = () => { export const getViewingPatternData = () => {
return parseJSON($viewingPatternData.get()); return parseJSON($viewingPatternData.get());
@ -91,7 +77,7 @@ export async function loadDBPatterns() {
} }
// reason: https://github.com/tidalcycles/strudel/issues/857 // reason: https://github.com/tidalcycles/strudel/issues/857
const $activePattern = sessionAtom('activePattern', '', { listen: false }); const $activePattern = sessionAtom('activePattern', '');
export function setActivePattern(key) { export function setActivePattern(key) {
$activePattern.set(key); $activePattern.set(key);