保誠-保戶業務員媒合平台
HelenHuang
2022-06-09 26a09f08cf1ed43c640879f23fdad56c5c9282f7
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"use strict";
 
/**
 * Queue for all the resources to be download except async scripts.
 * Async scripts have their own queue AsyncResourceQueue.
 */
module.exports = class ResourceQueue {
  constructor({ paused, asyncQueue } = {}) {
    this.paused = Boolean(paused);
    this._asyncQueue = asyncQueue;
  }
 
  getLastScript() {
    let head = this.tail;
 
    while (head) {
      if (head.isScript) {
        return head;
      }
      head = head.prev;
    }
 
    return null;
  }
 
  _moreScripts() {
    let found = false;
 
    let head = this.tail;
    while (head && !found) {
      found = head.isScript;
      head = head.prev;
    }
 
    return found;
  }
 
  _notify() {
    if (this._listener) {
      this._listener();
    }
  }
 
  setListener(listener) {
    this._listener = listener;
  }
 
  push(request, onLoad, onError, keepLast, element) {
    const isScript = element ? element.localName === "script" : false;
 
    if (!request) {
      if (isScript && !this._moreScripts()) {
        return onLoad();
      }
 
      request = Promise.resolve();
    }
    const q = this;
    const item = {
      isScript,
      err: null,
      element,
      fired: false,
      data: null,
      keepLast,
      prev: q.tail,
      check() {
        if (!q.paused && !this.prev && this.fired) {
          let promise;
 
          if (this.err && onError) {
            promise = onError(this.err);
          }
 
          if (!this.err && onLoad) {
            promise = onLoad(this.data);
          }
 
          Promise.resolve(promise)
            .then(() => {
              if (this.next) {
                this.next.prev = null;
                this.next.check();
              } else { // q.tail===this
                q.tail = null;
                q._notify();
              }
 
              this.finished = true;
 
              if (q._asyncQueue) {
                q._asyncQueue.notifyItem(this);
              }
            });
        }
      }
    };
    if (q.tail) {
      if (q.tail.keepLast) {
        // if the tail is the load event in document and we receive a new element to load
        // we should add this new request before the load event.
        if (q.tail.prev) {
          q.tail.prev.next = item;
        }
        item.prev = q.tail.prev;
        q.tail.prev = item;
        item.next = q.tail;
      } else {
        q.tail.next = item;
        q.tail = item;
      }
    } else {
      q.tail = item;
    }
    return request
      .then(data => {
        item.fired = 1;
        item.data = data;
        item.check();
      })
      .catch(err => {
        item.fired = true;
        item.err = err;
        item.check();
      });
  }
 
  resume() {
    if (!this.paused) {
      return;
    }
    this.paused = false;
 
    let head = this.tail;
    while (head && head.prev) {
      head = head.prev;
    }
    if (head) {
      head.check();
    }
  }
};