@libs-ui/utils 0.2.110 → 0.2.111
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cache.d.ts +1 -1
- package/esm2022/cache.mjs +58 -56
- package/fesm2022/libs-ui-utils.mjs +57 -55
- package/fesm2022/libs-ui-utils.mjs.map +1 -1
- package/package.json +1 -1
|
@@ -999,61 +999,61 @@ class UtilsCache {
|
|
|
999
999
|
static dbVersion = 1;
|
|
1000
1000
|
static db = null;
|
|
1001
1001
|
static init(config) {
|
|
1002
|
-
if (
|
|
1002
|
+
if (this.initdEvent) {
|
|
1003
1003
|
return;
|
|
1004
1004
|
}
|
|
1005
|
-
|
|
1005
|
+
this.initdEvent = true;
|
|
1006
1006
|
if (config.indexedDBName) {
|
|
1007
|
-
|
|
1007
|
+
this.dbName = config.indexedDBName;
|
|
1008
1008
|
}
|
|
1009
1009
|
if (config.typeKeyClearLocalStorage) {
|
|
1010
|
-
|
|
1010
|
+
this.typeKeyClearLocalStorage = config.typeKeyClearLocalStorage;
|
|
1011
1011
|
}
|
|
1012
1012
|
if (config.listKeyKeepWhenClearAll) {
|
|
1013
|
-
|
|
1013
|
+
this.listKeyKeepWhenClearALll = config.listKeyKeepWhenClearAll;
|
|
1014
1014
|
}
|
|
1015
1015
|
if (config.languageKeyCache) {
|
|
1016
|
-
|
|
1016
|
+
this.languageKeyCache = config.languageKeyCache;
|
|
1017
1017
|
}
|
|
1018
1018
|
}
|
|
1019
1019
|
static setLang(lang) {
|
|
1020
1020
|
if (lang !== UtilsLanguageConstants.VI && lang !== UtilsLanguageConstants.EN) {
|
|
1021
1021
|
throw Error('Language support vi | en');
|
|
1022
1022
|
}
|
|
1023
|
-
|
|
1023
|
+
this.Set(this.languageKeyCache, lang, this.CACHE_EXPIRE_NONE);
|
|
1024
1024
|
}
|
|
1025
1025
|
static getLang() {
|
|
1026
|
-
return
|
|
1026
|
+
return this.Get(this.languageKeyCache, UtilsLanguageConstants.defaultLang);
|
|
1027
1027
|
}
|
|
1028
1028
|
static openDB() {
|
|
1029
1029
|
return new Promise(resolve => {
|
|
1030
|
-
const request = indexedDB.open(
|
|
1030
|
+
const request = indexedDB.open(this.dbName, this.dbVersion);
|
|
1031
1031
|
request.onupgradeneeded = (event) => {
|
|
1032
1032
|
const db = event.target.result;
|
|
1033
|
-
if (!db.objectStoreNames.contains(
|
|
1034
|
-
const objectStore = db.createObjectStore(
|
|
1035
|
-
objectStore.createIndex(
|
|
1033
|
+
if (!db.objectStoreNames.contains(this.dbName)) {
|
|
1034
|
+
const objectStore = db.createObjectStore(this.dbName, { keyPath: this.itemIndexByKey });
|
|
1035
|
+
objectStore.createIndex(this.itemIndexByKey, this.itemIndexByKey, { unique: true });
|
|
1036
1036
|
}
|
|
1037
1037
|
};
|
|
1038
1038
|
request.onsuccess = () => {
|
|
1039
|
-
|
|
1039
|
+
this.db = request.result;
|
|
1040
1040
|
resolve(true);
|
|
1041
1041
|
};
|
|
1042
1042
|
request.onerror = (event) => {
|
|
1043
|
-
console.
|
|
1043
|
+
console.trace('Error opening IndexedDB:', event);
|
|
1044
1044
|
resolve(false);
|
|
1045
1045
|
};
|
|
1046
1046
|
});
|
|
1047
1047
|
}
|
|
1048
1048
|
static async getObjectStore() {
|
|
1049
|
-
if (!
|
|
1050
|
-
await
|
|
1049
|
+
if (!this.db) {
|
|
1050
|
+
await this.openDB();
|
|
1051
1051
|
}
|
|
1052
|
-
const transaction =
|
|
1052
|
+
const transaction = this.db?.transaction([this.dbName], 'readwrite');
|
|
1053
1053
|
if (!transaction) {
|
|
1054
1054
|
return null;
|
|
1055
1055
|
}
|
|
1056
|
-
return transaction.objectStore(
|
|
1056
|
+
return transaction.objectStore(this.dbName);
|
|
1057
1057
|
}
|
|
1058
1058
|
static get LocalStorage() {
|
|
1059
1059
|
try {
|
|
@@ -1067,12 +1067,12 @@ class UtilsCache {
|
|
|
1067
1067
|
return this.getLocalStorageFake();
|
|
1068
1068
|
}
|
|
1069
1069
|
catch (error) {
|
|
1070
|
-
console.
|
|
1070
|
+
console.trace(error);
|
|
1071
1071
|
return this.getLocalStorageFake();
|
|
1072
1072
|
}
|
|
1073
1073
|
}
|
|
1074
1074
|
static getLocalStorageFakeOnSafari() {
|
|
1075
|
-
if (typeof window.localStorage !== 'undefined' && !
|
|
1075
|
+
if (typeof window.localStorage !== 'undefined' && !this.storage && Object.keys(localStorage).length) {
|
|
1076
1076
|
this.storage = { ...localStorage };
|
|
1077
1077
|
}
|
|
1078
1078
|
return {
|
|
@@ -1102,7 +1102,7 @@ class UtilsCache {
|
|
|
1102
1102
|
};
|
|
1103
1103
|
}
|
|
1104
1104
|
static getLocalStorageFake() {
|
|
1105
|
-
if (!
|
|
1105
|
+
if (!this.storage) {
|
|
1106
1106
|
this.storage = {};
|
|
1107
1107
|
}
|
|
1108
1108
|
return {
|
|
@@ -1123,7 +1123,7 @@ class UtilsCache {
|
|
|
1123
1123
|
static async GetAsync(key, default_value, isKeyMD5 = false) {
|
|
1124
1124
|
key = isKeyMD5 ? key : md5(key);
|
|
1125
1125
|
return new Promise(async (resolve, reject) => {
|
|
1126
|
-
const objectStore = await
|
|
1126
|
+
const objectStore = await this.getObjectStore();
|
|
1127
1127
|
if (!objectStore) {
|
|
1128
1128
|
return resolve(default_value);
|
|
1129
1129
|
}
|
|
@@ -1133,7 +1133,7 @@ class UtilsCache {
|
|
|
1133
1133
|
return resolve(default_value);
|
|
1134
1134
|
}
|
|
1135
1135
|
const data = JSON.parse(decrypt(request.result.value));
|
|
1136
|
-
if (data.expire ===
|
|
1136
|
+
if (data.expire === this.CACHE_EXPIRE_NONE) {
|
|
1137
1137
|
return resolve(data.json);
|
|
1138
1138
|
}
|
|
1139
1139
|
const currentMillisecond = (new Date().valueOf() / 1000);
|
|
@@ -1157,7 +1157,7 @@ class UtilsCache {
|
|
|
1157
1157
|
}
|
|
1158
1158
|
try {
|
|
1159
1159
|
const data = JSON.parse(decrypt(cachedData));
|
|
1160
|
-
if (data.expire ===
|
|
1160
|
+
if (data.expire === this.CACHE_EXPIRE_NONE) {
|
|
1161
1161
|
return data.value ?? default_value;
|
|
1162
1162
|
}
|
|
1163
1163
|
const currentMillisecond = (new Date().valueOf() / 1000);
|
|
@@ -1167,42 +1167,44 @@ class UtilsCache {
|
|
|
1167
1167
|
return data.value;
|
|
1168
1168
|
}
|
|
1169
1169
|
catch (error) {
|
|
1170
|
-
console.
|
|
1170
|
+
console.trace(error);
|
|
1171
1171
|
return this.GetDefaultValueBySpecificKey(key, default_value);
|
|
1172
1172
|
}
|
|
1173
1173
|
}
|
|
1174
1174
|
static GetDefaultValueBySpecificKey(key, default_value) {
|
|
1175
1175
|
return default_value;
|
|
1176
1176
|
}
|
|
1177
|
-
static async SetAsync(key, value, expireTimeBySecond =
|
|
1177
|
+
static async SetAsync(key, value, expireTimeBySecond = this.CACHE_EXPIRE_TIME_DEFAULT, isKeyMD5 = false) {
|
|
1178
1178
|
return new Promise(async (resolve, reject) => {
|
|
1179
|
-
const objectStore = await
|
|
1179
|
+
const objectStore = await this.getObjectStore();
|
|
1180
1180
|
key = isKeyMD5 ? key : md5(key);
|
|
1181
1181
|
try {
|
|
1182
|
-
const currentMillisecond = expireTimeBySecond ===
|
|
1182
|
+
const currentMillisecond = expireTimeBySecond === this.CACHE_EXPIRE_NONE ? this.CACHE_EXPIRE_NONE : (new Date().valueOf() / 1000) + expireTimeBySecond;
|
|
1183
1183
|
const data = {
|
|
1184
1184
|
value: encrypt(JSON.stringify({ json: value, expire: currentMillisecond })),
|
|
1185
1185
|
};
|
|
1186
|
-
data[
|
|
1186
|
+
data[this.itemIndexByKey] = key;
|
|
1187
1187
|
if (!objectStore) {
|
|
1188
1188
|
return reject(new Error('Can not open object store'));
|
|
1189
1189
|
}
|
|
1190
1190
|
const request = objectStore?.put(data);
|
|
1191
1191
|
request.onsuccess = () => {
|
|
1192
|
+
console.trace(`Set key ${key} Success`);
|
|
1192
1193
|
resolve(request.result);
|
|
1193
1194
|
};
|
|
1194
|
-
request.onerror = () => {
|
|
1195
|
-
|
|
1195
|
+
request.onerror = (error) => {
|
|
1196
|
+
console.trace(`Set key ${key} Error`);
|
|
1197
|
+
resolve({ key, messageError: get(error, 'message') });
|
|
1196
1198
|
};
|
|
1197
1199
|
}
|
|
1198
1200
|
catch (error) {
|
|
1199
|
-
console.
|
|
1201
|
+
console.trace(`Set key ${key} Error`);
|
|
1200
1202
|
resolve({ key, messageError: get(error, 'message') });
|
|
1201
1203
|
}
|
|
1202
1204
|
});
|
|
1203
1205
|
}
|
|
1204
|
-
static Set(key, value, expireTimeBySecond =
|
|
1205
|
-
const currentMillisecond = expireTimeBySecond ===
|
|
1206
|
+
static Set(key, value, expireTimeBySecond = this.CACHE_EXPIRE_TIME_DEFAULT) {
|
|
1207
|
+
const currentMillisecond = expireTimeBySecond === this.CACHE_EXPIRE_NONE ? this.CACHE_EXPIRE_NONE : (new Date().valueOf() / 1000) + expireTimeBySecond;
|
|
1206
1208
|
const data = {
|
|
1207
1209
|
value: value,
|
|
1208
1210
|
expire: currentMillisecond
|
|
@@ -1212,13 +1214,13 @@ class UtilsCache {
|
|
|
1212
1214
|
return true;
|
|
1213
1215
|
}
|
|
1214
1216
|
catch (error) {
|
|
1215
|
-
console.
|
|
1217
|
+
console.trace(error);
|
|
1216
1218
|
return false;
|
|
1217
1219
|
}
|
|
1218
1220
|
}
|
|
1219
1221
|
static async ClearAsync(key, isMD5 = false) {
|
|
1220
1222
|
return new Promise(async (resolve) => {
|
|
1221
|
-
const objectStore = await
|
|
1223
|
+
const objectStore = await this.getObjectStore();
|
|
1222
1224
|
if (!objectStore) {
|
|
1223
1225
|
return resolve();
|
|
1224
1226
|
}
|
|
@@ -1239,17 +1241,17 @@ class UtilsCache {
|
|
|
1239
1241
|
}
|
|
1240
1242
|
static ClearAllAsync() {
|
|
1241
1243
|
return new Promise(async (resolve, reject) => {
|
|
1242
|
-
const objectStore = await
|
|
1244
|
+
const objectStore = await this.getObjectStore();
|
|
1243
1245
|
if (!objectStore) {
|
|
1244
1246
|
return resolve();
|
|
1245
1247
|
}
|
|
1246
1248
|
const request = objectStore.clear();
|
|
1247
1249
|
request.onsuccess = () => {
|
|
1248
|
-
console.
|
|
1250
|
+
console.trace('Database deleted successfully');
|
|
1249
1251
|
resolve();
|
|
1250
1252
|
};
|
|
1251
1253
|
request.onerror = (event) => {
|
|
1252
|
-
console.
|
|
1254
|
+
console.trace('Error deleting database:', event.target.error);
|
|
1253
1255
|
reject(event.target.error);
|
|
1254
1256
|
};
|
|
1255
1257
|
});
|
|
@@ -1257,22 +1259,22 @@ class UtilsCache {
|
|
|
1257
1259
|
static ClearAll() {
|
|
1258
1260
|
if (isEmbedFrame()) {
|
|
1259
1261
|
const data = {
|
|
1260
|
-
type:
|
|
1262
|
+
type: this.typeKeyClearLocalStorage,
|
|
1261
1263
|
response: {
|
|
1262
1264
|
idEvent: this.idService
|
|
1263
1265
|
}
|
|
1264
1266
|
};
|
|
1265
1267
|
UtilsCommunicateMicro.PostMessageToParent(data);
|
|
1266
1268
|
}
|
|
1267
|
-
const keys = [...
|
|
1269
|
+
const keys = [...this.listKeyKeepWhenClearALll];
|
|
1268
1270
|
Object.keys(this.LocalStorage).forEach(key => {
|
|
1269
1271
|
if (key.includes('kc-callback-')) {
|
|
1270
1272
|
keys.push(key);
|
|
1271
1273
|
}
|
|
1272
1274
|
});
|
|
1273
|
-
const stores =
|
|
1275
|
+
const stores = this.GetDataByKeys(Array.from(keys));
|
|
1274
1276
|
this.LocalStorage.clear();
|
|
1275
|
-
|
|
1277
|
+
this.SetDataByKey(stores);
|
|
1276
1278
|
}
|
|
1277
1279
|
static GetDataByKeys(keys) {
|
|
1278
1280
|
const stores = new Map();
|
|
@@ -1281,7 +1283,7 @@ class UtilsCache {
|
|
|
1281
1283
|
stores.set(key, this.LocalStorage.getItem(key));
|
|
1282
1284
|
return;
|
|
1283
1285
|
}
|
|
1284
|
-
stores.set(key,
|
|
1286
|
+
stores.set(key, this.Get(key));
|
|
1285
1287
|
});
|
|
1286
1288
|
return stores;
|
|
1287
1289
|
}
|
|
@@ -1291,15 +1293,15 @@ class UtilsCache {
|
|
|
1291
1293
|
this.LocalStorage.setItem(key, value);
|
|
1292
1294
|
return;
|
|
1293
1295
|
}
|
|
1294
|
-
if (key ===
|
|
1295
|
-
return
|
|
1296
|
+
if (key === this.languageKeyCache) {
|
|
1297
|
+
return this.setLang(value);
|
|
1296
1298
|
}
|
|
1297
|
-
|
|
1299
|
+
this.Set(key, value, this.CACHE_EXPIRE_NONE);
|
|
1298
1300
|
});
|
|
1299
1301
|
}
|
|
1300
1302
|
static DeleteKeyStartWithAsync(keyCacheStartWith, isKeyMD5 = false) {
|
|
1301
1303
|
return new Promise(async (resolve) => {
|
|
1302
|
-
const objectStore = await
|
|
1304
|
+
const objectStore = await this.getObjectStore();
|
|
1303
1305
|
if (!objectStore) {
|
|
1304
1306
|
return resolve({});
|
|
1305
1307
|
}
|
|
@@ -1312,8 +1314,8 @@ class UtilsCache {
|
|
|
1312
1314
|
return resolve({});
|
|
1313
1315
|
}
|
|
1314
1316
|
data.forEach(obj => {
|
|
1315
|
-
if (obj[
|
|
1316
|
-
|
|
1317
|
+
if (obj[this.itemIndexByKey].startsWith(keyCacheStartWith)) {
|
|
1318
|
+
this.ClearAsync(obj[this.itemIndexByKey], true);
|
|
1317
1319
|
}
|
|
1318
1320
|
});
|
|
1319
1321
|
return resolve({});
|
|
@@ -1325,7 +1327,7 @@ class UtilsCache {
|
|
|
1325
1327
|
}
|
|
1326
1328
|
static DeleteKeyStartWith(keyCache, isMD5 = false) {
|
|
1327
1329
|
keyCache = isMD5 ? md5(keyCache) : keyCache;
|
|
1328
|
-
const keys = Object.keys(
|
|
1330
|
+
const keys = Object.keys(this.LocalStorage);
|
|
1329
1331
|
if (!keys || !keys.length) {
|
|
1330
1332
|
return;
|
|
1331
1333
|
}
|
|
@@ -1337,17 +1339,17 @@ class UtilsCache {
|
|
|
1337
1339
|
}
|
|
1338
1340
|
static DeleteDatabaseIndexDB(dbName) {
|
|
1339
1341
|
return new Promise((resolve, reject) => {
|
|
1340
|
-
const request = indexedDB.deleteDatabase(dbName);
|
|
1342
|
+
const request = indexedDB.deleteDatabase(dbName || this.dbName);
|
|
1341
1343
|
request.onsuccess = () => {
|
|
1342
|
-
console.
|
|
1344
|
+
console.trace('Database deleted successfully');
|
|
1343
1345
|
resolve(true);
|
|
1344
1346
|
};
|
|
1345
1347
|
request.onerror = (event) => {
|
|
1346
|
-
console.
|
|
1348
|
+
console.trace('Error deleting database:', event.target.error);
|
|
1347
1349
|
reject(event.target.error);
|
|
1348
1350
|
};
|
|
1349
1351
|
request.onblocked = () => {
|
|
1350
|
-
console.
|
|
1352
|
+
console.trace('Delete request is blocked');
|
|
1351
1353
|
};
|
|
1352
1354
|
});
|
|
1353
1355
|
}
|