         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)
-
-    expose.doc = cmTaskpaper.editor.doc
-    expose.doc.on 'change', () =>
-        amplify.publish 'outline-changed', 'doc'
 
     cmTaskpaper.title.text = 'CodeMirror View'
 


# 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.


    window.onresize = () -> panel.update()

    amplify.subscribe 'outline-changed', (generation, source) ->
        # console.log 'outline-changed', generation, source
        if not doc.isClean(generation)
            expose.doc.setValue(outline.serialize())
        if generation > expose.generation and expose.doc and expose.outline
            setOutline(expose.doc.getValue())

window.onload = main
