• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import type { PendingCondition } from './PendingCondition';
17// Pending task queue
18export class PendingTask {
19  private tasks: Function[] = new Array<Function>();
20  private condition: PendingCondition;
21
22  constructor(condition: PendingCondition) {
23    if (condition == null) {
24      throw Error('PendingTask condition is null');
25    }
26    this.condition = condition;
27  }
28
29  public execute(task: Function): void {
30    if (task == null) {
31      return;
32    }
33    if (this.condition.shouldPending()) {
34      this.tasks.push(task)
35    } else {
36      task();
37    }
38  }
39
40  public flush(): void {
41    while (this.tasks.length > 0) {
42      if (this.condition.shouldPending()) {
43        return;
44      }
45      let task: Function = this.tasks.shift();
46      if (task != null) {
47        task();
48      }
49    }
50  }
51
52  public clear(): void {
53    this.tasks = [];
54  }
55}
56
57