• Stars
    star
    239
  • Rank 168,240 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 3 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Svelte store that keep its value through pages and reloads

Svelte Persistent store

A Svelte store that keep its value through pages and reloads

Github CI GitHub Repo stars NPM bundle size Download per week License NPM version Snyk Vulnerabilities for npm package

Installation

npm install @macfja/svelte-persistent-store

Usage

import { persist, createLocalStorage } from "@macfja/svelte-persistent-store"
import { writable } from "svelte/store"

let name = persist(writable("John"), createLocalStorage(), "name")

$name = "Jeanne Doe"

// if you reload the page the value of $name is 'Jeanne Doe'
import { persistBrowserSession } from "@macfja/svelte-persistent-store"
import { writable } from "svelte/store"

let title = persistBrowserSession(writable("Unsaved"), "document-name")

$title = "My Document"

// if you reload the page the value of $title is 'My Document'
import { writable } from "@macfja/svelte-persistent-store"

// Create a wriatble store, persisted in browser LocalStorage, with the key `name`
let name = writable("name", "John")

$name = "Jeanne Doe"

// if you reload the page the value of $name is 'Jeanne Doe'

Features

  • Multiple storages (Allow to have the best suited usage depending on your use case)
  • Work with any Svelte store
  • Work with classes, objects, primitive

Storages

There are 6 storages built-in:

  • createLocalStorage(), that use window.localStorage to save values
  • createSessionStorage(), that use window.sessionStorage to save values
  • createCookieStorage(), that use document.cookie to save values
  • createIndexedDBStorage(), that use window.indexedDB to save value
  • createChromeStorage(), that use chrome.storage to save values
  • createEncryptedStorage(), that wrap a storage to encrypt data (and key)

You can add more storages, you just need to implement the interface StorageInterface

Documentation

Documentation and examples can be generated with npm run doc, next open docs/index.html with your favorite web browser.

(Hint: If you don't want to generate the docs, a part of the example and documentation are available here)

Types

The persist function will return a new Store with type PersistentStore<T>.

The full signature of persist is:

declare function persist<T>(store: Writable<T>, storage: StorageInterface<T>, key: string): PersistentStore<T>

The persist function add a delete function on the store.

More information about types can be found in the generated types/index.d.ts (npm run prebuild) or in the generated documentation (npm run doc).

Backwards Compatibility Break

1.3.0 to 2.0.0

Data persisted in version 1.3.0 may not be deserializable with version 2.*.

If you have persisted store that contains Javascript class with version 1.3.0 of @macfja/svelte-persistent-store you will not be able to get the data by default. This is due to a change of data serialization. More information here

Contributing

Contributions are welcome. Please open up an issue or create PR if you would like to help out.

Read more in the Contributing file

License

The MIT License (MIT). Please see License File for more information.

More Repositories

1

PharBuilder

Create Phar of Composer based PHP application
PHP
122
star
2

php-redisearch

PHP Client for RediSearch
PHP
66
star
3

svelte-undoable

Memento design pattern in Svelte
TypeScript
45
star
4

svelte-oauth2

OAuth2 authorization for Svelte
TypeScript
39
star
5

OBD2

OBD2 Object interface for JAVA
Java
31
star
6

svelte-adapter-multi

A Svelte adapter that launch other adapters
JavaScript
16
star
7

svelte-adapter-neutralino

A SvelteKit adapter to generate a Neutralinojs application
JavaScript
16
star
8

enyo2-lib

JavaScript
15
star
9

svelte-p5

Small wrapper around P5 for Svelte
Svelte
10
star
10

Symfony-Console-Filechooser

Filechooser Helper for Symfony console (with autocompletion support)
PHP
9
star
11

sveltekit-session

Session management for SvelteKit
TypeScript
8
star
12

browser-storage-store

Set of Store that can use your browser Storage API to persist data.
JavaScript
4
star
13

svelte-expirable

A Svelte store with items that expire
TypeScript
4
star
14

svelte-invalidable

A Svelte that can be requested to update itself
TypeScript
4
star
15

Doctrine-TestSet

PHP
3
star
16

js-serializer

Transform any object, class, array, primitive to a serialized string and vice-versa
TypeScript
3
star
17

phpqa-extensions

More tools for PHPQA
XSLT
3
star
18

svelte-scroll-video

A component to play a video by scrolling the page
Svelte
3
star
19

livres

Keep track of your books
PHP
3
star
20

MpvService

Set of classes to manipulate Mpv media player
Java
3
star
21

sveltekit-cas

CAS/SSO integration in SvelteKit
TypeScript
2
star
22

ComposerClassRewrite

Rewrite any class of a composer based project
PHP
2
star
23

ChainConfig

A configuration loader, where order matters.
PHP
2
star
24

tracy-doctrine-sql

A Tracy panel for Doctrine SQL queries
HTML
2
star
25

value-provider

Get/Set object value with its getter/setter/property/metadata
PHP
2
star
26

phptransformer-twital

Twital support for PHPTransformers
PHP
2
star
27

OBD2Service

A Service for the OBD2 library
Java
2
star
28

BookRetriever

Library to retrieve data about books
PHP
2
star
29

enyo2.tmbundle

A TextMate bundle for Enyo 2
1
star
30

polyfill-registry

List of all (most of) polyfills for PHP
PHP
1
star
31

PhpKVO

KVO (Key Value Observing) design pattern in PHP.
PHP
1
star
32

Math

A set of tools around Mathematics.
PHP
1
star
33

CitiesBorder

Extract cities border from OpenStreetMap
Java
1
star
34

validator

Validator is a simple way to validate a property or an object.
PHP
1
star
35

Injector

Classes injector. Injection can be done in constructors, setters, properties and methods
Java
1
star