 }
 
 setUpEditor = (iframe) ->
-  editor = new Editor iframe, mutationObserverOptions, (mutations, node) ->
+  maybeEditor = new Editor iframe, mutationObserverOptions, (mutations, node) ->
     sync.getCurrentDocId (currentDocId) ->
       sync.getDoc currentDocId, (doc) ->
         doc.title    = inferTitleFrom(node) ? doc.title ? ""


    doc = {
      title:    inferTitleFrom(wrapperNode) ? ""
      chapters: inferChaptersFrom(wrapperNode)
    }

    sync.saveDocWithSnapshot doc, {html}, (doc, snapshot) ->


sync = new DreamSync()

# This will be initialized once the iframe has been added to the DOM.
maybeEditor = null

withEditor = (callback) ->
  if maybeEditor?
    callback maybeEditor
  else
    # If the editor isn't initialized yet, yield and try again until it's ready.
    setTimeout (-> withEditor callback), 0
