summaryrefslogtreecommitdiff
path: root/chromium/third_party/catapult/tracing/tracing/base/task.html
blob: 60ff686e8983db261ff42ae02f66a770c44e1572 (plain)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<!DOCTYPE html>
<!--
Copyright (c) 2013 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->
<link rel="import" href="/tracing/base/raf.html">
<link rel="import" href="/tracing/base/timing.html">

<script>
'use strict';

tr.exportTo('tr.b', function() {
  const Timing = tr.b.Timing;
  /**
   * A task is a combination of a run callback, a set of subtasks, and an after
   * task.
   *
   * When executed, a task does the following things:
   * 1. Runs its callback
   * 2. Runs its subtasks
   * 3. Runs its after callback.
   *
   * The list of subtasks and after task can be mutated inside step #1 but as
   * soon as the task's callback returns, the subtask list and after task is
   * fixed and cannot be changed again.
   *
   * Use task.after().after().after() to describe the toplevel passes that make
   * up your computation. Then, use subTasks to add detail to each subtask as it
   * runs. For example:
   *    var pieces = [];
   *    taskA = new Task(function() { pieces = getPieces(); });
   *    taskA.after(function(taskA) {
   *      pieces.forEach(function(piece) {
   *        taskA.subTask(function(taskB) { piece.process(); }, this);
   *      });
   *    });
   *
   * @constructor
   */
  function Task(runCb, thisArg) {
    if (runCb !== undefined && thisArg === undefined &&
        runCb.prototype !== undefined) {
      throw new Error('Almost certainly you meant to pass a bound callback ' +
          'or thisArg.');
    }
    this.runCb_ = runCb;
    this.thisArg_ = thisArg;
    this.afterTask_ = undefined;
    this.subTasks_ = [];
    this.updatesUi_ = false;
  }

  Task.prototype = {
    get name() {
      return this.runCb_.name;
    },

    /** Sets a hint for whether or not this task updates the UI. */
    set updatesUi(value) {
      this.updatesUi_ = value;
    },

    /*
     * See constructor documentation on semantics of subtasks.
     */
    subTask(cb, thisArg) {
      if (cb instanceof Task) {
        this.subTasks_.push(cb);
      } else {
        this.subTasks_.push(new Task(cb, thisArg));
      }
      return this.subTasks_[this.subTasks_.length - 1];
    },

    /**
     * Runs the current task and returns the task that should be executed next.
     */
    run() {
      if (this.runCb_ !== undefined) this.runCb_.call(this.thisArg_, this);
      const subTasks = this.subTasks_;
      this.subTasks_ = undefined; // Prevent more subTasks from being posted.

      if (!subTasks.length) return this.afterTask_;

      // If there are subtasks, then we want to execute all the subtasks and
      // then this task's afterTask. To make this happen, we update the
      // afterTask of all the subtasks so the point upward to each other, e.g.
      // subTask[0].afterTask to subTask[1] and so on. Then, the last subTask's
      // afterTask points at this task's afterTask.
      for (let i = 1; i < subTasks.length; i++) {
        subTasks[i - 1].afterTask_ = subTasks[i];
      }
      subTasks[subTasks.length - 1].afterTask_ = this.afterTask_;
      return subTasks[0];
    },

    /*
     * See constructor documentation on semantics of after tasks.
     */
    after(cb, thisArg) {
      if (this.afterTask_) {
        throw new Error('Has an after task already');
      }
      if (cb instanceof Task) {
        this.afterTask_ = cb;
      } else {
        this.afterTask_ = new Task(cb, thisArg);
      }
      return this.afterTask_;
    },

    /*
     * Adds a task after the chain of tasks.
     */
    enqueue(cb, thisArg) {
      if (!this.afterTask_) return this.after(cb, thisArg);
      return this.afterTask_.enqueue(cb, thisArg);
    }
  };

  Task.RunSynchronously = function(task) {
    let curTask = task;
    while (curTask) {
      curTask = curTask.run();
    }
  };

  /**
   * Runs a task using raf.requestIdleCallback, returning
   * a promise for its completion.
   */
  Task.RunWhenIdle = function(task) {
    return new Promise(function(resolve, reject) {
      let curTask = task;
      function runAnother() {
        try {
          curTask = curTask.run();
        } catch (e) {
          reject(e);
          return;
        }

        if (curTask) {
          if (curTask.updatesUi_) {
            tr.b.requestAnimationFrameInThisFrameIfPossible(runAnother);
          } else {
            tr.b.requestIdleCallback(runAnother);
          }
          return;
        }

        resolve();
      }
      tr.b.requestIdleCallback(runAnother);
    });
  };

  return {
    Task,
  };
});
</script>