bruce-models 2.1.0 → 2.1.1

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.
@@ -3140,259 +3140,125 @@
3140
3140
  });
3141
3141
  });
3142
3142
 
3143
- (function (ObjectUtils) {
3144
- /**
3145
- * Generates a Bruce compatible UID.
3146
- * @returns
3147
- */
3148
- function UId() {
3149
- return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
3150
- const r = Math.random() * 16 | 0, v = c == "x" ? r : (r & 0x3 | 0x8);
3151
- return v.toString(16);
3152
- });
3153
- }
3154
- ObjectUtils.UId = UId;
3155
- })(exports.ObjectUtils || (exports.ObjectUtils = {}));
3156
-
3157
- class CacheDictionary {
3143
+ const CACHE_VERSION_PREFIX = "v2_";
3144
+ class CacheUtil {
3158
3145
  constructor(id) {
3159
- this.memory = Object.create(null);
3160
- if (!id) {
3161
- id = "BRUCE_UI_MODELS_CACHE_DICT_" + exports.ObjectUtils.UId();
3162
- }
3163
- this.id = id;
3164
3146
  this.store = localforage.createInstance({
3165
- name: id
3166
- });
3167
- }
3168
- get Id() {
3169
- return this.id;
3170
- }
3171
- GetItems() {
3172
- return __awaiter(this, void 0, void 0, function* () {
3173
- if (this.items == null) {
3174
- const raw = yield this.store.getItem(this.id);
3175
- if (raw == null) {
3176
- this.items = [];
3177
- }
3178
- else {
3179
- // Check if any are expired.
3180
- // We can clean this every first time we load.
3181
- const now = new Date().getTime();
3182
- for (const item of raw) {
3183
- if (item.duration > -1 && (now - item.created) / 1000 > item.duration) {
3184
- if (item.inMemory) {
3185
- this.memory[item.key] = null;
3186
- delete this.memory[item.key];
3187
- }
3188
- else {
3189
- yield this.store.removeItem(item.key);
3190
- }
3191
- }
3192
- }
3193
- this.items = raw;
3194
- }
3195
- }
3196
- return this.items;
3197
- });
3198
- }
3199
- GetKeys() {
3200
- return __awaiter(this, void 0, void 0, function* () {
3201
- return (yield this.GetItems()).map(x => "" + x.key);
3147
+ name: CACHE_VERSION_PREFIX + id,
3202
3148
  });
3149
+ this.inMemoryPromises = {};
3150
+ this.keyVersions = {};
3203
3151
  }
3204
- Set(key, value, duration) {
3152
+ Set(key, value, duration = 300000) {
3205
3153
  return __awaiter(this, void 0, void 0, function* () {
3206
- key = "" + key;
3207
- const items = yield this.GetItems();
3208
- const itemIndex = items.findIndex(x => x.key == key);
3209
- if (itemIndex > -1) {
3210
- const item = items[itemIndex];
3211
- if (item.inMemory) {
3212
- this.memory[item.key] = null;
3213
- delete this.memory[item.key];
3214
- }
3215
- else {
3216
- yield this.store.removeItem(item.key);
3217
- }
3218
- items.splice(itemIndex, 1);
3219
- }
3220
- const item = {
3221
- key: key,
3222
- duration: duration,
3223
- created: new Date().getTime(),
3224
- inMemory: false
3225
- };
3226
- // Short duration, keep in memory.
3227
- if (duration < 60) {
3228
- item.inMemory = true;
3229
- this.memory[item.key] = value;
3230
- }
3231
- // Let's resolve and store the value in local-storage.
3232
- // If resolution crashes then we'll keep retaining that in memory.
3233
- else if (value instanceof Promise) {
3234
- value.then((x) => __awaiter(this, void 0, void 0, function* () {
3235
- try {
3236
- const items = yield this.GetItems();
3237
- const item = items.find(x => x.key == key);
3238
- if (!item) {
3239
- console.log("Item was removed from cache before promise resolved.", key, items);
3240
- return;
3241
- }
3242
- try {
3243
- yield this.store.setItem(item.key, x);
3244
- item.inMemory = false;
3245
- this.memory[item.key] = null;
3246
- delete this.memory[item.key];
3247
- yield this.store.setItem(this.id, items);
3248
- }
3249
- catch (e) {
3250
- console.warn(e);
3251
- }
3154
+ if (value instanceof Promise) {
3155
+ this.inMemoryPromises[key] = value;
3156
+ const currentVersion = (this.keyVersions[key] = (this.keyVersions[key] || 0) + 1);
3157
+ value
3158
+ .then((resolvedValue) => {
3159
+ if (this.keyVersions[key] === currentVersion) {
3160
+ this.Set(key, resolvedValue, duration);
3252
3161
  }
3253
- catch (e) {
3254
- console.warn(e);
3162
+ })
3163
+ .catch((error) => {
3164
+ if (this.keyVersions[key] === currentVersion) {
3165
+ this.Set(key, error, duration);
3255
3166
  }
3256
- }));
3257
- item.inMemory = true;
3258
- this.memory[item.key] = value;
3167
+ })
3168
+ .finally(() => {
3169
+ delete this.inMemoryPromises[key];
3170
+ });
3259
3171
  }
3260
- if (!item.inMemory) {
3261
- try {
3262
- yield this.store.setItem(item.key, value);
3263
- }
3264
- catch (e) {
3265
- item.inMemory = true;
3266
- this.memory[item.key] = value;
3267
- console.warn(e);
3268
- }
3172
+ else {
3173
+ const item = {
3174
+ value,
3175
+ expiry: Date.now() + duration,
3176
+ };
3177
+ yield this.store.setItem(key, item);
3269
3178
  }
3270
- items.push(item);
3271
- yield this.store.setItem(this.id, items);
3272
- this.items = items;
3273
3179
  });
3274
3180
  }
3275
- Remove(key) {
3181
+ Get(key) {
3276
3182
  return __awaiter(this, void 0, void 0, function* () {
3277
- key = "" + key;
3278
- const items = yield this.GetItems();
3279
- const index = items.findIndex(x => x.key == key);
3280
- if (index >= 0) {
3281
- const item = items[index];
3282
- if (item.inMemory) {
3283
- this.memory[item.key] = null;
3284
- delete this.memory[item.key];
3285
- }
3286
- else {
3287
- this.store.removeItem(item.key);
3288
- }
3289
- items.splice(index, 1);
3290
- this.store.setItem(this.id, items);
3183
+ if (this.inMemoryPromises[key]) {
3184
+ return this.inMemoryPromises[key];
3291
3185
  }
3292
- this.items = items;
3293
- });
3294
- }
3295
- Clear() {
3296
- return __awaiter(this, void 0, void 0, function* () {
3297
- const items = yield this.GetItems();
3298
- for (const item of items) {
3299
- if (item.inMemory) {
3300
- this.store.removeItem(item.key);
3301
- }
3186
+ const item = (yield this.store.getItem(key));
3187
+ if (!item) {
3188
+ return null;
3189
+ }
3190
+ if (Date.now() > item.expiry) {
3191
+ yield this.store.removeItem(key);
3192
+ return null;
3302
3193
  }
3303
- this.store.setItem(this.id, []);
3304
- this.items = [];
3305
- this.memory = Object.create(null);
3194
+ return item.value;
3306
3195
  });
3307
3196
  }
3308
- GetData(key) {
3197
+ ClearCache(pattern, mode = "equals") {
3309
3198
  return __awaiter(this, void 0, void 0, function* () {
3310
- key = "" + key;
3311
- const item = (yield this.GetItems()).find(x => x.key == key);
3312
- const isExpired = item == null || item.duration > -1 && (new Date().getTime() - item.created) / 1000 > item.duration;
3313
- if (isExpired) {
3314
- this.Remove(key);
3315
- return null;
3199
+ const keys = yield this.store.keys();
3200
+ const checkFuncs = {
3201
+ equals: (key) => key === pattern,
3202
+ contains: (key) => key.includes(pattern),
3203
+ startswith: (key) => key.startsWith(pattern),
3204
+ };
3205
+ const check = checkFuncs[mode];
3206
+ if (!check) {
3207
+ throw new Error("Invalid mode");
3316
3208
  }
3317
- if (item.inMemory) {
3318
- return this.memory[item.key];
3209
+ for (const key of keys) {
3210
+ if (check(key)) {
3211
+ yield this.store.removeItem(key);
3212
+ }
3319
3213
  }
3320
- const raw = yield this.store.getItem(item.key);
3321
- if (raw == null) {
3322
- return null;
3214
+ for (const key in this.inMemoryPromises) {
3215
+ if (check(key)) {
3216
+ delete this.inMemoryPromises[key];
3217
+ this.keyVersions[key]++;
3218
+ }
3323
3219
  }
3324
- return raw;
3325
3220
  });
3326
3221
  }
3327
3222
  }
3328
3223
  class CacheControl {
3329
3224
  constructor(id) {
3330
3225
  this.Disabled = false;
3331
- this.data = new CacheDictionary(id);
3226
+ this.data = new CacheUtil(id);
3332
3227
  }
3333
3228
  /**
3334
3229
  * @param id
3335
3230
  * @param data
3336
3231
  * @param duration seconds to keep the data in cache. -1 for infinite.
3337
3232
  */
3338
- Set(id, data, duration = -1) {
3233
+ Set(id, data, duration = 300) {
3339
3234
  return __awaiter(this, void 0, void 0, function* () {
3340
- return this.data.Set(id, data, duration);
3235
+ const durationMs = duration === -1 ? undefined : duration * 1000;
3236
+ return this.data.Set(String(id), data, durationMs);
3341
3237
  });
3342
3238
  }
3343
3239
  Get(id) {
3344
3240
  return __awaiter(this, void 0, void 0, function* () {
3345
- return this.data.GetData(id);
3241
+ return (yield this.data.Get(String(id)));
3346
3242
  });
3347
3243
  }
3348
3244
  Clear() {
3349
3245
  return __awaiter(this, void 0, void 0, function* () {
3350
- return this.data.Clear();
3246
+ return this.data.ClearCache("");
3351
3247
  });
3352
3248
  }
3353
3249
  Remove(id) {
3354
3250
  return __awaiter(this, void 0, void 0, function* () {
3355
- return this.data.Remove(id);
3251
+ return this.data.ClearCache(String(id), "equals");
3356
3252
  });
3357
3253
  }
3358
3254
  RemoveByStartsWith(text) {
3359
3255
  return __awaiter(this, void 0, void 0, function* () {
3360
- const items = [...(yield this.data.GetItems())];
3361
- for (const item of items) {
3362
- if (item.key.startsWith(text)) {
3363
- yield this.data.Remove(item.key);
3364
- }
3365
- }
3256
+ return this.data.ClearCache(text, "startswith");
3366
3257
  });
3367
3258
  }
3368
3259
  RemoveByContains(text) {
3369
3260
  return __awaiter(this, void 0, void 0, function* () {
3370
- const items = [...(yield this.data.GetItems())];
3371
- for (const item of items) {
3372
- if (item.key.includes(text)) {
3373
- yield this.data.Remove(item.key);
3374
- }
3375
- }
3376
- });
3377
- }
3378
- GetKeys() {
3379
- return __awaiter(this, void 0, void 0, function* () {
3380
- if (this.Disabled) {
3381
- return [];
3382
- }
3383
- const items = yield this.data.GetItems();
3384
- return items.map(x => x.key);
3385
- });
3386
- }
3387
- GetValues() {
3388
- return __awaiter(this, void 0, void 0, function* () {
3389
- if (this.Disabled) {
3390
- return [];
3391
- }
3392
- const items = yield this.data.GetItems();
3393
- const keys = items.map(x => x.key);
3394
- const values = yield Promise.all(keys.map(key => this.data.GetData(key)));
3395
- return values.filter(x => x != null);
3261
+ return this.data.ClearCache(text, "contains");
3396
3262
  });
3397
3263
  }
3398
3264
  }
@@ -4636,6 +4502,20 @@
4636
4502
  Color.ColorFromStr = ColorFromStr;
4637
4503
  })(exports.Color || (exports.Color = {}));
4638
4504
 
4505
+ (function (ObjectUtils) {
4506
+ /**
4507
+ * Generates a Bruce compatible UID.
4508
+ * @returns
4509
+ */
4510
+ function UId() {
4511
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
4512
+ const r = Math.random() * 16 | 0, v = c == "x" ? r : (r & 0x3 | 0x8);
4513
+ return v.toString(16);
4514
+ });
4515
+ }
4516
+ ObjectUtils.UId = UId;
4517
+ })(exports.ObjectUtils || (exports.ObjectUtils = {}));
4518
+
4639
4519
  /**
4640
4520
  * Utility to help with parsing and wrapping Bruce paths.
4641
4521
  */