From 262ad37d08769a2546e361aa4d6c468965046722 Mon Sep 17 00:00:00 2001 From: Xiao Gui <xgui3783@gmail.com> Date: Fri, 18 Mar 2022 08:05:19 +0100 Subject: [PATCH] bugfix: pli desc bugfix: race promise --- common/util.js | 2 +- common/util.spec.js | 54 ++++++++++++++++++- .../viewerCmp/viewerCmp.component.ts | 20 ++++--- 3 files changed, 63 insertions(+), 13 deletions(-) diff --git a/common/util.js b/common/util.js index cf336f90c..5f01f394a 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 b817b6607..11eb7c395 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 e57f4ecbb..abd782459 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 }] + : [] + })) ) ) -- GitLab