2016-07-02 06:49:59 +08:00
|
|
|
import window from 'window';
|
2019-07-05 03:19:24 +08:00
|
|
|
import { isUnd } from 'Common/Utils';
|
|
|
|
import { isStorageSupported } from 'Storage/RainLoop';
|
|
|
|
import { CLIENT_SIDE_STORAGE_INDEX_NAME } from 'Common/Consts';
|
2015-11-15 08:23:16 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
class LocalStorageDriver {
|
2016-09-10 06:38:16 +08:00
|
|
|
s = null;
|
|
|
|
|
2016-07-16 05:29:42 +08:00
|
|
|
constructor() {
|
2016-07-08 02:53:02 +08:00
|
|
|
this.s = window.localStorage || null;
|
|
|
|
}
|
|
|
|
|
2015-11-15 08:23:16 +08:00
|
|
|
/**
|
|
|
|
* @param {string} key
|
|
|
|
* @param {*} data
|
2016-06-30 08:02:45 +08:00
|
|
|
* @returns {boolean}
|
2015-11-15 08:23:16 +08:00
|
|
|
*/
|
|
|
|
set(key, data) {
|
2019-07-05 03:19:24 +08:00
|
|
|
if (!this.s) {
|
2016-07-08 02:53:02 +08:00
|
|
|
return false;
|
|
|
|
}
|
2015-11-15 08:23:16 +08:00
|
|
|
|
2016-07-08 02:53:02 +08:00
|
|
|
let storageResult = null;
|
2019-07-05 03:19:24 +08:00
|
|
|
try {
|
2016-07-08 02:53:02 +08:00
|
|
|
const storageValue = this.s.getItem(CLIENT_SIDE_STORAGE_INDEX_NAME) || null;
|
2016-08-10 06:52:50 +08:00
|
|
|
storageResult = null === storageValue ? null : window.JSON.parse(storageValue);
|
2019-07-05 03:19:24 +08:00
|
|
|
} catch (e) {} // eslint-disable-line no-empty
|
2015-11-15 08:23:16 +08:00
|
|
|
|
|
|
|
(storageResult || (storageResult = {}))[key] = data;
|
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
try {
|
2016-08-10 06:52:50 +08:00
|
|
|
this.s.setItem(CLIENT_SIDE_STORAGE_INDEX_NAME, window.JSON.stringify(storageResult));
|
2016-07-08 02:53:02 +08:00
|
|
|
return true;
|
2019-07-05 03:19:24 +08:00
|
|
|
} catch (e) {} // eslint-disable-line no-empty
|
2015-11-15 08:23:16 +08:00
|
|
|
|
2016-07-08 02:53:02 +08:00
|
|
|
return false;
|
2015-11-15 08:23:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} key
|
2016-06-30 08:02:45 +08:00
|
|
|
* @returns {*}
|
2015-11-15 08:23:16 +08:00
|
|
|
*/
|
|
|
|
get(key) {
|
2019-07-05 03:19:24 +08:00
|
|
|
if (!this.s) {
|
2016-07-08 02:53:02 +08:00
|
|
|
return null;
|
|
|
|
}
|
2016-04-21 01:12:51 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
try {
|
|
|
|
const storageValue = this.s.getItem(CLIENT_SIDE_STORAGE_INDEX_NAME) || null,
|
2016-08-10 06:52:50 +08:00
|
|
|
storageResult = null === storageValue ? null : window.JSON.parse(storageValue);
|
2015-11-15 08:23:16 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
return storageResult && !isUnd(storageResult[key]) ? storageResult[key] : null;
|
|
|
|
} catch (e) {} // eslint-disable-line no-empty
|
2015-11-15 08:23:16 +08:00
|
|
|
|
2016-07-08 02:53:02 +08:00
|
|
|
return null;
|
2015-11-15 08:23:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-30 08:02:45 +08:00
|
|
|
* @returns {boolean}
|
2015-11-15 08:23:16 +08:00
|
|
|
*/
|
|
|
|
static supported() {
|
2016-07-08 02:53:02 +08:00
|
|
|
return isStorageSupported('localStorage');
|
2015-11-15 08:23:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
export { LocalStorageDriver, LocalStorageDriver as default };
|