pulsar/static/index.js

163 lines
5.0 KiB
JavaScript
Raw Normal View History

2019-05-31 19:33:56 +03:00
(function() {
// Define the window start time before the requires so we get a more accurate
// window:start marker.
2019-05-31 19:33:56 +03:00
const startWindowTime = Date.now();
2019-05-31 19:33:56 +03:00
const electron = require('electron');
const path = require('path');
const Module = require('module');
const getWindowLoadSettings = require('../src/get-window-load-settings');
const { getReleaseChannel } = require('../src/get-app-details.js');
2019-05-31 19:33:56 +03:00
const StartupTime = require('../src/startup-time');
const entryPointDirPath = __dirname;
let blobStore = null;
2017-02-27 16:47:20 +03:00
2019-05-31 19:33:56 +03:00
const startupMarkers = electron.remote.getCurrentWindow().startupMarkers;
if (startupMarkers) {
2019-05-31 19:33:56 +03:00
StartupTime.importData(startupMarkers);
}
2019-05-31 19:33:56 +03:00
StartupTime.addMarker('window:start', startWindowTime);
2023-03-20 18:03:25 +03:00
window.onload = async function() {
2017-02-27 16:47:20 +03:00
try {
2019-05-31 19:33:56 +03:00
StartupTime.addMarker('window:onload:start');
const startTime = Date.now();
2023-03-20 18:03:25 +03:00
await require('second-mate').ready
2017-02-27 16:47:20 +03:00
2019-05-31 19:33:56 +03:00
process.on('unhandledRejection', function(error, promise) {
console.error(
'Unhandled promise rejection %o with error: %o',
promise,
error
);
});
2017-02-27 16:47:20 +03:00
// Normalize to make sure drive letter case is consistent on Windows
2019-05-31 19:33:56 +03:00
process.resourcesPath = path.normalize(process.resourcesPath);
2017-02-27 16:47:20 +03:00
2019-05-31 19:33:56 +03:00
setupAtomHome();
const devMode =
getWindowLoadSettings().devMode ||
!getWindowLoadSettings().resourcePath.startsWith(
process.resourcesPath + path.sep
);
2022-11-10 11:00:10 +03:00
const FileSystemBlobStore = require('../src/file-system-blob-store');
2019-05-31 19:33:56 +03:00
blobStore = FileSystemBlobStore.load(
path.join(process.env.ATOM_HOME, 'blob-store')
);
2017-03-02 11:11:54 +03:00
2022-11-10 11:00:10 +03:00
const NativeCompileCache = require('../src/native-compile-cache');
2019-05-31 19:33:56 +03:00
NativeCompileCache.setCacheStore(blobStore);
NativeCompileCache.setV8Version(process.versions.v8);
NativeCompileCache.install();
2017-03-02 11:11:54 +03:00
2017-02-27 16:47:20 +03:00
if (getWindowLoadSettings().profileStartup) {
2019-05-31 19:33:56 +03:00
profileStartup(Date.now() - startTime);
2017-02-27 16:47:20 +03:00
} else {
2019-05-31 19:33:56 +03:00
StartupTime.addMarker('window:setup-window:start');
setupWindow().then(() => {
2019-05-31 19:33:56 +03:00
StartupTime.addMarker('window:setup-window:end');
});
setLoadTime(Date.now() - startTime);
2015-08-22 02:56:32 +03:00
}
2017-02-27 16:47:20 +03:00
} catch (error) {
2019-05-31 19:33:56 +03:00
handleSetupError(error);
2017-02-27 16:47:20 +03:00
}
2019-05-31 19:33:56 +03:00
StartupTime.addMarker('window:onload:end');
};
2017-02-27 16:47:20 +03:00
2019-05-31 19:33:56 +03:00
function setLoadTime(loadTime) {
2017-02-27 16:47:20 +03:00
if (global.atom) {
2019-05-31 19:33:56 +03:00
global.atom.loadTime = loadTime;
2017-02-27 16:47:20 +03:00
}
}
2019-05-31 19:33:56 +03:00
function handleSetupError(error) {
const currentWindow = electron.remote.getCurrentWindow();
currentWindow.setSize(800, 600);
currentWindow.center();
currentWindow.show();
currentWindow.openDevTools();
console.error(error.stack || error);
2017-02-27 16:47:20 +03:00
}
2019-05-31 19:33:56 +03:00
function setupWindow() {
2022-11-10 11:00:10 +03:00
const CompileCache = require('../src/compile-cache');
2019-05-31 19:33:56 +03:00
CompileCache.setAtomHomeDirectory(process.env.ATOM_HOME);
CompileCache.install(process.resourcesPath, require);
2022-11-10 11:00:10 +03:00
const ModuleCache = require('../src/module-cache');
2019-05-31 19:33:56 +03:00
ModuleCache.register(getWindowLoadSettings());
2022-11-10 11:00:10 +03:00
require('document-register-element');
2022-11-10 11:00:10 +03:00
const Grim = require('grim');
const documentRegisterElement = document.registerElement;
document.registerElement = (type, options) => {
Grim.deprecate(
'Use `customElements.define` instead of `document.registerElement` see https://javascript.info/custom-elements'
);
return documentRegisterElement(type, options);
};
const { userSettings, appVersion } = getWindowLoadSettings();
2019-05-31 19:33:56 +03:00
2022-11-10 11:00:10 +03:00
const CSON = require('season');
2019-05-31 19:33:56 +03:00
CSON.setCacheDir(path.join(CompileCache.getCacheDirectory(), 'cson'));
const initScriptPath = path.relative(
entryPointDirPath,
getWindowLoadSettings().windowInitializationScript
);
2022-11-10 11:00:10 +03:00
const initialize = require(initScriptPath);
2019-05-31 19:33:56 +03:00
StartupTime.addMarker('window:initialize:start');
return initialize({ blobStore: blobStore }).then(function() {
StartupTime.addMarker('window:initialize:end');
electron.ipcRenderer.send('window-command', 'window:loaded');
});
2017-02-27 16:47:20 +03:00
}
2019-05-31 19:33:56 +03:00
function profileStartup(initialTime) {
function profile() {
console.profile('startup');
const startTime = Date.now();
setupWindow().then(function() {
setLoadTime(Date.now() - startTime + initialTime);
console.profileEnd('startup');
console.log(
'Switch to the Profiles tab to view the created startup profile'
);
});
2017-02-27 16:47:20 +03:00
}
2015-05-13 19:47:10 +03:00
2019-05-31 19:33:56 +03:00
const webContents = electron.remote.getCurrentWindow().webContents;
2017-02-27 16:47:20 +03:00
if (webContents.devToolsWebContents) {
2019-05-31 19:33:56 +03:00
profile();
2017-02-27 16:47:20 +03:00
} else {
2019-05-31 19:33:56 +03:00
webContents.once('devtools-opened', () => {
setTimeout(profile, 1000);
});
webContents.openDevTools();
2017-02-27 16:47:20 +03:00
}
}
2019-05-31 19:33:56 +03:00
function setupAtomHome() {
2017-02-27 16:47:20 +03:00
if (process.env.ATOM_HOME) {
2019-05-31 19:33:56 +03:00
return;
2017-02-27 16:47:20 +03:00
}
2015-06-04 02:11:16 +03:00
2017-02-27 16:47:20 +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
// https://github.com/atom/atom/issues/5412
if (getWindowLoadSettings() && getWindowLoadSettings().atomHome) {
2019-05-31 19:33:56 +03:00
process.env.ATOM_HOME = getWindowLoadSettings().atomHome;
}
}
2019-05-31 19:33:56 +03:00
})();