0.3.10 • Published 6 months ago

svelegante v0.3.10

Weekly downloads
-
License
-
Repository
-
Last release
6 months ago

Svelegante

A Classy writable store for Svelte. As with any class it can be extended.

Why?

While experimenting with Svelte I found myself oftenly extending the writable store, with the objective to keep "business logic" on a single place.

Svelegante was created as a class around the writable function, enforcing Svelte types.

Installation

npm install --save-dep svelegante

Works just like a writable store

// hello_world.js

import Store from 'svelegante';

export default new Store('Hello World');

// page.svelte

<script>
    import hello from './hello_world';
</script>

<h1>{ $hello }</h1>

It can be extended

To extend it you can simply use this.update and this.set on your customized methods, with the same syntax of the respective writable methods.

To retrieve the current value from within a customized method you can use this.current.

All this three methods are available to instances of the store, including this.current which is not reactive, it serves for the case where there is need to retrieve a value asynchronously.

// counter.js

import Store from 'svelegante';

/** @extends {Store<number>} */
class Counter extends Store {
	increment() {
		this.update((n) => n + 1);
	}

	decrement() {
		this.update((n) => n - 1);
	}
}

export default new Counter(0);

// counter.svelte

<script>
    import counter from './counter'
</script>

<button on:click={() => counter.decrement()}>-</button>
<span>{$counter}</span>
<button on:click={() => counter.increment()}>+</button>

It can be persisted

By now it can only be persisted in localStorage or sessionStorage.

To persist the store use the options parameter at the store constructor.

// counter.js

import Store from 'svelegante';

/** @extends {Store<number>} */
class Counter extends Store {
	increment() {
		this.update((n) => n + 1);
	}

	decrement() {
		this.update((n) => n - 1);
	}
}

export default new Counter(0, { storage: 'localStorage', key: 'counter', load: true });

Parameters are:

  • storage: a string "localStorage" or "sessionStorage".
  • key: the key to indentify the entry in the storage.
  • load: boolean. True will force the load of the current value in the storage into the store.
0.3.9

6 months ago

0.3.10

6 months ago

0.3.0

6 months ago

0.3.6

6 months ago

0.3.5

6 months ago

0.3.8

6 months ago

0.3.7

6 months ago

0.3.2

6 months ago

0.3.1

6 months ago

0.3.4

6 months ago

0.3.3

6 months ago

0.2.14

8 months ago

0.2.13

12 months ago

0.2.12

1 year ago

0.2.11

1 year ago

0.2.10

1 year ago

0.2.1

1 year ago

0.2.0

1 year ago

0.2.7

1 year ago

0.2.6

1 year ago

0.2.8

1 year ago

0.2.3

1 year ago

0.2.2

1 year ago

0.2.5

1 year ago

0.2.4

1 year ago

0.1.1

2 years ago

0.1.0

2 years ago