@dereekb/rxjs 10.0.21 → 10.0.23

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/index.cjs.js CHANGED
@@ -3193,7 +3193,13 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
3193
3193
  * @returns
3194
3194
  */
3195
3195
  function iterationHasNextAndCanLoadMore(iteration) {
3196
- return rxjs.combineLatest([iteration.hasNext$, iteration.canLoadMore$]).pipe(rxjs.map(util.reduceBooleansWithAndFn(true)), rxjs.shareReplay(1));
3196
+ return iteration.canLoadMore$.pipe(rxjs.switchMap(canLoadMore => {
3197
+ if (canLoadMore) {
3198
+ return iteration.hasNext$;
3199
+ } else {
3200
+ return rxjs.of(false);
3201
+ }
3202
+ }), rxjs.shareReplay(1));
3197
3203
  }
3198
3204
  /**
3199
3205
  * Automatically calls next up to the current maxPageLoadLimit configured on the iterator.
@@ -3238,7 +3244,9 @@ function iteratorNextPageUntilPage(iteration, page) {
3238
3244
  next: firstLatestPage => {
3239
3245
  const promise = util.performTaskLoop({
3240
3246
  initValue: firstLatestPage,
3241
- checkContinue: latestPage => checkPageLimit(latestPage),
3247
+ checkContinue: latestPage => __awaiter(this, void 0, void 0, function* () {
3248
+ return rxjs.firstValueFrom(iterationHasNextAndCanLoadMore(iteration)).then(canLoadMore => canLoadMore && checkPageLimit(latestPage));
3249
+ }),
3242
3250
  next: () => __awaiter(this, void 0, void 0, function* () {
3243
3251
  return yield iteration.nextPage();
3244
3252
  })
@@ -4796,34 +4804,43 @@ function itemAccumulatorNextPageUntilResultsCount(config) {
4796
4804
  countResultsFunction: countResults
4797
4805
  } = config;
4798
4806
  const getMaxResultsLimit = util.asGetter(maxResultsLimit);
4807
+ const canLoadMoreObs = iterationHasNextAndCanLoadMore(accumulator.itemIteration).pipe(timeoutStartWith(false, 100),
4808
+ // TODO: This can fail to emit anything if the iterator has been destroyed
4809
+ rxjs.shareReplay(1));
4799
4810
  function checkResultsLimit() {
4800
4811
  return __awaiter(this, void 0, void 0, function* () {
4801
- const allItems = yield rxjs.firstValueFrom(accumulator.allItems$);
4812
+ const allItems = yield rxjs.firstValueFrom(accumulator.currentAllItems$);
4813
+ const canLoadMore = yield rxjs.firstValueFrom(canLoadMoreObs);
4802
4814
  const currentCount = yield countResults(allItems);
4803
4815
  const maxResultsLimit = getMaxResultsLimit();
4816
+ const shouldContinue = canLoadMore && currentCount < maxResultsLimit;
4804
4817
  return {
4805
- shouldContinue: currentCount < maxResultsLimit,
4818
+ shouldContinue,
4806
4819
  currentCount
4807
4820
  };
4808
4821
  });
4809
4822
  }
4810
4823
  return new Promise((resolve, reject) => {
4811
- accumulator.allItems$.pipe(rxjs.first(), rxjs.switchMap(allItems => rxjs.from(util.asPromise(countResults(allItems))))).subscribe({
4824
+ accumulator.currentAllItems$.pipe(rxjs.first(), rxjs.switchMap(allItems => rxjs.from(util.asPromise(countResults(allItems))))).subscribe({
4812
4825
  next: currentResultsCount => __awaiter(this, void 0, void 0, function* () {
4813
- const page = yield util.performTaskLoop({
4826
+ util.performTaskLoop({
4814
4827
  initValue: currentResultsCount,
4815
- checkContinue: () => __awaiter(this, void 0, void 0, function* () {
4828
+ checkContinue: (x, i) => __awaiter(this, void 0, void 0, function* () {
4816
4829
  const result = yield checkResultsLimit();
4817
4830
  currentResultsCount = result.currentCount;
4818
4831
  return result.shouldContinue;
4819
4832
  }),
4820
4833
  next: () => __awaiter(this, void 0, void 0, function* () {
4821
- return yield accumulator.itemIteration.nextPage();
4834
+ return accumulator.itemIteration.nextPage();
4822
4835
  })
4823
- });
4824
- resolve({
4825
- page,
4826
- resultsCount: currentResultsCount
4836
+ }).then(page => {
4837
+ resolve({
4838
+ page,
4839
+ resultsCount: currentResultsCount
4840
+ });
4841
+ }).catch(error => {
4842
+ reject(error);
4843
+ throw error;
4827
4844
  });
4828
4845
  }),
4829
4846
  error: error => {
@@ -5109,12 +5126,12 @@ class ItemPageIterationInstance {
5109
5126
  this.latestSuccessfulPageResults$ = this.state$.pipe(rxjs.map(x => x.lastSuccessful), filterMaybe(), rxjs.shareReplay(1));
5110
5127
  this.currentState$ = this.currentPageResultState$.pipe(mapItemPageLoadingStateFromResultPageLoadingState(), rxjs.shareReplay(1));
5111
5128
  this.latestLoadedPage$ = this.latestPageResultState$.pipe(rxjs.map(x => x.page), rxjs.distinctUntilChanged(), rxjs.shareReplay(1));
5112
- this.numberOfPagesLoaded$ = this.latestLoadedPage$.pipe(rxjs.map(x => x + 1), rxjs.shareReplay(1));
5129
+ this.numberOfPagesLoaded$ = this.latestLoadedPage$.pipe(rxjs.map(x => x + 1), rxjs.defaultIfEmpty(0), rxjs.shareReplay(1));
5113
5130
  // MARK: ItemIteration
5114
5131
  /**
5115
5132
  * Whether or not there are more results to load.
5116
5133
  */
