This commit is contained in:
Pierre-Yves Gérardy 2017-06-14 00:15:08 +02:00
parent 47d59ea68a
commit 0e0ed7c45d
5 changed files with 88 additions and 86 deletions

View file

@ -11,7 +11,6 @@ var apiRedraw = require("../../api/redraw")
var apiMounter = require("../../api/mount") var apiMounter = require("../../api/mount")
o.spec("mount", function() { o.spec("mount", function() {
var FRAME_BUDGET = Math.floor(1000 / 60)
var $window, root, redrawService, mount, render, throttleMock var $window, root, redrawService, mount, render, throttleMock
o.beforeEach(function() { o.beforeEach(function() {

View file

@ -166,6 +166,6 @@ o.spec("redrawService", function() {
o(spy1.callCount).equals(2) o(spy1.callCount).equals(2)
o(spy2.callCount).equals(2) o(spy2.callCount).equals(2)
o(spy3.callCount).equals(2) o(spy3.callCount).equals(2)
}) })
}) })

View file

@ -15,7 +15,6 @@ o.spec("route", function() {
void [{protocol: "http:", hostname: "localhost"}, {protocol: "file:", hostname: "/"}].forEach(function(env) { void [{protocol: "http:", hostname: "localhost"}, {protocol: "file:", hostname: "/"}].forEach(function(env) {
void ["#", "?", "", "#!", "?!", "/foo"].forEach(function(prefix) { void ["#", "?", "", "#!", "?!", "/foo"].forEach(function(prefix) {
o.spec("using prefix `" + prefix + "` starting on " + env.protocol + "//" + env.hostname, function() { o.spec("using prefix `" + prefix + "` starting on " + env.protocol + "//" + env.hostname, function() {
var FRAME_BUDGET = Math.floor(1000 / 60)
var $window, root, redrawService, route, throttleMock var $window, root, redrawService, route, throttleMock
o.beforeEach(function() { o.beforeEach(function() {
@ -552,12 +551,12 @@ o.spec("route", function() {
route(root, "/a", { route(root, "/a", {
"/a" : { "/a" : {
render: function() { render: function() {
return m("div", m('p')) return m("div", m("p"))
}, },
}, },
"/b" : { "/b" : {
render: function() { render: function() {
return m("div", m('a')) return m("div", m("a"))
}, },
}, },
}) })
@ -840,7 +839,7 @@ o.spec("route", function() {
callAsync(function() { callAsync(function() {
throttleMock.fire() throttleMock.fire()
route.set('/b') route.set("/b")
callAsync(function() { callAsync(function() {
callAsync(function() { callAsync(function() {
callAsync(function() { callAsync(function() {
@ -1110,7 +1109,7 @@ o.spec("route", function() {
o(root.firstChild.nodeName).equals("B") o(root.firstChild.nodeName).equals("B")
route.set('/a') route.set("/a")
callAsync(function() { callAsync(function() {
throttleMock.fire() throttleMock.fire()

View file

@ -1,89 +1,91 @@
"use strict"
var o = require("../../ospec/ospec") var o = require("../../ospec/ospec")
var throttleMocker = require("../../test-utils/throttleMock") var throttleMocker = require("../../test-utils/throttleMock")
o.spec("throttleMock", function() { o.spec("throttleMock", function() {
o("works with one callback", function() { o("works with one callback", function() {
var throttleMock = throttleMocker() var throttleMock = throttleMocker()
var spy = o.spy() var spy = o.spy()
o(throttleMock.queueLength()).equals(0) o(throttleMock.queueLength()).equals(0)
var throttled = throttleMock.throttle(spy) var throttled = throttleMock.throttle(spy)
o(throttleMock.queueLength()).equals(0) o(throttleMock.queueLength()).equals(0)
o(spy.callCount).equals(0) o(spy.callCount).equals(0)
throttled() throttled()
o(throttleMock.queueLength()).equals(1) o(throttleMock.queueLength()).equals(1)
o(spy.callCount).equals(0) o(spy.callCount).equals(0)
throttled() throttled()
o(throttleMock.queueLength()).equals(1) o(throttleMock.queueLength()).equals(1)
o(spy.callCount).equals(0) o(spy.callCount).equals(0)
throttleMock.fire() throttleMock.fire()
o(throttleMock.queueLength()).equals(0) o(throttleMock.queueLength()).equals(0)
o(spy.callCount).equals(1) o(spy.callCount).equals(1)
throttleMock.fire() throttleMock.fire()
o(spy.callCount).equals(1) o(spy.callCount).equals(1)
}) })
o("works with two callbacks", function() { o("works with two callbacks", function() {
var throttleMock = throttleMocker() var throttleMock = throttleMocker()
var spy1 = o.spy() var spy1 = o.spy()
var spy2 = o.spy() var spy2 = o.spy()
o(throttleMock.queueLength()).equals(0) o(throttleMock.queueLength()).equals(0)
var throttled1 = throttleMock.throttle(spy1) var throttled1 = throttleMock.throttle(spy1)
o(throttleMock.queueLength()).equals(0) o(throttleMock.queueLength()).equals(0)
o(spy1.callCount).equals(0) o(spy1.callCount).equals(0)
o(spy2.callCount).equals(0) o(spy2.callCount).equals(0)
throttled1() throttled1()
o(throttleMock.queueLength()).equals(1) o(throttleMock.queueLength()).equals(1)
o(spy1.callCount).equals(0) o(spy1.callCount).equals(0)
o(spy2.callCount).equals(0) o(spy2.callCount).equals(0)
throttled1() throttled1()
o(throttleMock.queueLength()).equals(1) o(throttleMock.queueLength()).equals(1)
o(spy1.callCount).equals(0) o(spy1.callCount).equals(0)
o(spy2.callCount).equals(0) o(spy2.callCount).equals(0)
var throttled2 = throttleMock.throttle(spy2) var throttled2 = throttleMock.throttle(spy2)
o(throttleMock.queueLength()).equals(1) o(throttleMock.queueLength()).equals(1)
o(spy1.callCount).equals(0) o(spy1.callCount).equals(0)
o(spy2.callCount).equals(0) o(spy2.callCount).equals(0)
throttled2() throttled2()
o(throttleMock.queueLength()).equals(2) o(throttleMock.queueLength()).equals(2)
o(spy1.callCount).equals(0) o(spy1.callCount).equals(0)
o(spy2.callCount).equals(0) o(spy2.callCount).equals(0)
throttled2() throttled2()
o(throttleMock.queueLength()).equals(2) o(throttleMock.queueLength()).equals(2)
o(spy1.callCount).equals(0) o(spy1.callCount).equals(0)
o(spy2.callCount).equals(0) o(spy2.callCount).equals(0)
throttleMock.fire() throttleMock.fire()
o(throttleMock.queueLength()).equals(0) o(throttleMock.queueLength()).equals(0)
o(spy1.callCount).equals(1) o(spy1.callCount).equals(1)
o(spy2.callCount).equals(1) o(spy2.callCount).equals(1)
throttleMock.fire() throttleMock.fire()
o(spy1.callCount).equals(1) o(spy1.callCount).equals(1)
o(spy2.callCount).equals(1) o(spy2.callCount).equals(1)
}) })
}) })

View file

@ -1,25 +1,27 @@
"use strict"
module.exports = function() { module.exports = function() {
var queue = [] var queue = []
return { return {
throttle: function(fn) { throttle: function(fn) {
var pending = false var pending = false
return function() { return function() {
if (!pending) { if (!pending) {
queue.push(function(){ queue.push(function(){
pending = false pending = false
fn() fn()
}) })
pending = true pending = true
} }
} }
}, },
fire: function() { fire: function() {
var tasks = queue var tasks = queue
queue = [] queue = []
tasks.forEach(function(fn) {fn()}) tasks.forEach(function(fn) {fn()})
}, },
queueLength: function(){ queueLength: function(){
return queue.length return queue.length
} }
} }
} }