@danielkalen/simplybind
Version:
Magically simple, framework-less one-way/two-way data binding for frontend/backend in ~5kb.
87 lines (69 loc) • 2.39 kB
text/coffeescript
suite "Throttling", ()->
suiteSetup(restartSandbox)
test "Setting options.throttle for a binding will throttle the subscriber update frequency to once in a given timeframe", (done)->
invokeCount = 0
lastValue = null
dispatcher = 'prop':1
SimplyBind('prop', {throttle:15}).of(dispatcher)
.to (v)-> lastValue=v; invokeCount++
expect(invokeCount).to.equal(1)
expect(lastValue).to.equal(1)
dispatcher.prop = 2
dispatcher.prop = 3
dispatcher.prop = 4
dispatcher.prop = 5
expect(invokeCount).to.equal(1)
expect(lastValue).to.equal(1)
setTimeout ()->
expect(invokeCount).to.equal(2)
expect(lastValue).to.equal(5)
done()
, 25
test "Throttled updates are set for each binding individually", (done)->
invokeCount = {fastThrottle:0, slowThrottle:0, noThrottle:0}
dispatcher = 'prop':1
SimplyBind('prop', {throttle:10}).of(dispatcher).to ()-> invokeCount.fastThrottle++
SimplyBind('prop', {throttle:35}).of(dispatcher).to ()-> invokeCount.slowThrottle++
SimplyBind('prop').of(dispatcher).to ()-> invokeCount.noThrottle++
expect(invokeCount.fastThrottle).to.equal(1)
expect(invokeCount.slowThrottle).to.equal(1)
expect(invokeCount.noThrottle).to.equal(1)
dispatcher.prop = 2
dispatcher.prop = 3
dispatcher.prop = 4
dispatcher.prop = 5
expect(invokeCount.fastThrottle).to.equal(1)
expect(invokeCount.slowThrottle).to.equal(1)
expect(invokeCount.noThrottle).to.equal(5)
setTimeout ()->
expect(invokeCount.fastThrottle).to.equal(2)
expect(invokeCount.slowThrottle).to.equal(1)
expect(invokeCount.noThrottle).to.equal(5)
setTimeout ()->
expect(invokeCount.fastThrottle).to.equal(2)
expect(invokeCount.slowThrottle).to.equal(2)
expect(invokeCount.noThrottle).to.equal(5)
done()
, 35
, 25
test "Will remove existing throttle if passed a falsy value", ()->
invokeCount = 0
lastValue = null
dispatcher = 'prop':1
binding = SimplyBind('prop', throttle:15).of(dispatcher)
.to (v)-> lastValue=v; invokeCount++
dispatcher.prop = 2
dispatcher.prop = 3
dispatcher.prop = 4
dispatcher.prop = 5
expect(invokeCount).to.equal(1)
expect(lastValue).to.equal(1)
binding.setOption('throttle', 0)
dispatcher.prop = 2
dispatcher.prop = 3
dispatcher.prop = 4
dispatcher.prop = 5
expect(invokeCount).to.equal(5)
expect(lastValue).to.equal(5)