-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
159 lines (159 loc) · 5.79 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.storageKeyConfig = exports.storageConfig = exports.useLocalStorage = void 0;
const config_1 = require("./config");
const { storageConfig, storageKeyConfig } = config_1.default;
exports.storageConfig = storageConfig;
exports.storageKeyConfig = storageKeyConfig;
const react = () => {
if (!config_1.config.react) {
throw new Error('Please provide a react instance');
}
return config_1.config.react;
};
const storage = () => config_1.config.storage || window.localStorage;
const storageTrack = {};
let versionTrack;
const defaultTrackVersion = 1;
const trackKey = 'track';
const initKey = 'init';
class ReactLocalStorageKlass {
constructor(key) {
this.key = key;
this.updateState = null;
this.storageConfig = { defaults: {} };
}
init() {
const { storages } = config_1.config;
const storageConfig = storages[this.key];
if (!storageConfig) {
console.warn(`config definition for storage:${this.key} not found`);
}
if (!storageTrack[this.initKey]) {
storageTrack[this.initKey] = [];
}
const initialized = storageTrack[this.initKey].includes(this.key);
this.storageConfig = storageConfig;
const data = storage().getItem(this.keyName);
if (!data && !initialized && (storageConfig === null || storageConfig === void 0 ? void 0 : storageConfig.defaults)) {
this.save(this.keyName, storageConfig === null || storageConfig === void 0 ? void 0 : storageConfig.defaults);
this.setTrack(this.key, storageConfig === null || storageConfig === void 0 ? void 0 : storageConfig.version);
}
const stateValue = this.toState(data) || ((storageConfig === null || storageConfig === void 0 ? void 0 : storageConfig.defaults) || null);
!initialized && this.checkForMigration(stateValue);
const useState = react().useState;
const [state, updateState] = useState(stateValue);
this.updateState = updateState;
const customDispatcher = this.dispatcher();
storageTrack[this.initKey].push(this.key);
return [state, customDispatcher];
}
dispatcher() {
return {
update: this.update.bind(this),
reset: this.reset.bind(this),
remove: this.remove.bind(this)
};
}
update(data) {
var _a;
this.updateState && this.updateState(data);
this.save(this.keyName, data);
this.setTrack(this.key, (_a = this.storageConfig) === null || _a === void 0 ? void 0 : _a.version);
}
reset() {
var _a, _b;
const defaultValue = ((_a = this.storageConfig) === null || _a === void 0 ? void 0 : _a.defaults) || null;
if (!defaultValue) {
this.noDefinitionWaring();
}
this.updateState && this.updateState(defaultValue);
this.save(this.keyName, defaultValue);
this.setTrack(this.key, (_b = this.storageConfig) === null || _b === void 0 ? void 0 : _b.version);
}
remove() {
storage().removeItem(this.keyName);
this.removeTrack(this.key);
this.updateState && this.updateState(null);
}
getKeyName(key) {
const { namespace, delimiter } = config_1.config;
return namespace ? `${namespace}${delimiter || '/'}${key}` : key;
}
get keyName() {
return this.getKeyName(this.key);
}
get initKey() {
return this.getKeyName(initKey);
}
get trackKey() {
return this.getKeyName(trackKey);
}
toStorage(data) {
try {
return JSON.stringify(data);
}
catch (error) {
return data;
}
}
toState(data) {
try {
return JSON.parse(data);
}
catch (error) {
return data;
}
}
save(key, data) {
const proccessedData = this.toStorage(data);
storage().setItem(key, proccessedData);
}
checkForMigration(currentValue) {
var _a, _b, _c;
const track = this.getTrack();
if (!track[this.key]) {
this.setTrack(this.key);
}
else if (track[this.key].v !== (((_a = this.storageConfig) === null || _a === void 0 ? void 0 : _a.version) || defaultTrackVersion)) {
const migrationCallback = (_b = this.storageConfig) === null || _b === void 0 ? void 0 : _b.migration;
if (!migrationCallback) {
console.error(`Migration method not found for key:${this.key}`);
return;
}
const migratedValue = migrationCallback(currentValue, this.storageConfig.defaults);
if (!migratedValue) {
console.error('Expected return value from the callback');
}
else {
this.save(this.keyName, migratedValue);
}
this.setTrack(this.key, (_c = this.storageConfig) === null || _c === void 0 ? void 0 : _c.version);
}
}
getTrack() {
if (!versionTrack) {
const trackData = storage().getItem(this.trackKey);
return this.toState(trackData) || {};
}
return versionTrack;
}
setTrack(key, version = defaultTrackVersion) {
const track = this.getTrack();
track[this.key] = { v: version };
this.save(this.trackKey, track);
}
removeTrack(key) {
const track = this.getTrack();
delete track[key];
this.save(this.trackKey, Object.assign(track));
}
noDefinitionWaring() {
console.warn(`config definition for storage:${this.key} not found`);
}
}
const useLocalStorage = (key) => {
const reactLocalStorageKlass = new ReactLocalStorageKlass(key);
return reactLocalStorageKlass.init();
};
exports.useLocalStorage = useLocalStorage;