mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2025-12-11 08:46:16 +00:00
fix: observable immediately returns false if scheduled beyond max delay
Closes #229
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import {Pipe, PipeTransform} from '@angular/core';
|
||||
import {SCRange, isInRange, SCISO8601DateRange} from '@openstapps/core';
|
||||
import {NormalizedInterval, differenceInMilliseconds, interval, isEqual} from 'date-fns';
|
||||
import {NormalizedInterval, differenceInMilliseconds, interval} from 'date-fns';
|
||||
import {EMPTY, Observable, SchedulerLike, asyncScheduler, concat, defer, map, of, timer} from 'rxjs';
|
||||
|
||||
@Pipe({
|
||||
@@ -16,6 +16,8 @@ export class InRangePipe implements PipeTransform {
|
||||
|
||||
export const MIN_DATE = new Date(0);
|
||||
export const MAX_DATE = new Date(1e15);
|
||||
// Maximum safe delay for JavaScript timers (~24.8 days)
|
||||
export const MAX_DELAY = 2 ** 31 - 1;
|
||||
|
||||
@Pipe({
|
||||
name: 'rangeToDateInterval',
|
||||
@@ -43,7 +45,7 @@ export function isWithinIntervalObservable(
|
||||
return concat(
|
||||
of(activate <= 0 && deactivate > 0),
|
||||
activate <= 0 ? EMPTY : timer(value.start, scheduler).pipe(map(() => true)),
|
||||
isEqual(value.end, MAX_DATE) || deactivate <= 0
|
||||
differenceInMilliseconds(value.end, now) >= MAX_DELAY || deactivate <= 0
|
||||
? EMPTY
|
||||
: timer(value.end, scheduler).pipe(map(() => false)),
|
||||
);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import {TestScheduler} from 'rxjs/testing';
|
||||
import {MAX_DATE, MIN_DATE, isWithinIntervalObservable} from './in-range.pipe';
|
||||
import {interval} from 'date-fns';
|
||||
import {MAX_DELAY} from './in-range.pipe';
|
||||
|
||||
/**
|
||||
* Test macro
|
||||
@@ -39,4 +40,7 @@ describe('isWithinIntervalObservable', () => {
|
||||
test([500, 1000], '499ms ^', '499ms f t 499ms (f|)');
|
||||
|
||||
test([500, 1000], '^ 750ms !', 'f 499ms t');
|
||||
|
||||
// Long interval test case: emit `true` and then complete (EMPTY) because `end` is beyond the delay limit
|
||||
test([500, 500 + MAX_DELAY + 2000], '1s ^', '1s (t|)');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user