 
     window.onresize = () -> panel.update()
 
-    amplify.subscribe 'outline-changed', (source) ->
-        if source isnt 'doc' and expose.doc and expose.outline
+    amplify.subscribe 'outline-changed', (generation, source) ->
+        # console.log 'outline-changed', generation, source
+        if not doc.isClean(generation)
             expose.doc.setValue(outline.serialize())
-        if source isnt 'outline' and expose.doc and expose.outline
+        if generation > expose.generation and expose.doc and expose.outline
             setOutline(expose.doc.getValue())
 
 window.onload = main


# helper method to maintain sync between console and editor
expose.setOutline = (contents) ->
    expose.outline = new birch.Outline.createTaskPaperOutline(contents)
    expose.outline.onDidEndChanges () ->
        amplify.publish 'outline-changed', 'outline'

#
# The main application entry point.

        gutters: ['CodeMirror-linenumbers', 'CodeMirror-foldgutter']
        tabSize: 4
    })

    expose.doc = cmTaskpaper.editor.doc
    expose.generation = expose.doc.changeGeneration()
    expose.doc.on 'change', () =>
        amplify.publish 'outline-changed', doc.changeGeneration(), 'doc.change'

    cmTaskpaper.loadTarget './todo.taskpaper', () ->
        contents = doc.getValue()
        expose.generation = expose.doc.changeGeneration()
        setOutline(contents)

    cmTaskpaper.title.text = 'CodeMirror View'

