Last active
May 2, 2024 02:38
-
-
Save joshnuss/aa3539daf7ca412202b4c10d543bc077 to your computer and use it in GitHub Desktop.
Revisions
-
joshnuss revised this gist
Jul 15, 2020 . No changes.There are no files selected for viewing
-
joshnuss revised this gist
Jul 3, 2020 . No changes.There are no files selected for viewing
-
joshnuss revised this gist
Jul 3, 2020 . 1 changed file with 8 additions and 0 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 @@ -1,3 +1,11 @@ /* It's now a package. You can find it here: https://github.com/joshnuss/svelte-local-storage-store */ // Svelte store backed by window.localStorage // Persists store's data locally import {writable as internal, get} from 'svelte/store' -
joshnuss revised this gist
Jun 1, 2020 . 1 changed file with 2 additions and 5 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 @@ -33,9 +33,7 @@ export function writable(key, initialValue) { } /* LICENSE: Copyright 2020 Joshua Nussbaum @@ -44,5 +42,4 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -
joshnuss revised this gist
Jun 1, 2020 . 3 changed files with 42 additions and 34 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 @@ -1,33 +0,0 @@ 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 @@ -1,7 +1,48 @@ // Svelte store backed by window.localStorage // Persists store's data locally import {writable as internal, get} from 'svelte/store' // wraps a regular writable store export function writable(key, initialValue) { // create an underlying store const store = internal(initialValue) const {subscribe, set} = store // get the last value from localStorage const json = localStorage.getItem(key) // use the value from localStorage if it exists if (json) { set(JSON.parse(json)) } // return an object with the same interface as svelte's writable() return { // capture set and write to localStorage set(value) { localStorage.setItem(key, JSON.stringify(value)) set(value) }, // capture updates and write to localStore update(cb) { const value = cb(get(store)) this.set(value) }, // punt subscriptions to underlying store subscribe } } /* LICENSE Copyright 2020 Joshua Nussbaum Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ File renamed without changes. -
joshnuss renamed this gist
Jun 1, 2020 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
joshnuss revised this gist
Jun 1, 2020 . 3 changed files with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes.File renamed without changes.File renamed without changes. -
joshnuss renamed this gist
Jun 1, 2020 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
joshnuss renamed this gist
Jun 1, 2020 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
joshnuss revised this gist
Jun 1, 2020 . 1 changed file with 7 additions and 0 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 @@ -0,0 +1,7 @@ Copyright 2020 Joshua Nussbaum Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -
joshnuss revised this gist
May 24, 2020 . 1 changed file with 1 addition and 1 deletion.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 @@ -6,7 +6,7 @@ import {writable as internal, get} from 'svelte/store' export function writable(key, initialValue) { // create an underlying store const store = internal(initialValue) const {subscribe, set} = store // get the last value from localStorage const json = localStorage.getItem(key) -
joshnuss revised this gist
Feb 26, 2020 . 1 changed file with 8 additions and 0 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,24 +2,32 @@ // Persists store's data locally import {writable as internal, get} from 'svelte/store' // wraps a regular writable store export function writable(key, initialValue) { // create an underlying store const store = internal(initialValue) const {subscribe, set, update} = store // get the last value from localStorage const json = localStorage.getItem(key) // use the value from localStorage if it exists if (json) { set(JSON.parse(json)) } // return an object with the same interface as svelte's writable() return { // capture set and write to localStorage set(value) { localStorage.setItem(key, JSON.stringify(value)) set(value) }, // capture updates and write to localStore update(cb) { const value = cb(get(store)) this.set(value) }, // punt subscriptions to underlying store subscribe } } -
joshnuss revised this gist
Feb 26, 2020 . 1 changed file with 15 additions and 1 deletion.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 @@ -1,7 +1,21 @@ // define your stores import { writable } from './localStorageStore' // `preferences` will be: // - Synced with localStorage // - Accessible via pub/sub export const preferences = writable('preferences', { theme: 'dark', pane: '50%', ... }) // then when you want to use the store import { get } from 'svelte/store' import { preferences } from './stores' preferences.subscribe(...) // subscribe to changes preferences.update(...) // update value preferences.set(...) // set value get(preferences) // read value $preferences // read value with automatic subscription -
joshnuss revised this gist
Feb 26, 2020 . 1 changed file with 7 additions and 0 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 @@ -0,0 +1,7 @@ import { get } from 'svelte/store' import { preferences } from './stores' preferences.subscribe(...) // subscribe to changes preferences.update(...) // update value preferences.set(...) // set value get(preferences) // read value -
joshnuss revised this gist
Feb 26, 2020 . 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 @@ -16,11 +16,10 @@ export function writable(key, initialValue) { localStorage.setItem(key, JSON.stringify(value)) set(value) }, update(cb) { const value = cb(get(store)) this.set(value) }, subscribe } } -
joshnuss revised this gist
Feb 26, 2020 . 1 changed file with 8 additions 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 @@ -1,9 +1,10 @@ // Svelte store backed by window.localStorage // Persists store's data locally import {writable as internal, get} from 'svelte/store' export function writable(key, initialValue) { const store = internal(initialValue) const {subscribe, set, update} = store const json = localStorage.getItem(key) if (json) { @@ -15,6 +16,11 @@ export function writable(key, initialValue) { localStorage.setItem(key, JSON.stringify(value)) set(value) }, update(cb) { const value = cb(get(store)) this.set(value) }, subscribe } } -
joshnuss revised this gist
Feb 26, 2020 . 1 changed file with 5 additions and 5 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 @@ // Persists store's data locally import {writable as internal} from 'svelte/store' export function writable(key, initialValue) { const {subscribe, set} = internal(initialValue) const json = localStorage.getItem(key) if (json) { set(JSON.parse(json)) } return { set(value) { localStorage.setItem(key, JSON.stringify(value)) set(value) }, subscribe } -
joshnuss revised this gist
Feb 26, 2020 . 1 changed file with 0 additions and 1 deletion.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,6 @@ import {writable as internal} from 'svelte/store' export function writable(key, value) { const {subscribe, set} = internal(value) const json = localStorage.getItem(key) if (json) { -
joshnuss revised this gist
Feb 26, 2020 . 1 changed file with 15 additions and 15 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 @@ -3,19 +3,19 @@ import {writable as internal} from 'svelte/store' export function writable(key, value) { const {subscribe, set} = internal(value) const json = localStorage.getItem(key) if (json) { set(JSON.parse(json)) } return { set(val) { localStorage.setItem(key, JSON.stringify(val)) set(val) }, subscribe } } -
joshnuss created this gist
Feb 26, 2020 .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,21 @@ // Svelte store backed by window.localStorage // Persists store's data locally import {writable as internal} from 'svelte/store' export function writable(key, value) { const {subscribe, set} = internal(value) const json = localStorage.getItem(key) if (json) { set(JSON.parse(json)) } return { set(val) { localStorage.setItem(key, JSON.stringify(val)) set(val) }, subscribe } }