-# External code can't access this, and so won't be able to directly construct a Deps.Computation instance
+# External code can't access this, and so won't be able to directly construct a Tracker.Computation instance
 privateObj = {}
 nextId = 1
 afterFlushCallbacks = []
 queue = new Meteor._SynchronousQueue()
 
-_.extend Deps,
+_.extend Tracker,
 	currentComputationVar: new Meteor.EnvironmentVariable()
 	
 	flush: ->
 		if not queue.safeToRunTask()
-			throw new Error("Can't call Deps.flush while flushing, or inside Deps.autorun")
+			throw new Error("Can't call Tracker.flush while flushing, or inside Tracker.autorun")
 		
 		queue.drain()
 			

		if typeof f != "function"
			throw new Error("onInvalidate requires a function")
		
		f = Deps._makeNonreactive(Meteor.bindEnvironment(f, null, @))
		
		if @invalidated
			f()

			try
				f()
			catch e
				console.log "Exception from Tracker afterFlush function:", e.stack || e.message
	
	autorun: (f) ->
		c = new Tracker.Computation(f, Tracker.currentComputation, privateObj)
		
		if Tracker.active
			Tracker.onInvalidate -> c.stop()
			
		c
	
	nonreactive: (f) ->
		Tracker.currentComputationVar.withValue null, f
	
	_makeNonreactive: (f) ->
		if f.$isNonreactive
			return f
		result = (args...) ->
			Tracker.nonreactive =>
				f.apply(@, args)
		result.$isNonreactive = true
		result
	
	onInvalidate: (f) ->
		if not Tracker.active
			throw new Error("Tracker.onInvalidate requires a currentComputation")
			
		Tracker.currentComputation.onInvalidate(f)
		
	afterFlush: (f) ->
		afterFlushCallbacks.push(f)

# Compatibility with client-side Tracker
Object.defineProperties Tracker,
	currentComputation:
		get: ->
			Tracker.currentComputationVar.get()
	active:
		get: ->
			!!Tracker.currentComputationVar.get()

class Tracker.Computation
	constructor: (f, @_parent, p)->
		if p != privateObj
			throw new Error("Tracker.Computation constructor is private; use Tracker.autorun")
		
		@stopped = false
		@invalidated = false
