suite "Delaying", ()->
	suiteSetup(restartSandbox)
	test "Setting options.delay for a binding will delay the subscriber update using timeouts", (done)->
		@slow(150)
		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)
		
		setTimeout ()->
			expect(invokeCount, 'after too-short timeout').to.equal(0)
			expect(lastValue, 'after too-short timeout').to.equal(null)
			
			setTimeout ()->
				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)
				
				setTimeout ()->
					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++
					
					setTimeout ()->
						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++
						
						setTimeout ()->
							expect(invokeCount, 'after final timeout').to.equal(6)
							expect(lastValue, 'after final timeout').to.equal(8)
							done()
						, 5
					, 5
				, 5
			, 30
		, 5
	
	

	test "Delayed updates are set for each binding individually", (done)->
		@slow(150)
		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)

		setTimeout ()->
			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)
			setTimeout ()->
				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)
				done()
			, 35
		, 25
		SimplyBind.defaultOptions.updateOnBind = true
	

	test "Will remove existing delay if passed a falsy value", (done)->
		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)
		
		setTimeout ()->
			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)
			done()
		, 5


