summaryrefslogtreecommitdiffstats
path: root/lib/dom-batch.js
blob: 4671b80d3c69bafb10dc2f4988c296a9fd563f22 (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

/**
 * DOM-Batch
 *
 * Eliminates layout thrashing
 * by batching DOM read/write
 * interactions.
 *
 * @author Wilson Page <wilsonpage@me.com>
 */

;(function(){

  'use strict';

  // RequestAnimationFrame Polyfill
  var raf = window.requestAnimationFrame
    || window.webkitRequestAnimationFrame
    || window.mozRequestAnimationFrame
    || function(cb) { window.setTimeout(cb, 1000 / 60); };

  /**
   * Creates a new
   * DomBatch instance.
   *
   * (you should only have one
   * instance per application).
   *
   * @constructor
   */
  function DomBatch() {
    this.reads = [];
    this.writes = [];
    this.mode = null;
    this.pending = false;
  }

  /**
   * Adds a job to
   * the read queue.
   *
   * @param  {Function} fn
   * @api public
   */
  DomBatch.prototype.read = function(fn) {
    this.reads.push(fn);
    this.request('read');
  };

  /**
   * Adds a job to
   * the write queue.
   *
   * @param  {Function} fn
   * @api public
   */
  DomBatch.prototype.write = function(fn) {
    this.writes.push(fn);
    this.request('write');
  };

  /**
   * Makes the decision as to
   * whether a the frame needs
   * to be scheduled.
   *
   * @param  {String} type
   * @api private
   */
  DomBatch.prototype.request = function(type) {
    var self = this;

    // If we are currently writing, we don't
    // need to scedule a new frame as this
    // job will be emptied from the write queue
    if (this.mode === 'writing' && type === 'write') return;

    // If we are reading we don't need to schedule
    // a new frame as this read will be emptied
    // in the currently active read queue
    if (this.mode === 'reading' && type === 'read') return;

    // If we are reading we don't need to schedule
    // a new frame and this write job will be run
    // after the read queue has been emptied in the
    // currently active frame.
    if (this.mode === 'reading' && type === 'write') return;

    // If there is already a frame
    // scheduled, don't schedule another one
    if (this.pending) return;

    // Schedule frame (preserving context)
    raf(function() { self.frame(); });

    // Set flag to indicate
    // a frame has been scheduled
    this.pending = true;
  };

  /**
   * Calls each job in
   * the list passed.
   *
   * @param  {Array} list
   * @api private
   */
  DomBatch.prototype.run = function(list) {
    while (list.length) {
      list.shift().call(this);
    }
  };

  /**
   * Runs any read jobs followed
   * by any write jobs.
   *
   * @api private
   */
  DomBatch.prototype.frame = function() {

    // Set the pending flag to
    // false so that any new requests
    // that come in will schedule a new frame
    this.pending = false;

    // Set the mode to 'reading' so
    // that we know we can add more
    // reads to the queue instead of
    // scheduling a new frame.
    this.mode = 'reading';
    this.run(this.reads);

    // Set
    this.mode = 'writing';
    this.run(this.writes);

    this.mode = null;
  };

  /**
   * Expose 'DomBatch'
   */

  if (typeof exports === "object") {
    module.exports = DomBatch;
  } else if (typeof define === "function" && define.amd) {
    define(function(){ return DomBatch; });
  } else {
    window['DomBatch'] = DomBatch;
  }

})();