     e.preventDefault()
     shell.openExternal url
 
-  app.on 'will-quit', (e) ->
-    settings = storage.get()
-    settings['last-route'] = mainWindow.webContents.getURL()
-    storage.write settings
-
   # Emitted when the window is closed.
-  mainWindow.on 'closed', ->
+  mainWindow.on 'close', ->
     # Dereference the window object, usually you would store windows
     # in an array if your app supports multi windows, this is the time
     # when you should delete the corresponding element.
-    mainWindow = null
-
+    syncStorage ->
+      mainWindow = null

      preload         : NODE_REQUIRE
      nodeIntegration : no


  # Set application menu
  new ApplicationMenu

  # Start listening the web app
  new IPCReporter

  # Prepare AppStorage
  storage = new Storage template : STORAGE_TEMPLATE

  # and load the index.html of the app.
  mainWindow.loadURL storage.get()['last-route'] or ROOT_URL



module.exports = ->

  # Set application menu
  new ApplicationMenu

  # Start listening the web app
  new IPCReporter

  # Prepare AppStorage
  storage = new Storage template : STORAGE_TEMPLATE

  syncStorage = (callback = (->)) ->

    return  unless mainWindow

    settings = storage.get()
    settings['last-route'] = mainWindow.webContents.getURL()
    storage.write settings, callback

  # Sync storage on quit
  app.on 'will-quit', syncStorage

  # Create the browser window.
  mainWindow = new BrowserWindow
    width             : 1280
