Giter Site home page Giter Site logo

furudean / svelte-persistent-store Goto Github PK

View Code? Open in Web Editor NEW
35.0 2.0 1.0 104 KB

Svelte store that saves and loads data from localStorage or sessionStorage

License: MIT License

JavaScript 100.00%
localstorage sessionstorage store svelte svelte-kit

svelte-persistent-store's Introduction

A girl

This is a writable svelte store that saves and loads data from Window.localStorage or Window.sessionStorage. Works with Svelte Kit out of the box.

The store listens to events from the Storage interface and will sync its internal state upon changes. This makes debugging using the developer console easy, and it will update across sessions as well.

Install

npm install @furudean/svelte-persistent-store

Use

Note: By default only JSON serializable values are handled, but custom serialization and deserialization functions can be provided.

import { persistent } from "@furudean/svelte-persistent-store"

const preferences = persistent({
	start_value: {
		foo: "bar"
	},
	key: "preferences", // key to save as in Storage
	storage_type: "localStorage" // Storage object to use
})

Custom serialization functions

Since the Storage interface only supports strings, data needs to be converted to strings before saving. By default JSON.stringify and JSON.parse is used.

You can pass custom serializer and deserializer functions if you require specific behavior when loading or saving data from Storage. For example, you can handle Dates like this:

const persistent_date = persistent({
	start_value: new Date(),
	key: "my-persistent-date",
	storage_type: "localStorage",
	serialize: (date) => date.toISOString(), // transform before saving
	deserialize: (str) => new Date(str) // transform after loading
})

svelte-persistent-store's People

Contributors

dependabot[bot] avatar furudean avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

amerlander

svelte-persistent-store's Issues

more clear on documentation

hi thank you for the post, but it still so hard for me to understand on how to use this library, could you please updates the readme so its has more use case and on which file it should be implemented?

Usage with SvelteKit?

I'm attempting to use this in SvelteKit but getting:

TypeError: Failed to fetch dynamically imported module:

Is there any config needed for Vite/Svelte to get this working?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.