0.0.8 • Published 7 months ago

bokal-sourage v0.0.8

Weekly downloads
4
License
ISC
Repository
github
Last release
7 months ago

bokalSourage · npm version

It's localStorage manager with JSON parse/stringify serialization.
get/set/watch/wipe values, subscribe/publish messages in scoped namespace from all windows.

Usage

import BokalSourage from 'bokal-sourage';

const bs = new BokalSourage('prefix__'); // could leave prefix empty

const jsonSerializable = { // String, Number, null, Object, Array
  name: 'Flying Microtonal Banana',
  tracks: ['Billabong Valley'],
};

bs.set('album', jsonSerializable);
bs.get('album'); // => JSON.parse(JSON.stringify(jsonSerializable))

const onRateChange = (rate) => console.log(`Rate is changed: ${ rate }`);
const unwatch = bs.watch('rate', onRateChange);
bs.set('rate', 10); // any window
// 'Rate is changed: 10'

unwatch();
// or
bs.unwatch('rate', onRateChange);

bs.remove('something');

// clear keys in prefixed scope
bs.wipe(); // => [ ...wipedKeys ]

// publish() and subscribe() use watch/unwatch functionality above the `message:${ key }` storage keys
const onEggBoiled = dur => console.log(`Egg was boiled for ${ dur / 6e4 }m`);
bs.subscribe('egg:boiled', onEggBoiled)
bs.publish('egg:boiled', 18e4); // any window
// 'Egg was boiled for 3m'

// ref() creates reference to the specified key with getter and setter
const ref = bs.ref('USE_ME');
ref.set('like this');
ref.get(); // 'like this'

// proxy() creates Proxy object reflecting all bokalSourage keys
const proxy = bs.proxy();
proxy.HELLO = 'world';
proxy.HELLO // 'world'
0.0.8

7 months ago

0.0.7

7 months ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.6

2 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago