diff --git a/common/util.js b/common/util.js index cf336f90ccab73b235a1e6a03089303600e19810..5f01f394ae6301fcca029647c3fef775b740bf74 100644 --- a/common/util.js +++ b/common/util.js @@ -142,7 +142,7 @@ throw new Error(`fn failed ${retries} times. Aborting.`) } - exports.race = async (fn, { timeout = defaultConfig.timeout }) => { + exports.race = async (fn, { timeout = defaultConfig.timeout } = {}) => { return Promise.race([ fn(), new Promise((_rs, rj) => setTimeout(rj, timeout, `timed out: ${timeout}`)) diff --git a/common/util.spec.js b/common/util.spec.js index b817b6607331d06fdb9373c20d70e1d4eb90e08a..11eb7c395bb44e7758b8a99556d9e7262defde85 100644 --- a/common/util.spec.js +++ b/common/util.spec.js @@ -1,4 +1,4 @@ -import { getIdFromFullId, strToRgb, verifyPositionArg, arrayOrderedEql } from './util' +import { race, getIdFromFullId, strToRgb, verifyPositionArg, arrayOrderedEql } from './util' describe('common/util.js', () => { describe('getIdFromFullId', () => { @@ -150,4 +150,56 @@ describe('common/util.js', () => { }) }) }) + + describe("> race", () => { + const defaultTimeout = 1000 + describe("> without argument", () => { + it('> resolve should work', async () => { + await race(async () => { + await new Promise(rs => setTimeout(rs, 160, 'hello')) + }) + expect(true).toEqual(true) + }), + it('> reject should work', async () => { + const start = performance.now() + try{ + await race(async () => { + await new Promise(rs => setTimeout(rs, 5000, 'hello')) + }) + expect(true).toEqual(false) + } catch (e) { + + } finally { + const end = performance.now() + expect(end - start).toBeGreaterThan(defaultTimeout) + expect(end - start).toBeLessThan(defaultTimeout + 10) + } + }) + }) + + describe("> with argument", () => { + const timeout = 500 + it('> resolve should work', async () => { + await race(async () => { + await new Promise(rs => setTimeout(rs, 160, 'hello')) + }, { timeout }) + expect(true).toEqual(true) + }), + it('> reject should work', async () => { + const start = performance.now() + try{ + await race(async () => { + await new Promise(rs => setTimeout(rs, 1000, 'hello')) + }, { timeout } ) + expect(true).toEqual(false) + } catch (e) { + + } finally { + const end = performance.now() + expect(end - start).toBeGreaterThan(timeout) + expect(end - start).toBeLessThan(timeout + 10) + } + }) + }) + }) }) diff --git a/src/viewerModule/viewerCmp/viewerCmp.component.ts b/src/viewerModule/viewerCmp/viewerCmp.component.ts index e57f4ecbbf70f00de09d3f766fe945cef65cb9e4..abd782459a47ffa29451408135cb42153fb86d55 100644 --- a/src/viewerModule/viewerCmp/viewerCmp.component.ts +++ b/src/viewerModule/viewerCmp/viewerCmp.component.ts @@ -1,7 +1,7 @@ import { ChangeDetectionStrategy, ChangeDetectorRef, Component, ComponentFactory, ComponentFactoryResolver, Inject, Injector, Input, OnDestroy, Optional, TemplateRef, ViewChild, ViewContainerRef } from "@angular/core"; import { select, Store } from "@ngrx/store"; import { combineLatest, merge, NEVER, Observable, of, Subscription } from "rxjs"; -import {catchError, debounceTime, distinctUntilChanged, map, shareReplay, startWith, switchMap, mapTo } from "rxjs/operators"; +import {catchError, debounceTime, distinctUntilChanged, map, shareReplay, startWith, switchMap, mapTo, filter } from "rxjs/operators"; import { viewerStateSetSelectedRegions } from "src/services/state/viewerState/actions"; import { viewerStateContextedSelectedRegionsSelector, @@ -207,16 +207,14 @@ export class ViewerCmp implements OnDestroy { }) ) || NEVER, this._1umVoi$.pipe( - map(flag => flag - ? ({ - title: this._1umTitle, - description: this._1umDesc, - url: this._1umLink - ? [{ doi: this._1umLink }] - : [] - }) - : null - ) + filter(flag => flag), + map(() => ({ + title: this._1umTitle, + description: this._1umDesc, + url: this._1umLink + ? [{ doi: this._1umLink }] + : [] + })) ) )