Skip to content
Snippets Groups Projects
Select Git revision
  • 3ff823a63e87941ee184b8e844d3c4a19f6b52d8
  • master default protected
  • rednatco-v2
  • base-pairs-ladder
  • 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
  • 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

computation.ts

Blame
  • computation.ts 9.26 KiB
    /**
     * Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
     *
     * Adapted from https://github.com/dsehnal/LiteMol
     * @author David Sehnal <david.sehnal@gmail.com>
     */
    
    import Scheduler from './scheduler'
    import timeNow from './time'
    
    interface Computation<A> {
        (ctx?: Computation.Context): Promise<A>
    }
    
    namespace Computation {
        export let PRINT_ERRORS_TO_CONSOLE = false;
    
        export function create<A>(computation: (ctx: Context) => Promise<A>) {
            return ComputationImpl(computation);
        }
    
        export function resolve<A>(a: A) {
            return create<A>(_ => Promise.resolve(a));
        }
    
        export function reject<A>(reason: any) {
            return create<A>(_ => Promise.reject(reason));
        }
    
        export interface Params {
            updateRateMs?: number,
            observer?: ProgressObserver
        }
    
        export const Aborted = 'Aborted';
    
        export interface Progress {
            message: string,
            isIndeterminate: boolean,
            current: number,
            max: number,
            elapsedMs: number,
            requestAbort?: () => void
        }
    
        export interface ProgressUpdate {
            message?: string,
            abort?: boolean | (() => void),
            current?: number,
            max?: number
        }
    
        export interface Context {
            readonly isSynchronous: boolean,
            /** Also checks if the computation was aborted. If so, throws. */
            readonly requiresUpdate: boolean,
            requestAbort(): void,
    
            subscribe(onProgress: ProgressObserver): { dispose: () => void },
            /** Also checks if the computation was aborted. If so, throws. */
            update(info: ProgressUpdate): Promise<void> | void
        }
    
        export type ProgressObserver = (progress: Readonly<Progress>) => void;
    
        const emptyDisposer = { dispose: () => { } }
    
        /** A context without updates. */
        export const synchronous: Context = {
            isSynchronous: true,