suite "Delaying", ()->
	suiteSetup(restartSandbox)
	setup ()-> @clock = sinon.useFakeTimers()
	teardown ()-> @clock.restore()

	test "Setting options.delay for a binding will delay the subscriber update using timeouts", ()->
		invokeCount = 0
		lastValue = null
		dispatcher = 'prop':1

		Interface = SimplyBind('prop', {delay:15}).of(dispatcher)
			.to (v)-> lastValue=v; invokeCount++

		expect(invokeCount, 'post-binding').to.equal(0)
		expect(lastValue, 'post-binding').to.equal(null)
		
		@clock.tick(5)
		expect(invokeCount, 'after too-short timeout').to.equal(0)
		expect(lastValue, 'after too-short timeout').to.equal(null)
		
		@clock.tick(30)
		expect(invokeCount, 'after long enough timeout').to.equal(1)
		expect(lastValue, 'after long enough timeout').to.equal(1)
		
		Interface.setOption('delay', 1)
		dispatcher.prop++
		expect(invokeCount, 'after resetting delay to 1').to.equal(1)
		expect(lastValue, 'after resetting delay to 1').to.equal(1)
		
		@clock.tick(5)
		expect(invokeCount, 'after long enough timeout 2').to.equal(2)
		expect(lastValue, 'after long enough timeout 2').to.equal(2)
		dispatcher.prop++
		dispatcher.prop++
		dispatcher.prop++
		
		@clock.tick(5)
		expect(invokeCount, 'after final timeout').to.equal(3)
		expect(lastValue, 'after final timeout').to.equal(5)

		Interface.setOption('updateEvenIfSame', true)
		dispatcher.prop++
		dispatcher.prop++
		dispatcher.prop++
		

		@clock.tick(5)
		expect(invokeCount, 'after final timeout').to.equal(6)
		expect(lastValue, 'after final timeout').to.equal(8)
	
	

	test "Delayed updates are set for each binding individually", ()->
		SimplyBind.defaultOptions.updateOnBind = false
		invokeCount = {fastDelay:0, slowDelay:0, noDelay:0}
		dispatcher = 'prop':0

		SimplyBind('prop', {delay:10}).of(dispatcher).to ()-> invokeCount.fastDelay++
		SimplyBind('prop', {delay:35}).of(dispatcher).to ()-> invokeCount.slowDelay++
		SimplyBind('prop').of(dispatcher).to ()-> invokeCount.noDelay++

		expect(invokeCount.fastDelay, 'fastDelay (post-binding)').to.equal(0)
		expect(invokeCount.slowDelay, 'slowDelay (post-binding)').to.equal(0)
		expect(invokeCount.noDelay, 'noDelay (post-binding)').to.equal(0)

		dispatcher.prop++

		expect(invokeCount.fastDelay, 'fastDelay (after initial inc)').to.equal(0)
		expect(invokeCount.slowDelay, 'slowDelay (after initial inc)').to.equal(0)
		expect(invokeCount.noDelay, 'noDelay (after initial inc)').to.equal(1)

		@clock.tick(25)
		expect(invokeCount.fastDelay, 'fastDelay (after 25ms)').to.equal(1)
		expect(invokeCount.slowDelay, 'slowDelay (after 25ms)').to.equal(0)
		expect(invokeCount.noDelay, 'noDelay (after 25ms)').to.equal(1)

		@clock.tick(35)
		expect(invokeCount.fastDelay, 'fastDelay (after 35ms + 25ms)').to.equal(1)
		expect(invokeCount.slowDelay, 'slowDelay (after 35ms + 25ms)').to.equal(1)
		expect(invokeCount.noDelay, 'noDelay (after 35ms + 25ms)').to.equal(1)

		SimplyBind.defaultOptions.updateOnBind = true
	

	test "Will remove existing delay if passed a falsy value", ()->
		invokeCount = 0
		lastValue = null
		dispatcher = 'prop':0

		binding = SimplyBind('prop', delay:1, updateOnBind:false).of(dispatcher)
			.to (v)-> lastValue=v; invokeCount++

		expect(invokeCount).to.equal(0)
		expect(lastValue).to.equal(null)
		
		dispatcher.prop++
		expect(invokeCount).to.equal(0)
		expect(lastValue).to.equal(null)
		
		@clock.tick(5)
		expect(invokeCount).to.equal(1)
		expect(lastValue).to.equal(1)
		
		binding.setOption('delay', 0)
		dispatcher.prop++
		expect(invokeCount).to.equal(2)
		expect(lastValue).to.equal(2)
		
		dispatcher.prop++
		expect(invokeCount).to.equal(3)
		expect(lastValue).to.equal(3)


