   sendMessage: (obj) =>
     obj.id ||= "dart-tools-#{(@id++)}"
     msg = JSON.stringify(obj)
-    @process?.stdin?.write(msg + "\n")
+    if @writable
+      @process?.stdin?.write(msg + "\n")
 
     deferred = Q.defer()
     promiseMap[obj.id] = deferred

    cmd = path.join(sdkPath, "bin", "dart")
    Utils.whenDartSdkFound =>
      @process = spawn cmd, args
      @process.stdout.pipe(StreamSplitter("\n")).on 'token', @processMessage


      # Set analysis root.
      @sendMessage


module.exports =
class AnalysisServer extends Model
  FAILURE_HARD_STOP: 3
  FAILURE_HARD_STOP_TIMEOUT: 30
  analysisResults: []
  id: 1
  promiseMap = {}
  serverFailureCount: 0
  writable: false

  start: (packageRoot) =>
    promiseMap = {}
