@shival99/z-ui 1.2.17 → 1.2.19

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.
@@ -924,6 +924,8 @@ const Z_INDEXDB_DEFAULT_CONFIG = {
924
924
  defaultStore: 'ZStore',
925
925
  };
926
926
  const Z_INDEXDB_BATCH_SIZE = 100;
927
+ const Z_INDEXDB_MAX_VERSION = 1000;
928
+
927
929
  class ZIndexDbService {
928
930
  _db = null;
929
931
  _dbName;
@@ -978,7 +980,6 @@ class ZIndexDbService {
978
980
  }
979
981
  }
980
982
  async _reconnect() {
981
- console.warn('[ZIndexDb] Reconnecting...');
982
983
  this._closeConnection();
983
984
  this._dbReady = this._initDb();
984
985
  await this._dbReady;
@@ -1003,11 +1004,25 @@ class ZIndexDbService {
1003
1004
  if (this._db) {
1004
1005
  return;
1005
1006
  }
1007
+ const currentVersion = await this._getCurrentDbVersion();
1008
+ if (currentVersion >= Z_INDEXDB_MAX_VERSION) {
1009
+ await this._resetDatabase();
1010
+ this._version = 1;
1011
+ }
1012
+ else if (currentVersion > this._version) {
1013
+ this._version = currentVersion;
1014
+ }
1006
1015
  return new Promise((resolve, reject) => {
1007
1016
  try {
1008
1017
  const request = indexedDB.open(this._dbName, this._version);
1009
1018
  request.onupgradeneeded = event => {
1010
1019
  const db = event.target.result;
1020
+ const existingStores = Array.from(db.objectStoreNames);
1021
+ existingStores.forEach(storeName => {
1022
+ if (!this._stores.has(storeName)) {
1023
+ db.deleteObjectStore(storeName);
1024
+ }
1025
+ });
1011
1026
  this._stores.forEach((_, storeName) => {
1012
1027
  if (!db.objectStoreNames.contains(storeName)) {
1013
1028
  db.createObjectStore(storeName);
@@ -1016,11 +1031,16 @@ class ZIndexDbService {
1016
1031
  };
1017
1032
  request.onsuccess = event => {
1018
1033
  this._db = event.target.result;
1034
+ this._version = this._db.version;
1019
1035
  this._db.onerror = e => console.error('[ZIndexDb] Error:', e.target.error);
1020
1036
  this._db.onclose = () => {
1021
- console.warn('[ZIndexDb] Connection closed unexpectedly');
1022
1037
  this._db = null;
1023
1038
  };
1039
+ const missingStores = this._getMissingStores();
1040
+ if (missingStores.length > 0) {
1041
+ this._triggerUpgrade().then(resolve).catch(reject);
1042
+ return;
1043
+ }
1024
1044
  resolve();
1025
1045
  };
1026
1046
  request.onerror = event => {
@@ -1033,12 +1053,57 @@ class ZIndexDbService {
1033
1053
  }
1034
1054
  });
1035
1055
  }
1036
- /**
1037
- * Get a value from the store
1038
- * @param key - The key to retrieve
1039
- * @param defaultValue - Default value if key not found
1040
- * @param storeName - Optional store name (uses default if not specified)
1041
- */
1056
+ _getCurrentDbVersion() {
1057
+ return new Promise(resolve => {
1058
+ const request = indexedDB.open(this._dbName);
1059
+ request.onsuccess = event => {
1060
+ const db = event.target.result;
1061
+ const { version } = db;
1062
+ db.close();
1063
+ resolve(version);
1064
+ };
1065
+ request.onerror = () => {
1066
+ resolve(1);
1067
+ };
1068
+ });
1069
+ }
1070
+ async _triggerUpgrade() {
1071
+ this._closeConnection();
1072
+ if (this._version >= Z_INDEXDB_MAX_VERSION) {
1073
+ await this._resetDatabase();
1074
+ this._version = 1;
1075
+ }
1076
+ else {
1077
+ this._version += 1;
1078
+ }
1079
+ this._dbReady = this._initDb();
1080
+ await this._dbReady;
1081
+ }
1082
+ async _resetDatabase() {
1083
+ this._closeConnection();
1084
+ return new Promise((resolve, reject) => {
1085
+ const deleteRequest = indexedDB.deleteDatabase(this._dbName);
1086
+ deleteRequest.onsuccess = () => resolve();
1087
+ deleteRequest.onerror = () => reject(deleteRequest.error);
1088
+ deleteRequest.onblocked = () => {
1089
+ setTimeout(() => this._resetDatabase().then(resolve).catch(reject), 100);
1090
+ };
1091
+ });
1092
+ }
1093
+ _getMissingStores() {
1094
+ if (!this._db) {
1095
+ return [];
1096
+ }
1097
+ const existingStores = Array.from(this._db.objectStoreNames);
1098
+ const requiredStores = Array.from(this._stores.keys());
1099
+ return requiredStores.filter(store => !existingStores.includes(store));
1100
+ }
1101
+ _ensureStoreExists(storeName) {
1102
+ if (!this._db) {
1103
+ return false;
1104
+ }
1105
+ return this._db.objectStoreNames.contains(storeName);
1106
+ }
1042
1107
  async get(key, defaultValue, storeName) {
1043
1108
  const storeConfig = this._getStoreConfig(storeName);
1044
1109
  return this._withRetry(async () => {
@@ -1049,6 +1114,12 @@ class ZIndexDbService {
1049
1114
  return defaultValue;
1050
1115
  }
1051
1116
  }
1117
+ if (!this._ensureStoreExists(storeConfig.name)) {
1118
+ await this._triggerUpgrade();
1119
+ if (!this._ensureStoreExists(storeConfig.name)) {
1120
+ return defaultValue;
1121
+ }
1122
+ }
1052
1123
  const _key = storeConfig.encrypt !== false ? this._encrypt(key) : key;
1053
1124
  return new Promise(resolve => {
1054
1125
  try {
@@ -1078,12 +1149,6 @@ class ZIndexDbService {
1078
1149
  });
1079
1150
  });
1080
1151
  }
1081
- /**
1082
- * Set a value in the store
1083
- * @param key - The key to set
1084
- * @param value - The value to store
1085
- * @param storeName - Optional store name
1086
- */
1087
1152
  async set(key, value, storeName) {
1088
1153
  const storeConfig = this._getStoreConfig(storeName);
1089
1154
  return this._withRetry(async () => {
@@ -1094,6 +1159,12 @@ class ZIndexDbService {
1094
1159
  throw new Error('Database not initialized');
1095
1160
  }
1096
1161
  }
1162
+ if (!this._ensureStoreExists(storeConfig.name)) {
1163
+ await this._triggerUpgrade();
1164
+ if (!this._ensureStoreExists(storeConfig.name)) {
1165
+ throw new Error(`Failed to create store "${storeConfig.name}"`);
1166
+ }
1167
+ }
1097
1168
  const _key = storeConfig.encrypt !== false ? this._encrypt(key) : key;
1098
1169
  const _value = storeConfig.encrypt !== false ? this._encrypt(value) : value;
1099
1170
  return new Promise((resolve, reject) => {
@@ -1111,11 +1182,6 @@ class ZIndexDbService {
1111
1182
  });
1112
1183
  });
1113
1184
  }
1114
- /**
1115
- * Delete a key from the store
1116
- * @param key - Key or array of keys to delete
1117
- * @param storeName - Optional store name
1118
- */
1119
1185
  async delete(key, storeName) {
1120
1186
  const storeConfig = this._getStoreConfig(storeName);
1121
1187
  return this._withRetry(async () => {
@@ -1126,6 +1192,9 @@ class ZIndexDbService {
1126
1192
  throw new Error('Database not initialized');
1127
1193
  }
1128
1194
  }
1195
+ if (!this._ensureStoreExists(storeConfig.name)) {
1196
+ return;
1197
+ }
1129
1198
  const keys = Array.isArray(key) ? key : [key];
1130
1199
  return new Promise((resolve, reject) => {
1131
1200
  try {
@@ -1144,10 +1213,6 @@ class ZIndexDbService {
1144
1213
  });
1145
1214
  });
1146
1215
  }
1147
- /**
1148
- * Clear all data from a store (respecting protected keys)
1149
- * @param storeName - Optional store name
1150
- */
1151
1216
  async clear(storeName) {
1152
1217
  const storeConfig = this._getStoreConfig(storeName);
1153
1218
  const protectedKeys = [...this._globalProtectedKeys, ...(storeConfig.protectedKeys ?? [])];
@@ -1159,6 +1224,9 @@ class ZIndexDbService {
1159
1224
  throw new Error('Database not initialized');
1160
1225
  }
1161
1226
  }
1227
+ if (!this._ensureStoreExists(storeConfig.name)) {
1228
+ return;
1229
+ }
1162
1230
  if (protectedKeys.length === 0) {
1163
1231
  return new Promise((resolve, reject) => {
1164
1232
  const transaction = this._db.transaction([storeConfig.name], 'readwrite');
@@ -1200,10 +1268,6 @@ class ZIndexDbService {
1200
1268
  });
1201
1269
  });
1202
1270
  }
1203
- /**
1204
- * Get all items from a store
1205
- * @param storeName - Optional store name
1206
- */
1207
1271
  async getAll(storeName) {
1208
1272
  const storeConfig = this._getStoreConfig(storeName);
1209
1273
  return this._withRetry(async () => {
@@ -1214,6 +1278,9 @@ class ZIndexDbService {
1214
1278
  return {};
1215
1279
  }
1216
1280
  }
1281
+ if (!this._ensureStoreExists(storeConfig.name)) {
1282
+ return {};
1283
+ }
1217
1284
  return new Promise(resolve => {
1218
1285
  try {
1219
1286
  const transaction = this._db.transaction([storeConfig.name], this._mode);
@@ -1234,7 +1301,7 @@ class ZIndexDbService {
1234
1301
  }
1235
1302
  }
1236
1303
  catch {
1237
- // Skip invalid entries
1304
+ // Skip
1238
1305
  }
1239
1306
  }
1240
1307
  });
@@ -1249,14 +1316,13 @@ class ZIndexDbService {
1249
1316
  });
1250
1317
  });
1251
1318
  }
1252
- /**
1253
- * Set multiple items at once
1254
- * @param items - Object with key-value pairs
1255
- * @param storeName - Optional store name
1256
- */
1257
1319
  async setMultiple(items, storeName) {
1258
1320
  const storeConfig = this._getStoreConfig(storeName);
1259
1321
  const entries = Object.entries(items);
1322
+ await this._dbReady;
1323
+ if (this._db && !this._ensureStoreExists(storeConfig.name)) {
1324
+ await this._triggerUpgrade();
1325
+ }
1260
1326
  for (let i = 0; i < entries.length; i += Z_INDEXDB_BATCH_SIZE) {
1261
1327
  const batch = entries.slice(i, i + Z_INDEXDB_BATCH_SIZE);
1262
1328
  await this._withRetry(async () => {
@@ -1267,6 +1333,9 @@ class ZIndexDbService {
1267
1333
  throw new Error('Database not initialized');
1268
1334
  }
1269
1335
  }
1336
+ if (!this._ensureStoreExists(storeConfig.name)) {
1337
+ throw new Error(`Store "${storeConfig.name}" does not exist`);
1338
+ }
1270
1339
  return new Promise((resolve, reject) => {
1271
1340
  const transaction = this._db.transaction([storeConfig.name], 'readwrite');
1272
1341
  const store = transaction.objectStore(storeConfig.name);
@@ -1281,40 +1350,26 @@ class ZIndexDbService {
1281
1350
  });
1282
1351
  }
1283
1352
  }
1284
- /**
1285
- * Get the list of available store names
1286
- */
1287
1353
  getStoreNames() {
1288
1354
  return Array.from(this._stores.keys());
1289
1355
  }
1290
- /**
1291
- * Check if a store exists
1292
- * @param storeName - Store name to check
1293
- */
1294
1356
  hasStore(storeName) {
1295
1357
  return this._stores.has(storeName);
1296
1358
  }
1297
- /**
1298
- * Add a new store (requires database version upgrade)
1299
- * Note: This will cause database reconnection
1300
- * @param config - Store configuration
1301
- */
1302
1359
  async addStore(config) {
1303
1360
  if (this._stores.has(config.name)) {
1304
1361
  return;
1305
1362
  }
1306
1363
  this._stores.set(config.name, config);
1307
- this._version += 1;
1308
- this._closeConnection();
1309
- this._dbReady = this._initDb();
1310
- await this._dbReady;
1364
+ await this._triggerUpgrade();
1311
1365
  }
1312
1366
  }
1313
1367
 
1314
1368
  const Z_THEME_CONFIG = new InjectionToken('Z_THEME_CONFIG');
1315
1369
  const Z_DEFAULT_THEME = 'neutral';
1316
- const Z_THEME_CACHE_KEY = 'z-theme';
1317
- const Z_DARK_MODE_CACHE_KEY = 'z-dark-mode';
1370
+ const Z_THEME_CACHE_KEY = 'Z_THEME';
1371
+ const Z_DARK_MODE_CACHE_KEY = 'Z_DARK_MODE';
1372
+ const Z_THEME_CONFIG_CACHE_KEY = 'Z_THEME_CONFIG';
1318
1373
  const Z_THEME_CSS_MAP = {
1319
1374
  neutral: '',
1320
1375
  green: 'assets/css/themes/green.css',
@@ -1415,8 +1470,8 @@ class ZHttpAbstractService {
1415
1470
  constructor() {
1416
1471
  this.indexDbService = new ZIndexDbService({
1417
1472
  dbName: 'ZHttpCache',
1418
- defaultStore: 'HttpCache',
1419
- stores: [{ name: 'HttpCache', encrypt: true }],
1473
+ defaultStore: 'zHttpCache',
1474
+ stores: [{ name: 'zHttpCache', encrypt: true }],
1420
1475
  });
1421
1476
  }
1422
1477
  get(endpoint, options) {
@@ -1843,13 +1898,14 @@ class ZThemeService {
1843
1898
  _isBrowser = isPlatformBrowser(this._platformId);
1844
1899
  _defaultTheme = this._config?.defaultTheme ?? Z_DEFAULT_THEME;
1845
1900
  _defaultDarkMode = this._config?.defaultDarkMode ?? false;
1846
- _isDark = signal(this._getSavedDarkMode(), ...(ngDevMode ? [{ debugName: "_isDark" }] : []));
1847
- _currentTheme = signal(this._getSavedTheme(), ...(ngDevMode ? [{ debugName: "_currentTheme" }] : []));
1901
+ _isDark = signal(this._getInitialDarkMode(), ...(ngDevMode ? [{ debugName: "_isDark" }] : []));
1902
+ _currentTheme = signal(this._getInitialTheme(), ...(ngDevMode ? [{ debugName: "_currentTheme" }] : []));
1848
1903
  _loadedThemes = new Set(['neutral']);
1849
1904
  isDark = this._isDark.asReadonly();
1850
1905
  currentTheme = this._currentTheme.asReadonly();
1851
1906
  constructor() {
1852
1907
  if (this._isBrowser) {
1908
+ this._syncConfigToCache();
1853
1909
  this._initializeTheme();
1854
1910
  }
1855
1911
  effect(() => {
@@ -1901,18 +1957,43 @@ class ZThemeService {
1901
1957
  this.toggleDarkMode(savedDarkMode);
1902
1958
  }
1903
1959
  }
1904
- _getSavedTheme() {
1960
+ _getInitialTheme() {
1905
1961
  if (!this._isBrowser) {
1906
1962
  return this._defaultTheme;
1907
1963
  }
1964
+ if (this._hasConfigChanged()) {
1965
+ return this._defaultTheme;
1966
+ }
1908
1967
  return ZCacheService.get(Z_THEME_CACHE_KEY) ?? this._defaultTheme;
1909
1968
  }
1910
- _getSavedDarkMode() {
1969
+ _getInitialDarkMode() {
1911
1970
  if (!this._isBrowser) {
1912
1971
  return this._defaultDarkMode;
1913
1972
  }
1973
+ if (this._hasConfigChanged()) {
1974
+ return this._defaultDarkMode;
1975
+ }
1914
1976
  return ZCacheService.get(Z_DARK_MODE_CACHE_KEY) ?? this._defaultDarkMode;
1915
1977
  }
1978
+ _hasConfigChanged() {
1979
+ const cachedConfig = ZCacheService.get(Z_THEME_CONFIG_CACHE_KEY);
1980
+ if (!cachedConfig) {
1981
+ return false;
1982
+ }
1983
+ const currentConfig = {
1984
+ defaultTheme: this._defaultTheme,
1985
+ defaultDarkMode: this._defaultDarkMode,
1986
+ };
1987
+ return (cachedConfig.defaultTheme !== currentConfig.defaultTheme ||
1988
+ cachedConfig.defaultDarkMode !== currentConfig.defaultDarkMode);
1989
+ }
1990
+ _syncConfigToCache() {
1991
+ const currentConfig = {
1992
+ defaultTheme: this._defaultTheme,
1993
+ defaultDarkMode: this._defaultDarkMode,
1994
+ };
1995
+ ZCacheService.set(Z_THEME_CONFIG_CACHE_KEY, currentConfig);
1996
+ }
1916
1997
  _loadThemeCSS(theme, onLoad) {
1917
1998
  const cssPath = Z_THEME_CSS_MAP[theme];
1918
1999
  if (!cssPath) {
@@ -1953,5 +2034,5 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.0.6", ngImpor
1953
2034
  * Generated bundle index. Do not edit.
1954
2035
  */
1955
2036
 
1956
- export { ZCacheService, ZExcelService, ZHttpAbstractService, ZIndexDbService, ZSubjectService, ZThemeService, ZTranslateService, Z_DARK_MODE_CACHE_KEY, Z_DEFAULT_THEME, Z_EXCEL_BORDER_THIN, Z_EXCEL_CHAR_WIDTH_MAP, Z_EXCEL_COLORS, Z_EXCEL_DEFAULT_CONFIG, Z_EXCEL_FONT_MULTIPLIERS, Z_EXCEL_WIDTH_LIMITS, Z_LANG_CACHE_KEY, Z_THEME_CACHE_KEY, Z_THEME_CONFIG, Z_THEME_CSS_MAP };
2037
+ export { ZCacheService, ZExcelService, ZHttpAbstractService, ZIndexDbService, ZSubjectService, ZThemeService, ZTranslateService, Z_DARK_MODE_CACHE_KEY, Z_DEFAULT_THEME, Z_EXCEL_BORDER_THIN, Z_EXCEL_CHAR_WIDTH_MAP, Z_EXCEL_COLORS, Z_EXCEL_DEFAULT_CONFIG, Z_EXCEL_FONT_MULTIPLIERS, Z_EXCEL_WIDTH_LIMITS, Z_INDEXDB_BATCH_SIZE, Z_INDEXDB_DEFAULT_CONFIG, Z_INDEXDB_MAX_VERSION, Z_LANG_CACHE_KEY, Z_THEME_CACHE_KEY, Z_THEME_CONFIG, Z_THEME_CONFIG_CACHE_KEY, Z_THEME_CSS_MAP };
1957
2038
  //# sourceMappingURL=shival99-z-ui-services.mjs.map