TeaSpeak-Client/modules/renderer/index.ts
2020-05-04 11:54:35 +02:00

164 lines
5.1 KiB
TypeScript

/* --------------- bootstrap --------------- */
import * as rh from "./require-handler";
import * as crash_handler from "../crash_handler";
import * as path from "path";
/* first of all setup crash handler */
{
const is_electron_run = process.argv[0].endsWith("electron") || process.argv[0].endsWith("electron.exe");
crash_handler.initialize_handler("renderer", is_electron_run);
}
/* some decls */
declare global {
interface Window {
$: any;
jQuery: any;
jsrender: any;
impl_display_critical_error: any;
displayCriticalError: any;
teaclient_initialize: any;
open_connected_question: () => Promise<boolean>;
}
}
rh.initialize(path.join(__dirname, "backend-impl"));
/* --------------- main initialize --------------- */
import {Arguments, parse_arguments, process_args} from "../shared/process-arguments";
import * as electron from "electron";
import {remote} from "electron";
import * as os from "os";
import * as loader from "tc-loader";
import ipcRenderer = electron.ipcRenderer;
/* we use out own jquery resource */
loader.register_task(loader.Stage.JAVASCRIPT, {
name: "teaclient jquery",
function: async () => {
window.$ = require("jquery");
window.jQuery = window.$;
Object.assign(window.$, window.jsrender = require('jsrender'));
},
priority: 80
});
loader.register_task(loader.Stage.INITIALIZING, {
name: "teaclient initialize persistent storage",
function: async () => {
const storage = require("./PersistentLocalStorage");
await storage.initialize();
},
priority: 90
});
loader.register_task(loader.Stage.INITIALIZING, {
name: "teaclient initialize logging",
function: async () => {
const logger = require("./logger");
logger.setup();
},
priority: 80
});
loader.register_task(loader.Stage.INITIALIZING, {
name: "teaclient initialize error",
function: async () => {
const _impl = message => {
if(!process_args.has_flag(Arguments.DEBUG)) {
console.error("Displaying critical error: %o", message);
message = message.replace(/<br>/i, "\n");
const win = remote.getCurrentWindow();
remote.dialog.showMessageBox({
type: "error",
buttons: ["exit"],
title: "A critical error happened!",
message: message
});
win.close();
} else {
console.error("Received critical error: %o", message);
console.error("Ignoring error due to the debug mode");
}
};
if(window.impl_display_critical_error)
window.impl_display_critical_error = _impl;
else
window.displayCriticalError = _impl;
},
priority: 100
});
loader.register_task(loader.Stage.INITIALIZING, {
name: "teaclient initialize arguments",
function: async () => {
parse_arguments();
if(process_args.has_value(Arguments.DUMMY_CRASH_RENDERER))
crash_handler.handler.crash();
if(!process_args.has_flag(Arguments.DEBUG)) {
window.open_connected_question = () => remote.dialog.showMessageBox(remote.getCurrentWindow(), {
type: 'question',
buttons: ['Yes', 'No'],
title: 'Confirm',
message: 'Are you really sure?\nYou\'re still connected!'
}).then(result => result.response === 0);
}
},
priority: 110
});
loader.register_task(loader.Stage.INITIALIZING, {
name: 'gdb-waiter',
function: async () => {
if(process_args.has_flag(Arguments.DEV_TOOLS_GDB)) {
console.log("Process ID: %d", process.pid);
await new Promise(resolve => {
console.log("Waiting for continue!");
const listener = () => {
console.log("Continue");
document.removeEventListener('click', listener);
resolve();
};
document.addEventListener('click', listener);
});
}
},
priority: 100
});
loader.register_task(loader.Stage.LOADED, {
name: "argv connect",
function: async () => {
ipcRenderer.send('basic-action', "parse-connect-arguments");
},
priority: 0
});
loader.register_task(loader.Stage.JAVASCRIPT_INITIALIZING, {
name: "teaclient load adapters",
function: async () => {
/* all files which replaces a native driver */
try {
require("./version");
require("./menu");
require("./context-menu");
require("./app_backend");
require("./icon-helper").initialize();
require("./connection/FileTransfer");
} catch (error) {
console.log(error);
window.displayCriticalError("Failed to load native extensions: " + error);
throw error;
}
remote.getCurrentWindow().on('focus', () => remote.getCurrentWindow().flashFrame(false));
},
priority: 60
});
export async function initialize() { }