bokal-sourage

0.0.8 • Public • Published

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'

Package Sidebar

Install

npm i bokal-sourage

Weekly Downloads

1

Version

0.0.8

License

ISC

Unpacked Size

7.92 kB

Total Files

4

Last publish

Collaborators

  • ish-