Blame view

node_modules/aes-decrypter/src/async-stream.js 952 Bytes
6a9ffbcc   liuqimichale   地图点击事件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  /**
   * @file async-stream.js
   */
  import Stream from './stream';
  
  /**
   * A wrapper around the Stream class to use setTiemout
   * and run stream "jobs" Asynchronously
   *
   * @class AsyncStream
   * @extends Stream
   */
  export default class AsyncStream extends Stream {
    constructor() {
      super(Stream);
      this.jobs = [];
      this.delay = 1;
      this.timeout_ = null;
    }
  
    /**
     * process an async job
     *
     * @private
     */
    processJob_() {
      this.jobs.shift()();
      if (this.jobs.length) {
        this.timeout_ = setTimeout(this.processJob_.bind(this),
                                   this.delay);
      } else {
        this.timeout_ = null;
      }
    }
  
    /**
     * push a job into the stream
     *
     * @param {Function} job the job to push into the stream
     */
    push(job) {
      this.jobs.push(job);
      if (!this.timeout_) {
        this.timeout_ = setTimeout(this.processJob_.bind(this),
                                   this.delay);
      }
    }
  }