5117
- this.hasNext$ = this.hasReachedEndResult$.pipe(rxjs.map(x => !x), rxjs.shareReplay(1));
5134
+ this.hasNext$ = this.hasReachedEndResult$.pipe(rxjs.map(x => !x), rxjs.defaultIfEmpty(false), rxjs.shareReplay(1));
5118
5135
  /**
5119
5136
  * Whether or not the successfulPageResultsCount has passed the maxPageLoadLimit
5120
5137
  */
@@ -5136,18 +5153,24 @@ class ItemPageIterationInstance {
5136
5153
  this._maxPageLoadLimit.next(limit);
5137
5154
  }
5138
5155
  nextPage(request = {}) {
5139
- return new Promise((resolve, reject) => {
5140
- this._nextFinished$.pipe(rxjs.exhaustMap(() => this.latestPageResultState$), rxjs.first()).subscribe({
5156
+ return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
5157
+ this._nextFinished$.pipe(rxjs.exhaustMap(() => this.latestPageResultState$), rxjs.defaultIfEmpty({
5158
+ error: undefined,
5159
+ page: -1
5160
+ }), rxjs.first()).subscribe({
5141
5161
  next: latestState => {
5142
5162
  if (latestState.error) {
5143
5163
  reject(latestState.error);
5144
5164
  } else {
5145
5165
  resolve(latestState.page);
5146
5166
  }
5167
+ },
5168
+ error: error => {
5169
+ reject(error);
5147
5170
  }
5148
5171
  });
5149
5172
  this.next(request);
5150
- });
5173
+ }));
5151
5174
  }
