 
   #  private functions
 
+  _newUserFunctionSubject: (name, initialValue) ->
+    subj = new Rx.BehaviorSubject(initialValue)
+    subj.valueChangesSub = subj.distinctUntilChanged().subscribe (value) =>
+      @valueChanges.onNext [name, value]
+    subj
+
   _recalculate: ->
     for name, subj of @userFunctionSubjects
       subj.onNext @_sheetValue name


  _userFunctionSubject: (name) -> @userFunctionSubjects[name]
  _unknownUserFunctionSubject: (name) -> (@userFunctionSubjects[name] = @_newUserFunctionSubject(name, new CalculationError(name, "Unknown name")))

  _newUserFunctionSubject: (name, initialValue) ->
    subj = new Rx.BehaviorSubject(initialValue)
    subj.valueChangesSub = subj.subscribe (value) =>
      @valueChanges.onNext [name, value]
    subj

  _userFunctionStream: (func, theFunction, functionNames) ->
    if _.includes(functionNames, func.name) then return new Rx.BehaviorSubject( new CalculationError func.name, 'Formula uses itself')

    @userFunctionImpls[name] = functionImpl
    @sheet[name] = functionImpl.theFunction

    subj = @userFunctionSubjects[name] or (@userFunctionSubjects[name] = @_newUserFunctionSubject(name, @_sheetValue name))
    #    subj.sourceSub?.dispose()
    #    source = @_userFunctionStream funcDef, functionImpl.theFunction, functionImpl.functionNames
    #    subj.sourceSub = source.subscribe subj

    @_recalculate()
#    if not subj.observeStream then subj.observeStream = subj.observeOn Rx.Scheduler.timeout
