cloudcmd/client/dom/storage.js
2017-04-14 15:29:00 +03:00

84 lines
1.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const itype = require('itype/legacy');
const jonny = require('jonny');
const exec = require('execon');
/* приватный переключатель возможности работы с кэшем */
let Allowed;
/**
* allow Storage usage
*/
module.exports.setAllowed = (isAllowed) => {
Allowed = isAllowed;
};
/** remove element */
module.exports.remove = (item, callback) => {
if (Allowed)
localStorage.removeItem(item);
exec(callback, null, Allowed);
return module.exports;
};
module.exports.removeMatch = (string, callback) => {
const reg = RegExp('^' + string + '.*$');
const test = (a) => reg.test(a);
const remove = (a) => localStorage.removeItem(a);
Object.keys(localStorage)
.filter(test)
.forEach(remove);
exec(callback);
return module.exports;
};
/** если доступен localStorage и
* в нём есть нужная нам директория -
* записываем данные в него
*/
module.exports.set = (name, data, callback) => {
let str, error;
if (itype.object(data))
str = jonny.stringify(data);
if (Allowed && name)
error = exec.try(() => {
localStorage.setItem(name, str || data);
});
exec(callback, error);
return module.exports;
},
/** Если доступен Storage принимаем из него данные*/
module.exports.get = (name, callback) => {
let ret;
if (Allowed)
ret = localStorage.getItem(name);
exec(callback, null, ret);
return module.exports;
},
/** функция чистит весь кэш для всех каталогов*/
module.exports.clear = (callback) => {
const ret = Allowed;
if (ret)
localStorage.clear();
exec(callback, null, ret);
return module.exports;
};