5152
5175
  next(request = {}) {
5153
5176
  this._pushNext(request);
package/index.esm.js CHANGED
@@ -1,5 +1,5 @@
1
- import { isObservable, of, switchMap, map, filter, skipWhile, EMPTY, combineLatest, delay, startWith, distinctUntilChanged, mergeMap, BehaviorSubject, shareReplay, skip, first, merge, finalize, scan, exhaustMap, identity, throttleTime, timeout, tap, throwError, timer, takeWhile, delayWhen, asyncScheduler, from, catchError, firstValueFrom, defaultIfEmpty } from 'rxjs';
2
- import { getValueFromGetter, isMaybeSo, areEqualPOJOValues, convertToArray, reduceBooleansWithAndFn, asGetter, performTaskLoop, isMaybeNot, pushItemOrArrayItemsIntoArray, pushArrayItemsIntoArray, forEachWithArray, asArray, filterAndMapFunction, objectKeysEqualityComparatorFunction, objectKeyEqualityComparatorFunction, asPromise, randomNumberFactory, mapKeysIntersectionObjectToArray, mapsHaveSameKeys, incrementingNumberFactory, filterUniqueFunction, build, cachedGetter, allKeyValueTuples, keyValueMapFactory, multiKeyValueMapFactory, setContainsAllValues, setContainsAnyValue, setContainsNoneOfValue, hasSameValues, compareEqualityWithValueFromItemsFunction, searchStringFilterFunction, objectHasKey, toReadableError, reduceBooleansWithOr, reduceBooleansWithAnd, valuesAreBothNullishOrEquivalent, filterMaybeValues, mergeObjects, safeCompareEquality, limitArray, hasNonNullValue, mapFunctionOutputPair, lastValue, flattenArray, filteredPage, FIRST_PAGE, hasValueOrNotEmpty, getNextPageNumber, reduceBooleansWithOrFn } from '@dereekb/util';
1
+ import { isObservable, of, switchMap, map, filter, skipWhile, EMPTY, combineLatest, delay, startWith, distinctUntilChanged, mergeMap, BehaviorSubject, shareReplay, skip, first, merge, finalize, firstValueFrom, scan, exhaustMap, identity, throttleTime, timeout, tap, throwError, timer, takeWhile, delayWhen, asyncScheduler, from, catchError, defaultIfEmpty } from 'rxjs';
2
+ import { getValueFromGetter, isMaybeSo, areEqualPOJOValues, convertToArray, asGetter, performTaskLoop, isMaybeNot, pushItemOrArrayItemsIntoArray, pushArrayItemsIntoArray, forEachWithArray, asArray, filterAndMapFunction, objectKeysEqualityComparatorFunction, objectKeyEqualityComparatorFunction, asPromise, randomNumberFactory, mapKeysIntersectionObjectToArray, mapsHaveSameKeys, incrementingNumberFactory, filterUniqueFunction, build, cachedGetter, allKeyValueTuples, keyValueMapFactory, multiKeyValueMapFactory, setContainsAllValues, setContainsAnyValue, setContainsNoneOfValue, hasSameValues, compareEqualityWithValueFromItemsFunction, searchStringFilterFunction, objectHasKey, toReadableError, reduceBooleansWithOr, reduceBooleansWithAnd, valuesAreBothNullishOrEquivalent, filterMaybeValues, mergeObjects, safeCompareEquality, limitArray, hasNonNullValue, mapFunctionOutputPair, lastValue, flattenArray, filteredPage, FIRST_PAGE, hasValueOrNotEmpty, getNextPageNumber, reduceBooleansWithOrFn } from '@dereekb/util';
3
3
  import ms from 'ms';
4
4
 
5
5
  var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
@@ -3283,7 +3283,13 @@ $$1({ target: 'Promise', stat: true, forced: FORCED_PROMISE_CONSTRUCTOR }, {
3283
3283
  * @returns
3284
3284
  */
3285
3285
  function iterationHasNextAndCanLoadMore(iteration) {
3286
- return combineLatest([iteration.hasNext$, iteration.canLoadMore$]).pipe(map(reduceBooleansWithAndFn(true)), shareReplay(1));
3286
+ return iteration.canLoadMore$.pipe(switchMap(canLoadMore => {
3287
+ if (canLoadMore) {
3288
+ return iteration.hasNext$;
3289
+ } else {
3290
+ return of(false);
3291
+ }
3292
+ }), shareReplay(1));
3287
3293
  }
3288
3294
 
3289
3295
  /**
@@ -3330,7 +3336,7 @@ function iteratorNextPageUntilPage(iteration, page) {
3330
3336
  next: firstLatestPage => {
3331
3337
  const promise = performTaskLoop({
3332
3338
  initValue: firstLatestPage,
3333
- checkContinue: latestPage => checkPageLimit(latestPage),
3339
+ checkContinue: async latestPage => firstValueFrom(iterationHasNextAndCanLoadMore(iteration)).then(canLoadMore => canLoadMore && checkPageLimit(latestPage)),
3334
3340
  next: async () => await iteration.nextPage()
3335
3341
  });
3336
3342
  resolve(promise);
@@ -5120,30 +5126,39 @@ function itemAccumulatorNextPageUntilResultsCount(config) {
5120
5126
  countResultsFunction: countResults
5121
5127
  } = config;
5122
5128
  const getMaxResultsLimit = asGetter(maxResultsLimit);
5129
+ const canLoadMoreObs = iterationHasNextAndCanLoadMore(accumulator.itemIteration).pipe(timeoutStartWith(false, 100),
5130
+ // TODO: This can fail to emit anything if the iterator has been destroyed
5131
+ shareReplay(1));
5123
5132
  async function checkResultsLimit() {
5124
- const allItems = await firstValueFrom(accumulator.allItems$);
5133
+ const allItems = await firstValueFrom(accumulator.currentAllItems$);
5134
+ const canLoadMore = await firstValueFrom(canLoadMoreObs);
5125
5135
  const currentCount = await countResults(allItems);
5126
5136
  const maxResultsLimit = getMaxResultsLimit();
5137
+ const shouldContinue = canLoadMore && currentCount < maxResultsLimit;
5127
5138
  return {
5128
- shouldContinue: currentCount < maxResultsLimit,
5139
+ shouldContinue,
5129
5140
  currentCount
5130
5141
  };
5131
5142
  }
5132
5143
  return new Promise((resolve, reject) => {
5133
- accumulator.allItems$.pipe(first(), switchMap(allItems => from(asPromise(countResults(allItems))))).subscribe({
5144
+ accumulator.currentAllItems$.pipe(first(), switchMap(allItems => from(asPromise(countResults(allItems))))).subscribe({
5134
5145
  next: async currentResultsCount => {
5135
- const page = await performTaskLoop({
5146
+ performTaskLoop({
5136
5147
  initValue: currentResultsCount,
5137
- checkContinue: async () => {
5148
+ checkContinue: async (x, i) => {
5138
5149
  const result = await checkResultsLimit();
5139
5150
  currentResultsCount = result.currentCount;
5140
5151
  return result.shouldContinue;
5141
5152
  },
5142
- next: async () => await accumulator.itemIteration.nextPage()
5143
- });
5144
- resolve({
5145
- page,
5146
- resultsCount: currentResultsCount
5153
+ next: async () => accumulator.itemIteration.nextPage()
5154
+ }).then(page => {
5155
+ resolve({
5156
+ page,
5157
+ resultsCount: currentResultsCount
5158
+ });
5159
+ }).catch(error => {
5160
+ reject(error);
5161
+ throw error;
5147
5162
  });
5148
5163
  },
5149
5164
  error: error => {
@@ -5450,12 +5465,12 @@ class ItemPageIterationInstance {
5450
5465
  this.latestSuccessfulPageResults$ = this.state$.pipe(map(x => x.lastSuccessful), filterMaybe(), shareReplay(1));
5451
5466
  this.currentState$ = this.currentPageResultState$.pipe(mapItemPageLoadingStateFromResultPageLoadingState(), shareReplay(1));
5452
5467
  this.latestLoadedPage$ = this.latestPageResultState$.pipe(map(x => x.page), distinctUntilChanged(), shareReplay(1));
5453
- this.numberOfPagesLoaded$ = this.latestLoadedPage$.pipe(map(x => x + 1), shareReplay(1));
5468
+ this.numberOfPagesLoaded$ = this.latestLoadedPage$.pipe(map(x => x + 1), defaultIfEmpty(0), shareReplay(1));
5454
5469
  // MARK: ItemIteration
5455
5470
  /**
5456
5471
  * Whether or not there are more results to load.
5457
5472
  */
5458
- this.hasNext$ = this.hasReachedEndResult$.pipe(map(x => !x), shareReplay(1));
5473
+ this.hasNext$ = this.hasReachedEndResult$.pipe(map(x => !x), defaultIfEmpty(false), shareReplay(1));
5459
5474
  /**
5460
5475
  * Whether or not the successfulPageResultsCount has passed the maxPageLoadLimit
5461
5476
  */
@@ -5477,17 +5492,24 @@ class ItemPageIterationInstance {
5477
5492
  this._maxPageLoadLimit.next(limit);
5478
5493
  }
5479
5494
  nextPage(request = {}) {
5480
- return new Promise((resolve, reject) => {
5481
- this._nextFinished$.pipe(exhaustMap(() => this.latestPageResultState$), first()).subscribe({
5495
+ var _this = this;
5496
+ return new Promise(async function (resolve, reject) {
5497
+ _this._nextFinished$.pipe(exhaustMap(() => _this.latestPageResultState$), defaultIfEmpty({
5498
+ error: undefined,
5499
+ page: -1
5500
+ }), first()).subscribe({
5482
5501
  next: latestState => {
5483
5502
  if (latestState.error) {
5484
5503
  reject(latestState.error);
5485
5504
  } else {
5486
5505
  resolve(latestState.page);
5487
5506
  }
5507
+ },
5508
+ error: error => {
5509
+ reject(error);
5488
5510
  }
5489
5511
  });
5490
- this.next(request);
5512
+ _this.next(request);
5491
5513
  });
5492
5514
  }
5493
5515
  next(request = {}) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dereekb/rxjs",
3
- "version": "10.0.21",
3
+ "version": "10.0.23",
4
4
  "exports": {
5
5
  ".": {
6
6
  "types": "./src/index.d.ts",
@@ -1,6 +1,6 @@
1
1
  import { type Observable } from 'rxjs';
2
2
  import { type ItemIteration, type PageItemIteration } from './iteration';
3
- import { type Maybe, type GetterOrValue } from '@dereekb/util';
3
+ import { type Maybe, type GetterOrValue, type PageNumber } from '@dereekb/util';
4
4
  /**
5
5
  * Creates an observable from the input iteration that checks both the hasNext$ and canLoadMore$ states.
6
6
  *
@@ -29,4 +29,4 @@ export declare function iteratorNextPageUntilMaxPageLoadLimit(iterator: PageItem
29
29
  * @param page
30
30
  * @returns
31
31
  */
32
- export declare function iteratorNextPageUntilPage(iteration: PageItemIteration, page: GetterOrValue<number>): Promise<number>;
32
+ export declare function iteratorNextPageUntilPage(iteration: PageItemIteration, page: GetterOrValue<number>): Promise<PageNumber>;