Преглед изворни кода

refactor batcher into singleton

Evan You пре 11 година
родитељ
комит
b2ca72a66b
4 измењених фајлова са 69 додато и 76 уклоњено
  1. 47 71
      src/batcher.js
  2. 1 3
      src/watcher.js
  3. 1 2
      test/unit/specs/batcher_spec.js
  4. 20 0
      test/unit/specs/util/env_spec.js

+ 47 - 71
src/batcher.js

@@ -1,46 +1,52 @@
 var _ = require('./util')
 
+// we have two separate queues: one for directive updates
+// and one for user watcher registered via $watch().
+// we want to guarantee directive updates to be called
+// before user watchers so that when user watchers are
+// triggered, the DOM would have already been in updated
+// state.
+var queue = []
+var userQueue = []
+var has = {}
+var waiting = false
+var flushing = false
+
 /**
- * The Batcher maintains a job queue to be run
- * async on the next event loop. A "job" can be any object
- * that implements the following interface:
- * 
- *   {
- *     id: {Number} - optional
- *     run: {Function}
- *     user: {Boolean} - optional
- *   }
- *
- * The `id` property is used to prevent duplication of jobs,
- * while jobs with `user:true` need to be processed after
- * all internal jobs have been processed first.
- *
- * In most cases a job will actually be a Watcher instance
- * which implements the above interface.
+ * Reset the batcher's state.
  */
 
-function Batcher () {
-  this.reset()
+function reset () {
+  queue = []
+  userQueue = []
+  has = {}
+  waiting = false
+  flushing = false
 }
 
-var p = Batcher.prototype
+/**
+ * Flush both queues and run the jobs.
+ */
+
+function flush () {
+  flushing = true
+  run(queue)
+  run(userQueue)
+  reset()
+}
 
 /**
- * Reset the batcher's state.
+ * Run the jobs in a single queue.
+ *
+ * @param {Array} queue
  */
 
-p.reset = function () {
-  this.has = {}
-  // we have two separate queues: one for directive updates
-  // and one for user watcher registered via $watch().
-  // we want to guarantee directive updates to be called
-  // before user watchers so that when user watchers are
-  // triggered, the DOM would have already been in updated
-  // state.
-  this.queue = []
-  this.userQueue = []
-  this.waiting = false
-  this.flushing = false
+function run (queue) {
+  // do not cache length because more jobs might be pushed
+  // as we run existing jobs
+  for (var i = 0; i < queue.length; i++) {
+    queue[i].run()
+  }
 }
 
 /**
@@ -54,51 +60,21 @@ p.reset = function () {
  *   - {Function}      run
  */
 
-p.push = function (job) {
-  if (!job.id || !this.has[job.id] || this.flushing) {
+exports.push = function (job) {
+  if (!job.id || !has[job.id] || flushing) {
     // A user watcher callback could trigger another
     // directive update during the flushing; at that time
     // the directive queue would already have been run, so
     // we call that update immediately as it is pushed.
-    if (this.flushing && !job.user) {
+    if (flushing && !job.user) {
       job.run()
       return
     }
-    var queue = job.user
-      ? this.userQueue
-      : this.queue
-    queue.push(job)
-    this.has[job.id] = job
-    if (!this.waiting) {
-      this.waiting = true
-      _.nextTick(this.flush, this)
+    ;(job.user ? userQueue : queue).push(job)
+    has[job.id] = job
+    if (!waiting) {
+      waiting = true
+      _.nextTick(flush)
     }
   }
-}
-
-/**
- * Flush both queues and run the jobs.
- */
-
-p.flush = function () {
-  this.flushing = true
-  run(this.queue)
-  run(this.userQueue)
-  this.reset()
-}
-
-/**
- * Run the jobs in a single queue.
- *
- * @param {Array} queue
- */
-
-function run (queue) {
-  // do not cache length because more jobs might be pushed
-  // as we run existing jobs
-  for (var i = 0; i < queue.length; i++) {
-    queue[i].run()
-  }
-}
-
-module.exports = Batcher
+}

+ 1 - 3
src/watcher.js

@@ -2,9 +2,7 @@ var _ = require('./util')
 var config = require('./config')
 var Observer = require('./observer')
 var expParser = require('./parsers/expression')
-var Batcher = require('./batcher')
-
-var batcher = new Batcher()
+var batcher = require('./batcher')
 var uid = 0
 
 /**

+ 1 - 2
test/unit/specs/batcher_spec.js

@@ -1,9 +1,8 @@
-var Batcher = require('../../../src/batcher')
+var batcher = require('../../../src/batcher')
 var nextTick = require('../../../src/util').nextTick
 
 describe('Batcher', function () {
 
-  var batcher = new Batcher()
   var spy
 
   beforeEach(function () {

+ 20 - 0
test/unit/specs/util/env_spec.js

@@ -0,0 +1,20 @@
+var _ = require('../../../../src/util')
+
+describe('Util - Environment', function () {
+
+  describe('nextTick', function () {
+
+    it('should accept context', function (done) {
+      var ctx = {}
+      _.nextTick(function () {
+        this.id = 1
+      }, ctx)
+      _.nextTick(function () {
+        expect(ctx.id).toBe(1)
+        done()
+      })
+    })
+
+  })
+
+})