瀏覽代碼

added isPromiseLike helper

- use instead of instanceof Promise
Alexander Rose 3 年之前
父節點
當前提交
17b25354f5

+ 2 - 1
src/mol-repr/structure/complex-visual.ts

@@ -35,6 +35,7 @@ import { StructureParams, StructureMeshParams, StructureTextParams, StructureDir
 import { Clipping } from '../../mol-theme/clipping';
 import { Clipping } from '../../mol-theme/clipping';
 import { TextureMesh } from '../../mol-geo/geometry/texture-mesh/texture-mesh';
 import { TextureMesh } from '../../mol-geo/geometry/texture-mesh/texture-mesh';
 import { WebGLContext } from '../../mol-gl/webgl/context';
 import { WebGLContext } from '../../mol-gl/webgl/context';
+import { isPromiseLike } from '../../mol-util/type-helpers';
 
 
 export interface  ComplexVisual<P extends StructureParams> extends Visual<Structure, P> { }
 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);
             prepareUpdate(theme, props, structure || currentStructure);
             if (updateState.createGeometry) {
             if (updateState.createGeometry) {
                 const newGeometry = createGeometry(ctx, newStructure, newTheme, newProps, geometry);
                 const newGeometry = createGeometry(ctx, newStructure, newTheme, newProps, geometry);
-                if (newGeometry instanceof Promise) {
+                if (isPromiseLike(newGeometry)) {
                     return newGeometry.then(g => {
                     return newGeometry.then(g => {
                         update(g);
                         update(g);
                         finalize(ctx);
                         finalize(ctx);

+ 2 - 1
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 { StructureParams, StructureMeshParams, StructureSpheresParams, StructurePointsParams, StructureLinesParams, StructureTextParams, StructureDirectVolumeParams, StructureTextureMeshParams, StructureCylindersParams } from './params';
 import { Clipping } from '../../mol-theme/clipping';
 import { Clipping } from '../../mol-theme/clipping';
 import { WebGLContext } from '../../mol-gl/webgl/context';
 import { WebGLContext } from '../../mol-gl/webgl/context';
+import { isPromiseLike } from '../../mol-util/type-helpers';
 
 
 export type StructureGroup = { structure: Structure, group: Unit.SymmetryGroup }
 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);
             prepareUpdate(theme, props, structureGroup || currentStructureGroup);
             if (updateState.createGeometry) {
             if (updateState.createGeometry) {
                 const newGeometry = _createGeometry(ctx, newStructureGroup.group.units[0], newStructureGroup.structure, newTheme, newProps, geometry);
                 const newGeometry = _createGeometry(ctx, newStructureGroup.group.units[0], newStructureGroup.structure, newTheme, newProps, geometry);
-                if (newGeometry instanceof Promise) {
+                if (isPromiseLike(newGeometry)) {
                     return newGeometry.then(g => {
                     return newGeometry.then(g => {
                         update(g);
                         update(g);
                         finalize(ctx);
                         finalize(ctx);

+ 2 - 1
src/mol-repr/volume/representation.ts

@@ -31,6 +31,7 @@ import { Task } from '../../mol-task';
 import { SizeValues } from '../../mol-gl/renderable/schema';
 import { SizeValues } from '../../mol-gl/renderable/schema';
 import { Clipping } from '../../mol-theme/clipping';
 import { Clipping } from '../../mol-theme/clipping';
 import { WebGLContext } from '../../mol-gl/webgl/context';
 import { WebGLContext } from '../../mol-gl/webgl/context';
+import { isPromiseLike } from '../../mol-util/type-helpers';
 
 
 export interface VolumeVisual<P extends VolumeParams> extends Visual<Volume, P> { }
 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);
             prepareUpdate(theme, props, volume || currentVolume);
             if (updateState.createGeometry) {
             if (updateState.createGeometry) {
                 const newGeometry = createGeometry(ctx, newVolume, newTheme, newProps, geometry);
                 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 {
             } else {
                 update();
                 update();
             }
             }

+ 4 - 0
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 {
 export function assertUnreachable(x: never): never {
     throw new Error('unreachable');
     throw new Error('unreachable');
+}
+
+export function isPromiseLike<T = any>(x: any): x is Promise<T> {
+    return typeof x?.then === 'function';
 }
 }