@ethlete/query 1.0.1 → 2.0.0

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
- import { getCookie, setCookie, deleteCookie, DestroyService, equal, clone } from '@ethlete/core';
1
+ import { BehaviorSubjectWithSubscriberCount, getCookie, setCookie, deleteCookie, DestroyService, equal, clone } from '@ethlete/core';
2
2
  import { Observable, filter, takeWhile, switchMap, of, BehaviorSubject, Subject, takeUntil, interval, tap, timer, fromEvent, startWith, concat, take, debounceTime, combineLatest, map, distinctUntilChanged } from 'rxjs';
3
3
  import * as i0 from '@angular/core';
4
4
  import { Directive, Input, InjectionToken, inject, ChangeDetectorRef, ViewContainerRef, TemplateRef, ErrorHandler, ElementRef } from '@angular/core';
@@ -545,6 +545,12 @@ class Query {
545
545
  }
546
546
  return ts < Date.now();
547
547
  }
548
+ get isInUse() {
549
+ return this._state$.subscriberCount > 0;
550
+ }
551
+ get _config() {
552
+ return this._queryConfig;
553
+ }
548
554
  constructor(_client, _queryConfig, _routeWithParams, _args) {
549
555
  this._client = _client;
550
556
  this._queryConfig = _queryConfig;
@@ -553,7 +559,7 @@ class Query {
553
559
  this._currentId = 0;
554
560
  this._pollingSubscription = null;
555
561
  this._onAbort$ = new Subject();
556
- this._state$ = new BehaviorSubject({
562
+ this._state$ = new BehaviorSubjectWithSubscriberCount({
557
563
  type: "PREPARED" /* QueryStateType.Prepared */,
558
564
  meta: { id: this._currentId },
559
565
  });
@@ -1355,6 +1361,24 @@ class QueryStore {
1355
1361
  this._store.delete(id);
1356
1362
  this._logState(id, null, 'REMOVE');
1357
1363
  }
1364
+ forEach(callback) {
1365
+ for (const [key, query] of this._store) {
1366
+ callback(query, key);
1367
+ }
1368
+ }
1369
+ refreshQueriesInUse(config) {
1370
+ const { ignoreCacheValidity, purgeUnused } = config ?? {};
1371
+ for (const [key, query] of this._store) {
1372
+ if (query.isInUse &&
1373
+ (query.isExpired || ignoreCacheValidity) &&
1374
+ (query._config.autoRefreshOn?.queryClientDefaultHeadersChange ?? true)) {
1375
+ query.execute({ skipCache: true });
1376
+ }
1377
+ else if (purgeUnused && !query.isInUse) {
1378
+ this.remove(key);
1379
+ }
1380
+ }
1381
+ }
1358
1382
  _logState(key, item, operation) {
1359
1383
  if (!this._config?.enableChangeLogging)
1360
1384
  return;
@@ -1384,7 +1408,7 @@ class QueryStore {
1384
1408
  _runGarbageCollector() {
1385
1409
  this._logGarbageCollector('Collecting...');
1386
1410
  this._store.forEach((item, key) => {
1387
- if (item.isExpired) {
1411
+ if (item.isExpired && !item.isInUse) {
1388
1412
  this.remove(key);
1389
1413
  }
1390
1414
  });
@@ -1470,6 +1494,9 @@ class QueryClient {
1470
1494
  if (shouldCacheQuery(queryConfig.method)) {
1471
1495
  const existingQuery = this._store.get(cacheKey);
1472
1496
  if (existingQuery) {
1497
+ if (existingQuery.isExpired) {
1498
+ existingQuery.execute();
1499
+ }
1473
1500
  return existingQuery;
1474
1501
  }
1475
1502
  }
@@ -1491,14 +1518,17 @@ class QueryClient {
1491
1518
  }
1492
1519
  this._authProvider$.next(authProvider);
1493
1520
  };
1494
- this.setDefaultHeaders = (headers) => {
1521
+ this.setDefaultHeaders = (config) => {
1495
1522
  if (!this._clientConfig.request) {
1496
1523
  this._clientConfig.request = {
1497
- headers: headers ?? undefined,
1524
+ headers: config.headers ?? undefined,
1498
1525
  };
1499
1526
  }
1500
1527
  else {
1501
- this._clientConfig.request.headers = headers ?? undefined;
1528
+ this._clientConfig.request.headers = config.headers ?? undefined;
1529
+ }
1530
+ if (config.refreshQueriesInUse) {
1531
+ this._store.refreshQueriesInUse({ purgeUnused: true, ignoreCacheValidity: true });
1502
1532
  }
1503
1533
  };
1504
1534
  this.clearAuthProvider = () => {