Last active
May 30, 2019 09:05
-
-
Save mgutz/89c0e1b1a447232192783e57ed5c59fc to your computer and use it in GitHub Desktop.
Revisions
-
mgutz revised this gist
May 30, 2019 . 1 changed file with 10 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,18 +2,18 @@ import {readable, writable} from 'svelte/store'; // could be its own module const util = { _root: null, // similar to createSelector but instead returns a reactive variable createGetter(store, fn, initialData) { return readable(initialData, set => { store.subscribe(state => set(fn(state, this._root))); }); }, // set the root store setRoot(root) { this._root = root; }, }; @@ -22,7 +22,7 @@ const util = { //---------------------------------------------------------------------------- const initialState = { selected: 'green', colors: { red: '#f00', green: '#0f0', -
mgutz revised this gist
May 30, 2019 . 1 changed file with 22 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,10 +4,10 @@ import {readable, writable} from 'svelte/store'; const util = { // similar to createSelector but instead returns a reactive variable createGetter(store, fn) { const w = writable(); store.subscribe(state => { // can take advantage of memoization here w.update(() => { return fn(state); }); @@ -17,8 +17,10 @@ const util = { }, }; //---------------------------------------------------------------------------- // State //---------------------------------------------------------------------------- const initialState = { selected: 'red', colors: { @@ -34,16 +36,30 @@ const store = writable(initialState); // Actions //---------------------------------------------------------------------------- // writable publishes updates so no need to be immutable export const add = (k, v) => store.update(state => { state.colors[k] = v; return state; }); export const setSelected = k => store.update(state => { state.selected = k; return state; }); //---------------------------------------------------------------------------- // Getters //---------------------------------------------------------------------------- // usage: {#each $colorsGetter as [k, v]} ... {/each} export const colorsGetter = util.createGetter(store, state => { const result = []; for (const k in state.colors) { // skip red to show filtering if (k === 'red') continue; result.push([k, state.colors[k]]); } return result; -
mgutz revised this gist
May 30, 2019 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,7 @@ import {readable, writable} from 'svelte/store'; const util = { // similar to createSelector but instead returns a reactive variable createGetter(store, fn) { let w = writable(); store.subscribe(state => { // can memoize here @@ -41,7 +41,6 @@ export const setSelected = k => store.update(state => ({...state, selected: k})) // Getters //---------------------------------------------------------------------------- export const colorsGetter = util.createGetter(store, state => { const result = []; for (const k in state.colors) { -
mgutz created this gist
May 30, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,53 @@ import {readable, writable} from 'svelte/store'; // could be its own module const util = { // similar to createSelector but instead returns a reactive variable createGetter(store, fn) { let w = writable(fn(initialState)); store.subscribe(state => { // can memoize here w.update(() => { return fn(state); }); }); return w; }, }; // selected should be its own store but the purpose of this example is to show // how to deal with nested objects const initialState = { selected: 'red', colors: { red: '#f00', green: '#0f0', blue: '#00f', }, }; const store = writable(initialState); //---------------------------------------------------------------------------- // Actions //---------------------------------------------------------------------------- export const add = (k, v) => store.update(state => ({...state, colors: {...state.colors, [k]: v}})); export const setSelected = k => store.update(state => ({...state, selected: k})); //---------------------------------------------------------------------------- // Getters //---------------------------------------------------------------------------- // usage: {#each $colorsGetter as [k, v]}{/each} export const colorsGetter = util.createGetter(store, state => { const result = []; for (const k in state.colors) { result.push([k, state.colors[k]]); } return result; }); export default store;