lib0 0.2.44 → 0.2.45

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/dist/test.js CHANGED
@@ -7407,15 +7407,31 @@
7407
7407
  return n ? n.val : undefined
7408
7408
  };
7409
7409
 
7410
+ /**
7411
+ * @template K, V
7412
+ *
7413
+ * @param {Cache<K, V>} cache
7414
+ * @param {K} key
7415
+ */
7416
+ const remove = (cache, key) => {
7417
+ const n = cache._map.get(key);
7418
+ if (n) {
7419
+ removeNode(cache._q, n);
7420
+ cache._map.delete(key);
7421
+ return n.val && !(n.val instanceof Promise) ? n.val : undefined
7422
+ }
7423
+ };
7424
+
7410
7425
  /**
7411
7426
  * @template K, V
7412
7427
  *
7413
7428
  * @param {Cache<K, V>} cache
7414
7429
  * @param {K} key
7415
7430
  * @param {function():Promise<V>} init
7431
+ * @param {boolean} removeNull Optional argument that automatically removes values that resolve to null/undefined from the cache.
7416
7432
  * @return {Promise<V> | V}
7417
7433
  */
7418
- const setIfUndefined = (cache, key, init) => {
7434
+ const setIfUndefined = (cache, key, init, removeNull = false) => {
7419
7435
  const now = removeStale(cache);
7420
7436
  const q = cache._q;
7421
7437
  const n = cache._map.get(key);
@@ -7433,6 +7449,9 @@
7433
7449
  if (p === node.val) {
7434
7450
  node.val = v;
7435
7451
  }
7452
+ if (removeNull && v == null) {
7453
+ remove(cache, key);
7454
+ }
7436
7455
  });
7437
7456
  return p
7438
7457
  }
@@ -7499,6 +7518,16 @@
7499
7518
  await xp;
7500
7519
  // we override the Entry.val property in cache when p resolves. However, we must prevent that when the value is overriden before p is resolved.
7501
7520
  assert(get(c, 'a') === 'y');
7521
+
7522
+ // test that we can remove properties
7523
+ remove(c, 'a');
7524
+ remove(c, 'does not exist'); // remove a non-existent property to achieve full test-coverage
7525
+ assert(get(c, 'a') === undefined);
7526
+
7527
+ // test that the optional property in setifUndefined works
7528
+ const yp = setIfUndefined(c, 'a', () => resolveWith(null), true);
7529
+ assert(await yp === null);
7530
+ assert(get(c, 'a') === undefined);
7502
7531
  };
7503
7532
 
7504
7533
  var cache = /*#__PURE__*/Object.freeze({