pulsar/static/index.js

182 lines
5.4 KiB
JavaScript
Raw Normal View History

2015-08-22 02:56:32 +03:00
(function () {
var path = require('path')
2015-10-30 20:42:38 +03:00
var FileSystemBlobStore = require('../src/file-system-blob-store')
2015-10-30 13:36:58 +03:00
var NativeCompileCache = require('../src/native-compile-cache')
2015-08-22 02:56:32 +03:00
var loadSettings = null
var loadSettingsError = null
var blobStore = null
2015-02-13 00:31:49 +03:00
2015-08-22 02:56:32 +03:00
window.onload = function () {
try {
var startTime = Date.now()
2015-05-13 19:47:10 +03:00
2015-08-22 02:56:32 +03:00
process.on('unhandledRejection', function (error, promise) {
console.error('Unhandled promise rejection %o with error: %o', promise, error)
})
2015-08-22 02:56:32 +03:00
// Ensure ATOM_HOME is always set before anything else is required
// This is because of a difference in Linux not inherited between browser and render processes
// issue #5142
2015-11-04 01:31:35 +03:00
if (loadSettings && loadSettings.atomHome) {
process.env.ATOM_HOME = loadSettings.atomHome
}
blobStore = FileSystemBlobStore.load(
2015-10-30 20:42:38 +03:00
path.join(process.env.ATOM_HOME, 'blob-store/')
2015-10-27 16:03:00 +03:00
)
NativeCompileCache.setCacheStore(blobStore)
2015-10-27 16:03:00 +03:00
NativeCompileCache.install()
2015-08-22 02:56:32 +03:00
// Normalize to make sure drive letter case is consistent on Windows
process.resourcesPath = path.normalize(process.resourcesPath)
2015-02-13 00:31:49 +03:00
2015-08-22 02:56:32 +03:00
if (loadSettingsError) {
throw loadSettingsError
}
2015-08-22 02:56:32 +03:00
var devMode = loadSettings.devMode || !loadSettings.resourcePath.startsWith(process.resourcesPath + path.sep)
2015-05-13 19:47:10 +03:00
2015-08-22 02:56:32 +03:00
if (devMode) {
setupDeprecatedPackages()
}
2015-08-22 02:56:32 +03:00
if (loadSettings.profileStartup) {
profileStartup(loadSettings, Date.now() - startTime)
} else {
setupWindow(loadSettings)
setLoadTime(Date.now() - startTime)
}
} catch (error) {
handleSetupError(error)
2015-06-04 02:11:16 +03:00
}
2015-08-22 02:56:32 +03:00
}
2015-06-04 02:11:16 +03:00
2015-08-22 02:56:32 +03:00
function setLoadTime (loadTime) {
if (global.atom) {
global.atom.loadTime = loadTime
console.log('Window load time: ' + global.atom.getWindowLoadTime() + 'ms')
}
}
2015-02-13 00:31:49 +03:00
2015-08-22 02:56:32 +03:00
function handleSetupError (error) {
var currentWindow = require('remote').getCurrentWindow()
currentWindow.setSize(800, 600)
currentWindow.center()
currentWindow.show()
currentWindow.openDevTools()
console.error(error.stack || error)
2015-04-29 03:42:15 +03:00
}
2015-08-22 02:56:32 +03:00
function setupWindow (loadSettings) {
var CompileCache = require('../src/compile-cache')
CompileCache.setAtomHomeDirectory(loadSettings.atomHome)
2015-08-22 02:56:32 +03:00
var ModuleCache = require('../src/module-cache')
ModuleCache.register(loadSettings)
ModuleCache.add(loadSettings.resourcePath)
// Start the crash reporter before anything else.
require('crash-reporter').start({
productName: 'Atom',
companyName: 'GitHub',
// By explicitly passing the app version here, we could save the call
// of "require('remote').require('app').getVersion()".
extra: {_version: loadSettings.appVersion}
})
setupVmCompatibility()
setupCsonCache(CompileCache.getCacheDirectory())
var initialize = require(loadSettings.windowInitializationScript)
initialize({blobStore: blobStore})
2015-08-22 02:56:32 +03:00
require('ipc').sendChannel('window-command', 'window:loaded')
}
function setupCsonCache (cacheDir) {
require('season').setCacheDir(path.join(cacheDir, 'cson'))
}
2015-08-22 02:56:32 +03:00
function setupVmCompatibility () {
var vm = require('vm')
if (!vm.Script.createContext) {
vm.Script.createContext = vm.createContext
}
2015-05-13 19:50:11 +03:00
}
2015-04-28 04:13:35 +03:00
2015-08-22 02:56:32 +03:00
function setupDeprecatedPackages () {
var metadata = require('../package.json')
if (!metadata._deprecatedPackages) {
try {
metadata._deprecatedPackages = require('../build/deprecated-packages.json')
} catch(requireError) {
console.error('Failed to setup deprecated packages list', requireError.stack)
}
}
}
function profileStartup (loadSettings, initialTime) {
function profile () {
console.profile('startup')
try {
var startTime = Date.now()
setupWindow(loadSettings)
setLoadTime(Date.now() - startTime + initialTime)
} catch (error) {
handleSetupError(error)
} finally {
console.profileEnd('startup')
console.log('Switch to the Profiles tab to view the created startup profile')
}
}
var currentWindow = require('remote').getCurrentWindow()
if (currentWindow.devToolsWebContents) {
profile()
} else {
currentWindow.openDevTools()
currentWindow.once('devtools-opened', function () {
setTimeout(profile, 1000)
2015-08-22 02:56:32 +03:00
})
2015-06-04 02:11:16 +03:00
}
}
2015-08-22 02:56:32 +03:00
function parseLoadSettings () {
var rawLoadSettings = decodeURIComponent(window.location.hash.substr(1))
try {
2015-08-22 02:56:32 +03:00
loadSettings = JSON.parse(rawLoadSettings)
} catch (error) {
2015-08-22 02:56:32 +03:00
console.error('Failed to parse load settings: ' + rawLoadSettings)
loadSettingsError = error
}
2015-05-13 19:47:10 +03:00
}
2015-08-22 02:56:32 +03:00
function setupWindowBackground () {
if (loadSettings && loadSettings.isSpec) {
return
}
var backgroundColor = window.localStorage.getItem('atom:window-background-color')
if (!backgroundColor) {
return
}
var backgroundStylesheet = document.createElement('style')
backgroundStylesheet.type = 'text/css'
backgroundStylesheet.innerText = 'html, body { background: ' + backgroundColor + ' !important; }'
2015-08-22 02:56:32 +03:00
document.head.appendChild(backgroundStylesheet)
// Remove once the page loads
window.addEventListener('load', function loadWindow () {
window.removeEventListener('load', loadWindow, false)
setTimeout(function () {
backgroundStylesheet.remove()
backgroundStylesheet = null
}, 1000)
}, false)
2015-05-13 19:47:10 +03:00
}
2015-08-22 02:56:32 +03:00
parseLoadSettings()
setupWindowBackground()
})()