Skip to content

Instantly share code, notes, and snippets.

@joshnuss
Last active May 2, 2024 02:38
Show Gist options
  • Save joshnuss/aa3539daf7ca412202b4c10d543bc077 to your computer and use it in GitHub Desktop.
Save joshnuss/aa3539daf7ca412202b4c10d543bc077 to your computer and use it in GitHub Desktop.

Revisions

  1. joshnuss revised this gist Jul 15, 2020. No changes.
  2. joshnuss revised this gist Jul 3, 2020. No changes.
  3. joshnuss revised this gist Jul 3, 2020. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions localStorageStore.js
    Original 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'
  4. joshnuss revised this gist Jun 1, 2020. 1 changed file with 2 additions and 5 deletions.
    7 changes: 2 additions & 5 deletions localStorageStore.js
    Original file line number Diff line number Diff line change
    @@ -33,9 +33,7 @@ export function writable(key, initialValue) {
    }

    /*
    LICENSE
    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.
    */
    */
  5. joshnuss revised this gist Jun 1, 2020. 3 changed files with 42 additions and 34 deletions.
    33 changes: 0 additions & 33 deletions _localStorageStore.js
    Original file line number Diff line number Diff line change
    @@ -1,33 +0,0 @@
    // 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
    }
    }
    43 changes: 42 additions & 1 deletion __LICENSE.txt → localStorageStore.js
    Original 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.
    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.
  6. joshnuss renamed this gist Jun 1, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. joshnuss revised this gist Jun 1, 2020. 3 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
  8. joshnuss renamed this gist Jun 1, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  9. joshnuss renamed this gist Jun 1, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  10. joshnuss revised this gist Jun 1, 2020. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions LICENSE.txt
    Original 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.
  11. joshnuss revised this gist May 24, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion localStorageStore.js
    Original 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, update} = store
    const {subscribe, set} = store
    // get the last value from localStorage
    const json = localStorage.getItem(key)

  12. joshnuss revised this gist Feb 26, 2020. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions localStorageStore.js
    Original 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
    }
    }
  13. joshnuss revised this gist Feb 26, 2020. 1 changed file with 15 additions and 1 deletion.
    16 changes: 15 additions & 1 deletion usage.js
    Original 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
    get(preferences) // read value
    $preferences // read value with automatic subscription
  14. joshnuss revised this gist Feb 26, 2020. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions usage.js
    Original 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
  15. joshnuss revised this gist Feb 26, 2020. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions localStorageStore.js
    Original 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
    }
    }
    }
  16. joshnuss revised this gist Feb 26, 2020. 1 changed file with 8 additions and 2 deletions.
    10 changes: 8 additions & 2 deletions localStorageStore.js
    Original 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} from 'svelte/store'
    import {writable as internal, get} from 'svelte/store'

    export function writable(key, initialValue) {
    const {subscribe, set} = internal(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
    }
    }
  17. joshnuss revised this gist Feb 26, 2020. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions localStorageStore.js
    Original 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, value) {
    const {subscribe, set} = internal(value)
    export function writable(key, initialValue) {
    const {subscribe, set} = internal(initialValue)
    const json = localStorage.getItem(key)

    if (json) {
    set(JSON.parse(json))
    }

    return {
    set(val) {
    localStorage.setItem(key, JSON.stringify(val))
    set(val)
    set(value) {
    localStorage.setItem(key, JSON.stringify(value))
    set(value)
    },
    subscribe
    }
  18. joshnuss revised this gist Feb 26, 2020. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion localStorageStore.js
    Original 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) {
  19. joshnuss revised this gist Feb 26, 2020. 1 changed file with 15 additions and 15 deletions.
    30 changes: 15 additions & 15 deletions localStorageStore.js
    Original 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
    }
    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
    }
    }
  20. joshnuss created this gist Feb 26, 2020.
    21 changes: 21 additions & 0 deletions localStorageStore.js
    Original 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
    }
    }