Skip to content
Snippets Groups Projects
Select Git revision
  • b1755604e29e7f28f8960f3ce62cceed5de5aa67
  • master default protected
  • rednatco-v2
  • rednatco
  • test
  • ntc-tube-uniform-color
  • ntc-tube-missing-atoms
  • restore-vertex-array-per-program
  • watlas2
  • dnatco_new
  • cleanup-old-nodejs
  • webmmb
  • fix_auth_seq_id
  • update_deps
  • ext_dev
  • ntc_balls
  • nci-2
  • plugin
  • bugfix-0.4.5
  • nci
  • servers
  • v0.5.0-dev.1
  • v0.4.5
  • v0.4.4
  • v0.4.3
  • v0.4.2
  • v0.4.1
  • v0.4.0
  • v0.3.12
  • v0.3.11
  • v0.3.10
  • v0.3.9
  • v0.3.8
  • v0.3.7
  • v0.3.6
  • v0.3.5
  • v0.3.4
  • v0.3.3
  • v0.3.2
  • v0.3.1
  • v0.3.0
41 results

renderer.ts

Blame
  • context.ts 4.25 KiB
    /**
     * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
     *
     * @author David Sehnal <david.sehnal@gmail.com>
     */
    
    import { Structure, StructureElement, Unit } from '../structure';
    import { now } from '../../../mol-util/now';
    import { ElementIndex } from '../model';
    import { Link } from '../structure/unit/links';
    import { LinkType } from '../model/types';
    import { StructureSelection } from './selection';
    
    export interface QueryContextView {
        readonly element: StructureElement.Location;
        readonly currentStructure: Structure;
    }
    
    export class QueryContext implements QueryContextView {
        private currentElementStack: StructureElement.Location[] = [];
        private currentAtomicLinkStack: QueryContextLinkInfo<Unit.Atomic>[] = [];
        private currentStructureStack: Structure[] = [];
        private inputStructureStack: Structure[] = [];
    
        private timeCreated = now();
    
        readonly timeoutMs: number;
        readonly inputStructure: Structure;
    
        /** Current element */
        readonly element = StructureElement.Location.create();
        currentStructure: Structure = void 0 as any;
    
        /** Current link between atoms */
        readonly atomicLink = QueryContextLinkInfo.empty<Unit.Atomic>();
    
        /** Supply this from the outside. Used by the internal.generator.current symbol */
        currentSelection: StructureSelection | undefined = void 0;
    
        setElement(unit: Unit, e: ElementIndex) {
            this.element.unit = unit;
            this.element.element = e;
        }
    
        pushCurrentElement(): StructureElement.Location {
            this.currentElementStack[this.currentElementStack.length] = this.element;
            (this.element as StructureElement.Location) = StructureElement.Location.create();
            return this.element;
        }
    
        popCurrentElement() {
            (this.element as StructureElement.Location) = this.currentElementStack.pop()!;
        }
    
        pushCurrentLink() {
            if (this.atomicLink) this.currentAtomicLinkStack.push(this.atomicLink);
            (this.atomicLink as QueryContextLinkInfo<Unit.Atomic>) = QueryContextLinkInfo.empty();
            return this.atomicLink;
        }
    
        popCurrentLink() {
            if (this.currentAtomicLinkStack.length > 0) {
                (this.atomicLink as QueryContextLinkInfo<Unit.Atomic>) = this.currentAtomicLinkStack.pop()!;
            } else {
                (this.atomicLink as any) = void 0;
            }
        }
    
        pushCurrentStructure() {
            if (this.currentStructure) this.currentStructureStack.push(this.currentStructure);
        }
    
        popCurrentStructure() {
            if (this.currentStructureStack.length) (this.currentStructure as Structure) = this.currentStructureStack.pop()!;
            else (this.currentStructure as Structure) = void 0 as any;
        }
    
        pushInputStructure(structure: Structure) {
            this.inputStructureStack.push(this.inputStructure);
            (this.inputStructure as any) = structure;
        }
    
        popInputStructure() {
            if (this.inputStructureStack.length === 0) throw new Error('Must push before pop.');
            (this.inputStructure as any) = this.inputStructureStack.pop();
        }
    
        throwIfTimedOut() {
            if (this.timeoutMs === 0) return;
            if (now() - this.timeCreated > this.timeoutMs) {
                throw new Error(`The query took too long to execute (> ${this.timeoutMs / 1000}s).`);
            }
        }
    
        tryGetCurrentSelection() {
            if (!this.currentSelection) throw new Error('The current selection is not assigned.');
            return this.currentSelection;
        }
    
        constructor(structure: Structure, options?: QueryContextOptions) {
            this.inputStructure = structure;
            this.timeoutMs = (options && options.timeoutMs) || 0;
            this.currentSelection = options && options.currentSelection;
        }
    }
    
    export interface QueryContextOptions {
        timeoutMs?: number,
        currentSelection?: StructureSelection
    }
    
    export interface QueryPredicate { (ctx: QueryContext): boolean }
    export interface QueryFn<T = any> { (ctx: QueryContext): T }
    
    export interface QueryContextLinkInfo<U extends Unit = Unit> {
        link: Link.Location<U>,
        type: LinkType,
        order: number
    }
    
    export namespace QueryContextLinkInfo {
        export function empty<U extends Unit = Unit>(): QueryContextLinkInfo<U> {
            return { link: Link.Location() as Link.Location<U>, type: LinkType.Flag.None, order: 0 };
        }
    }