@nextgis/ngw-connector 3.0.0-alpha.6 → 3.0.0-alpha.8

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.
@@ -1,4 +1,4 @@
1
- /** Bundle of @nextgis/ngw-connector; version: 3.0.0-alpha.6; author: NextGIS */
1
+ /** Bundle of @nextgis/ngw-connector; version: 3.0.0-alpha.8; author: NextGIS */
2
2
  function defined(val) {
3
3
  return val !== void 0 && val !== null;
4
4
  }
@@ -569,10 +569,23 @@ const _Cache = class _Cache {
569
569
  * });
570
570
  * ```
571
571
  */
572
- addFull(key, valueToSet, props) {
573
- return this.add(key, valueToSet, props, true);
574
- }
575
- add(key, valueToSet, props, onlyFull) {
572
+ addFull(key, valueToSet, propsOrOptions, expirationTime) {
573
+ if (typeof propsOrOptions === "object" && "valueToSet" in propsOrOptions) {
574
+ propsOrOptions.onlyFull = true;
575
+ }
576
+ return this.add(key, valueToSet, propsOrOptions, true, expirationTime);
577
+ }
578
+ add(key, valueToSet, propsOrOptions, onlyFull, expirationTime) {
579
+ let opt;
580
+ let props;
581
+ if (typeof propsOrOptions === "object" && "valueToSet" in propsOrOptions) {
582
+ opt = propsOrOptions;
583
+ onlyFull = opt.onlyFull;
584
+ expirationTime = opt.expirationTime;
585
+ props = opt.props;
586
+ } else {
587
+ props = propsOrOptions;
588
+ }
576
589
  const exist = this._find(key, props);
577
590
  if (!exist) {
578
591
  let value;
@@ -582,10 +595,12 @@ const _Cache = class _Cache {
582
595
  value = valueToSet;
583
596
  }
584
597
  const nonEmptyProps = props && JSON.parse(JSON.stringify(objectRemoveEmpty(props)));
598
+ expirationTime = expirationTime ? Date.now() + expirationTime : void 0;
585
599
  const cacheItem = {
586
600
  key,
587
601
  value,
588
- props: nonEmptyProps
602
+ props: nonEmptyProps,
603
+ expirationTime
589
604
  };
590
605
  if (onlyFull && !full(value)) {
591
606
  return value;
@@ -612,9 +627,19 @@ const _Cache = class _Cache {
612
627
  }
613
628
  match(key, props) {
614
629
  const cacheRecord = this._find(key, props);
630
+ if (cacheRecord && cacheRecord.expirationTime && Date.now() > cacheRecord.expirationTime) {
631
+ this.delete(cacheRecord);
632
+ return void 0;
633
+ }
615
634
  return cacheRecord == null ? void 0 : cacheRecord.value;
616
635
  }
617
636
  matchAll(key, props) {
637
+ const now = Date.now();
638
+ this.cache.forEach((item) => {
639
+ if (item.expirationTime && now > item.expirationTime) {
640
+ this.delete(item);
641
+ }
642
+ });
618
643
  if (key) {
619
644
  return this.cache.filter((x) => this._filter(x, key, props)).map((x) => x.value);
620
645
  }
@@ -644,7 +669,7 @@ let Cache = _Cache;
644
669
 
645
670
  var pkg = {
646
671
  name: "@nextgis/ngw-connector",
647
- version: "3.0.0-alpha.6",
672
+ version: "3.0.0-alpha.8",
648
673
  _priority: 12,
649
674
  description: "A lightweight HTTP client optimized for use with NextGIS Web API",
650
675
  main: "index.js",
@@ -653,7 +678,7 @@ var pkg = {
653
678
  jsdelivr: "lib/ngw-connector.global.prod.js",
654
679
  types: "lib/index.d.ts",
655
680
  dependencies: {
656
- "@nextgis/cache": "^3.0.0-alpha.5",
681
+ "@nextgis/cache": "^3.0.0-alpha.7",
657
682
  "@nextgis/cancelable-promise": "^3.0.0-alpha.5",
658
683
  "@nextgis/utils": "^3.0.0-alpha.5",
659
684
  "@types/events": "^3.0.0",
@@ -1405,9 +1430,6 @@ class NgwConnector {
1405
1430
  addConnector(this);
1406
1431
  }
1407
1432
  }
1408
- static create(options) {
1409
- return new this(options);
1410
- }
1411
1433
  /**
1412
1434
  * Clear the cache.
1413
1435
  */
@@ -1588,7 +1610,7 @@ class NgwConnector {
1588
1610
  this._cleanActiveRequest(id);
1589
1611
  }
1590
1612
  });
1591
- if (cache && method === "GET") {
1613
+ if (method === "GET") {
1592
1614
  const cacheOptions = cacheProps ? cacheProps : __spreadProps(__spreadValues$2({}, objectRemoveEmpty({
1593
1615
  headers,
1594
1616
  withCredentials,
@@ -1598,12 +1620,10 @@ class NgwConnector {
1598
1620
  })), {
1599
1621
  params
1600
1622
  });
1601
- return this.cache.add(
1602
- cacheName || url,
1603
- createPromise,
1604
- cacheOptions,
1605
- false
1606
- );
1623
+ return this.cache.add(cacheName || url, createPromise, {
1624
+ props: cacheOptions,
1625
+ expirationTime: cache ? void 0 : 500
1626
+ });
1607
1627
  }
1608
1628
  return createPromise();
1609
1629
  });
@@ -2067,6 +2087,9 @@ class NgwConnectorExtended extends NgwConnector {
2067
2087
  cacheId: options.cacheId
2068
2088
  });
2069
2089
  }
2090
+ static create(options) {
2091
+ return new this(options);
2092
+ }
2070
2093
  /**
2071
2094
  * Clear the cache.
2072
2095
  */