mirror of
https://github.com/gorhill/uBlock.git
synced 2026-03-11 09:04:36 +00:00
[mv3] Code review implementation of custom "console"
Related commit:
28ea00fd11
This commit is contained in:
parent
18f9acd844
commit
704f2dd734
1 changed files with 13 additions and 17 deletions
|
|
@ -43,18 +43,21 @@ export const isSideloaded = (( ) => {
|
||||||
|
|
||||||
const CONSOLE_MAX_LINES = 32;
|
const CONSOLE_MAX_LINES = 32;
|
||||||
const consoleOutput = [];
|
const consoleOutput = [];
|
||||||
let consoleWritePtr = 0;
|
|
||||||
|
|
||||||
sessionRead('console').then(before => {
|
sessionRead('console').then(before => {
|
||||||
if ( Array.isArray(before) === false ) { return; }
|
if ( Array.isArray(before) === false ) { return; }
|
||||||
const current = getConsoleOutput();
|
for ( const s of before.reverse() ) {
|
||||||
const merged = [ ...before, ...current ].slice(-CONSOLE_MAX_LINES);
|
consoleOutput.unshift(s);
|
||||||
for ( let i = 0; i < merged.length; i++ ) {
|
|
||||||
consoleOutput[i] = merged[i];
|
|
||||||
}
|
}
|
||||||
consoleWritePtr = merged.length % CONSOLE_MAX_LINES;
|
consoleTruncate();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const consoleTruncate = ( ) => {
|
||||||
|
if ( consoleOutput.length <= CONSOLE_MAX_LINES ) { return; }
|
||||||
|
consoleOutput.copyWithin(0, -CONSOLE_MAX_LINES);
|
||||||
|
consoleOutput.length = CONSOLE_MAX_LINES;
|
||||||
|
};
|
||||||
|
|
||||||
const consoleAdd = (...args) => {
|
const consoleAdd = (...args) => {
|
||||||
if ( args.length === 0 ) { return; }
|
if ( args.length === 0 ) { return; }
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
|
|
@ -68,9 +71,10 @@ const consoleAdd = (...args) => {
|
||||||
for ( let i = 0; i < args.length; i++ ) {
|
for ( let i = 0; i < args.length; i++ ) {
|
||||||
const s = `[${time}]${args[i]}`;
|
const s = `[${time}]${args[i]}`;
|
||||||
if ( Boolean(s) === false ) { continue; }
|
if ( Boolean(s) === false ) { continue; }
|
||||||
consoleOutput[consoleWritePtr++] = s;
|
if ( s === consoleOutput.at(-1) ) { continue; }
|
||||||
consoleWritePtr %= CONSOLE_MAX_LINES;
|
consoleOutput.push(s);
|
||||||
}
|
}
|
||||||
|
consoleTruncate();
|
||||||
sessionWrite('console', getConsoleOutput());
|
sessionWrite('console', getConsoleOutput());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -91,15 +95,7 @@ export const ubolErr = (...args) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getConsoleOutput = ( ) => {
|
export const getConsoleOutput = ( ) => {
|
||||||
return [
|
return consoleOutput.slice();
|
||||||
...consoleOutput.slice(consoleWritePtr),
|
|
||||||
...consoleOutput.slice(0, consoleWritePtr),
|
|
||||||
].reduce((acc, val) => {
|
|
||||||
if ( val !== acc.at(-1) ) {
|
|
||||||
acc.push(val);
|
|
||||||
}
|
|
||||||
return acc;
|
|
||||||
}, []);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue