@accuweather/data_manager

AccuWeather Data Manager

Usage no npm install needed!

<script type="module">
  import accuweatherDataManager from 'https://cdn.skypack.dev/@accuweather/data_manager';
</script>

README

AccuWeather Data Manager

Each website uses a DataManager to set and get cookies and data from localStorage/sessionStorage (if available).

CAUTION: DataManager internally caches and serializes/unserializes data. If you instantiate multiple DataManagers, there could be confusion if you try to retrieve data from one that was set using the other. Because of this, you should only instantiate one DataManager per site, at a high level, and pass it to other modules/functions as necessary.

Instantiate like so:

var DManagerCtor = require('@accuweather/data_manager');
var DManager = new DManagerCtor();

Uses the following methods:

saveData(name, value, expires, path, storageSystem)

name and value should be strings. CAUTION when setting/getting non-string data: behavior may be unexpected. expires is measured in days -- typically we use 365 (one non-leap year). expires defaults to 30 minutes.

path defaults to '/' and probably shouldn't be set to anything other than that. storageSystem, if given, should be either the string 'localStorage' or 'sessionStorage' (this is to ensure that it doesn't fail when undefined in browsers that don't support it).

DManager.saveData('myData', 'example', 365, '/', 'localStorage');

getData(name, storageSystem)

As above, storageSystem (if given) should be the string 'localStorage' or 'sessionStorage'.

var x = DManager.getData('myData', 'localStorage'); // "example"

To reduce the number of cookies we set, we can use "chips" -- serialized key-value pairs within a single cookie value.

saveChip(name, chip, value, expires)

name is the name of the cookie, while chip is the name of the chip within the cookie. Other parameters as above.

DManager.saveChip('foo', 'chip1', 'value1', 365);
DManager.saveChip('foo', 'chip2', 'value2', 365);

getChip(name, chip)

DManager.getChip('foo', 'chip1'); // "value1"
DManager.getChip('foo', 'chip2'); // "value2"

// serialized:
DManager.getData('foo'); // "chip1=value1&chip2=value2"