From 17b25354f5cb45fe2084d6e02a28728db67c2330 Mon Sep 17 00:00:00 2001 From: Alexander Rose <alexander.rose@weirdbyte.de> Date: Sat, 15 May 2021 11:16:32 -0700 Subject: [PATCH] added isPromiseLike helper - use instead of instanceof Promise --- src/mol-repr/structure/complex-visual.ts | 3 ++- src/mol-repr/structure/units-visual.ts | 3 ++- src/mol-repr/volume/representation.ts | 3 ++- src/mol-util/type-helpers.ts | 4 ++++ 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/mol-repr/structure/complex-visual.ts b/src/mol-repr/structure/complex-visual.ts index b803f9c9b..26cecd0a3 100644 --- a/src/mol-repr/structure/complex-visual.ts +++ b/src/mol-repr/structure/complex-visual.ts @@ -35,6 +35,7 @@ import { StructureParams, StructureMeshParams, StructureTextParams, StructureDir import { Clipping } from '../../mol-theme/clipping'; import { TextureMesh } from '../../mol-geo/geometry/texture-mesh/texture-mesh'; import { WebGLContext } from '../../mol-gl/webgl/context'; +import { isPromiseLike } from '../../mol-util/type-helpers'; export interface ComplexVisual<P extends StructureParams> extends Visual<Structure, P> { } @@ -213,7 +214,7 @@ export function ComplexVisual<G extends Geometry, P extends StructureParams & Ge prepareUpdate(theme, props, structure || currentStructure); if (updateState.createGeometry) { const newGeometry = createGeometry(ctx, newStructure, newTheme, newProps, geometry); - if (newGeometry instanceof Promise) { + if (isPromiseLike(newGeometry)) { return newGeometry.then(g => { update(g); finalize(ctx); diff --git a/src/mol-repr/structure/units-visual.ts b/src/mol-repr/structure/units-visual.ts index 513d3e084..1fb2795ac 100644 --- a/src/mol-repr/structure/units-visual.ts +++ b/src/mol-repr/structure/units-visual.ts @@ -39,6 +39,7 @@ import { SizeValues } from '../../mol-gl/renderable/schema'; import { StructureParams, StructureMeshParams, StructureSpheresParams, StructurePointsParams, StructureLinesParams, StructureTextParams, StructureDirectVolumeParams, StructureTextureMeshParams, StructureCylindersParams } from './params'; import { Clipping } from '../../mol-theme/clipping'; import { WebGLContext } from '../../mol-gl/webgl/context'; +import { isPromiseLike } from '../../mol-util/type-helpers'; export type StructureGroup = { structure: Structure, group: Unit.SymmetryGroup } @@ -269,7 +270,7 @@ export function UnitsVisual<G extends Geometry, P extends StructureParams & Geom prepareUpdate(theme, props, structureGroup || currentStructureGroup); if (updateState.createGeometry) { const newGeometry = _createGeometry(ctx, newStructureGroup.group.units[0], newStructureGroup.structure, newTheme, newProps, geometry); - if (newGeometry instanceof Promise) { + if (isPromiseLike(newGeometry)) { return newGeometry.then(g => { update(g); finalize(ctx); diff --git a/src/mol-repr/volume/representation.ts b/src/mol-repr/volume/representation.ts index a5edc4ba3..5591c176a 100644 --- a/src/mol-repr/volume/representation.ts +++ b/src/mol-repr/volume/representation.ts @@ -31,6 +31,7 @@ import { Task } from '../../mol-task'; import { SizeValues } from '../../mol-gl/renderable/schema'; import { Clipping } from '../../mol-theme/clipping'; import { WebGLContext } from '../../mol-gl/webgl/context'; +import { isPromiseLike } from '../../mol-util/type-helpers'; export interface VolumeVisual<P extends VolumeParams> extends Visual<Volume, P> { } @@ -173,7 +174,7 @@ export function VolumeVisual<G extends Geometry, P extends VolumeParams & Geomet prepareUpdate(theme, props, volume || currentVolume); if (updateState.createGeometry) { const newGeometry = createGeometry(ctx, newVolume, newTheme, newProps, geometry); - return newGeometry instanceof Promise ? newGeometry.then(update) : update(newGeometry); + return isPromiseLike(newGeometry) ? newGeometry.then(update) : update(newGeometry); } else { update(); } diff --git a/src/mol-util/type-helpers.ts b/src/mol-util/type-helpers.ts index c4e96eb46..d597aa4d2 100644 --- a/src/mol-util/type-helpers.ts +++ b/src/mol-util/type-helpers.ts @@ -29,4 +29,8 @@ export interface FiniteArray<T, L extends number = number> extends ReadonlyArray export function assertUnreachable(x: never): never { throw new Error('unreachable'); +} + +export function isPromiseLike<T = any>(x: any): x is Promise<T> { + return typeof x?.then === 'function'; } \ No newline at end of file -- GitLab