Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/chainlist/svelte-syncable

Syncable store for Svelte
https://github.com/chainlist/svelte-syncable

svelte svelte-framework svelte-js svelte-v3 svelte3 sveltejs

Last synced: 4 months ago
JSON representation

Syncable store for Svelte

Awesome Lists containing this project

README

        

# Svelte Store Storage plugin [![Build Status](https://travis-ci.org/chainlist/svelte-syncable.svg?branch=master)](https://travis-ci.org/chainlist/svelte-syncable)

## Install

`npm i -D svelte-syncable`

or

`yarn add -D svelte-syncable`

## How to use

Setting a prefix is optional, the default one is `svelteStore`

Create your syncable store value
```javascript
// store.js
import { syncable, setPrefix } from 'svelte-syncable';

// All the syncable value will be stored as "foorbar-{name}"
setPrefix('foobar');

export const answer = syncable('answer', 42);
export const todos = syncable('todos', [
{ id: 1, text: 'Find the answer of life', done: false }
]);
```

Use your store value as every other svelte store observables
```html
// Component.svelte

import { answer, todos } from './store.js';


    {#each $todos as todo, todo.id}
  • {todo.text}

  • {/each}

The count is {$answer}
```

## API

### setPrefix(prefix: string): void

Set the localStorage prefix to {name}
The function has to be called on the top of your store.js file.

### syncable(name: string, value: any, hydrate: boolean = true): SvelteObservable

Create a svelte observable store value and synchronize it with the localStorage.
The value will by hydrated from the localStorage by default, set `hydrate` to false to avoid this behavior.
The `hydrate` parameter is optional and set to true by default.

```javascript
import { syncable } from 'svelte-syncable';
import { writable } from 'svelte/store';

export const count = syncable('count', 0, false);
```