Move some more files to typescript

This commit is contained in:
Tamius Han 2021-02-09 00:52:58 +01:00
parent a907d0d404
commit 2d8bf0c0ee
3 changed files with 30 additions and 35 deletions

View File

@ -1,19 +1,25 @@
import currentBrowser from '../conf/BrowserDetect';
import { decycle } from 'json-cyclic'; import { decycle } from 'json-cyclic';
import Comms from './comms/Comms'; import Comms from './comms/Comms';
import BrowserDetect from '../conf/BrowserDetect';
if (process.env.CHANNEL !== 'stable'){ if (process.env.CHANNEL !== 'stable'){
console.info('Loading Logger'); console.info('Loading Logger');
} }
class Logger { class Logger {
constructor(options) { temp_disable: boolean = false;
this.onLogEndCallbacks = []; onLogEndCallbacks: any[] = [];
this.history = []; history: any[] = [];
this.globalHistory = {}; globalHistory: any = {};
this.isContentScript = false; isContentScript: boolean = false;
this.isBackgroundScript = true; isBackgroundScript: boolean = true;
vuexStore: any;
uwInstance: any;
conf: any;
startTime: number;
stopTime: number;
constructor(options) {
this.vuexStore = options?.vuexStore; this.vuexStore = options?.vuexStore;
this.uwInstance = options?.uwInstance; this.uwInstance = options?.uwInstance;
} }
@ -23,20 +29,15 @@ class Logger {
console.info('Saving logger conf:', conf) console.info('Saving logger conf:', conf)
} }
if (currentBrowser.firefox || currentBrowser.edge) { (BrowserDetect.browserObj as any).storage.local.set( {'uwLogger': JSON.stringify(conf)});
return browser.storage.local.set( {'uwLogger': JSON.stringify(conf)});
} else if (currentBrowser.chrome) {
return chrome.storage.local.set( {'uwLogger': JSON.stringify(conf)});
}
} }
static syncConfig(callback) { static syncConfig(callback) {
const br = currentBrowser.firefox ? browser : chrome; (BrowserDetect.browserObj as any).storage.onChanged.addListener( (changes, area) => {
br.storage.onChanged.addListener( (changes, area) => {
if (changes.uwLogger) { if (changes.uwLogger) {
const newLoggerConf = JSON.parse(changes.uwLogger.newValue) const newLoggerConf = JSON.parse(changes.uwLogger.newValue)
if (process.env.CHANNEL === 'dev') { if (process.env.CHANNEL === 'dev') {
console.info('Logger settings reloaded. New conf:', conf); console.info('Logger settings reloaded. New conf:', newLoggerConf);
} }
callback(newLoggerConf); callback(newLoggerConf);
} }
@ -46,15 +47,11 @@ class Logger {
static async getConfig() { static async getConfig() {
let ret; let ret;
if (currentBrowser.firefox) { if (BrowserDetect.firefox) {
ret = await browser.storage.local.get('uwLogger'); ret = await (BrowserDetect.browserObj as any).storage.local.get('uwLogger');
} else if (currentBrowser.chrome) { } else if (BrowserDetect.anyChromium) {
ret = await new Promise( (resolve, reject) => { ret = await new Promise( (resolve, reject) => {
chrome.storage.local.get('uwLogger', (res) => resolve(res)); (BrowserDetect.browserObj as any).storage.local.get('uwLogger', (res) => resolve(res));
});
} else if (currentBrowser.edge) {
ret = await new Promise( (resolve, reject) => {
browser.storage.local.get('uwLogger', (res) => resolve(res));
}); });
} }
@ -101,9 +98,7 @@ class Logger {
this.temp_disable = false; this.temp_disable = false;
this.stopTime = this.conf.timeout ? performance.now() + (this.conf.timeout * 1000) : undefined; this.stopTime = this.conf.timeout ? performance.now() + (this.conf.timeout * 1000) : undefined;
const br = currentBrowser.firefox ? browser : chrome; (BrowserDetect.browserObj as any).storage.onChanged.addListener( (changes, area) => {
br.storage.onChanged.addListener( (changes, area) => {
if (process.env.CHANNEL === 'dev') { if (process.env.CHANNEL === 'dev') {
if (!changes.uwLogger) { if (!changes.uwLogger) {
// console.info('[Logger::<storage/on change> No new logger settings!'); // console.info('[Logger::<storage/on change> No new logger settings!');
@ -131,7 +126,7 @@ class Logger {
} }
clear() { clear() {
this.log = []; this.history = [];
this.startTime = performance.now(); this.startTime = performance.now();
this.stopTime = this.conf.timeout ? performance.now() + (this.conf.timeout * 1000) : undefined; this.stopTime = this.conf.timeout ? performance.now() + (this.conf.timeout * 1000) : undefined;
} }
@ -142,9 +137,9 @@ class Logger {
Logger.saveConfig(conf); Logger.saveConfig(conf);
} }
async getSaved() { // async getSaved() {
return Logger.getSaved(); // return Logger.getSaved();
} // }
// allow syncing of start times between bg and page scripts. // allow syncing of start times between bg and page scripts.
@ -174,7 +169,7 @@ class Logger {
getFileLogJSONString() { getFileLogJSONString() {
return { return {
site: window && window.location, site: window && window.location,
log: JSON.toString(this.history), log: JSON.stringify(this.history),
} }
} }
@ -213,7 +208,7 @@ class Logger {
parseStack() { parseStack() {
const trace = (new Error()).stack; const trace = (new Error()).stack;
const stackInfo = {}; const stackInfo: any = {};
// we turn our stack into array and remove the "file::line" part of the trace, // we turn our stack into array and remove the "file::line" part of the trace,
// since that is useless because minification/webpack // since that is useless because minification/webpack
stackInfo['stack'] = {trace: trace.split('\n').map(a => a.split('@')[0])}; stackInfo['stack'] = {trace: trace.split('\n').map(a => a.split('@')[0])};
@ -336,7 +331,7 @@ class Logger {
} }
} }
canLogConsole(component, stackInfo) { canLogConsole(component, stackInfo?) {
if (!this.conf.consoleOptions?.enabled || this.temp_disable) { if (!this.conf.consoleOptions?.enabled || this.temp_disable) {
return false; return false;
} }
@ -437,7 +432,7 @@ class Logger {
let ts = performance.now(); let ts = performance.now();
let secondMark = ts - 1000; let secondMark = ts - 1000;
let halfSecondMark = ts - 500; let halfSecondMark = ts - 500;
let i = this.history.length(); let i = this.history.length;
// correct ts _after_ secondMark and halfSecondMark were determined // correct ts _after_ secondMark and halfSecondMark were determined
if (ts <= this.history[this.history.length - 1]) { if (ts <= this.history[this.history.length - 1]) {

View File

@ -1,3 +1,3 @@
export async function sleep(timeout) { export async function sleep(timeout) {
return new Promise( (resolve, reject) => setTimeout(() => resolve(), timeout)); return new Promise( (resolve, reject) => setTimeout(() => resolve(null), timeout));
} }