Skip to content
Snippets Groups Projects
Select Git revision
  • 89a35d9cb4c40c57a0e12ae6772fe5614c57b381
  • 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

algorithm.ts

Blame
  • async-queue.ts 1.20 KiB
    /**
     * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
     *
     * @author David Sehnal <david.sehnal@gmail.com>
     */
    
    import { arrayRemoveInPlace } from './array';
    import { Subject } from 'rxjs';
    
    export class AsyncQueue<T> {
        private queue: T[] = [];
        private signal = new Subject<{ v: T, stillPresent: boolean }>();
    
        get length() { return this.queue.length; }
    
        enqueue(v: T) {
            this.queue.push(v);
            if (this.queue.length === 1) return true;
            return this.waitFor(v);
        }
    
        handled(v: T) {
            arrayRemoveInPlace(this.queue, v);
            if (this.queue.length > 0) {
                this.signal.next({ v: this.queue[0], stillPresent: true });
            }
        }
    
        remove(v: T) {
            const rem = arrayRemoveInPlace(this.queue, v);
            if (rem)
                this.signal.next({ v, stillPresent: false });
            return rem;
        }
    
        private waitFor(t: T): Promise<boolean> {
            return new Promise(res => {
                const sub = this.signal.subscribe(({ v, stillPresent: removed }) => {
                    if (v === t) {
                        sub.unsubscribe();
                        res(removed);
                    }
                });
            });
        }
    }