diff --git a/client/utils/hookConsoleSync.js b/client/utils/hookConsoleSync.js new file mode 100644 index 0000000000..3aeacf06a6 --- /dev/null +++ b/client/utils/hookConsoleSync.js @@ -0,0 +1,21 @@ +import parse from 'console-feed/lib/Hook/parse'; +import methods from 'console-feed/lib/definitions/Methods'; +import { Encode } from 'console-feed'; + +/** + * Synchronous replacement for console-feed's Hook. Encodes function + * arguments immediately, so the values of mutable arguments are + * captured accurately. + * + * (See https://github.com/samdenty/console-feed/pull/77.) + */ +export default function hookConsoleSync(targetConsole, callback) { + methods.forEach((method) => { + const native = targetConsole[method]; + targetConsole[method] = function patched(...args) { + native.apply(this, args); + const parsed = parse(method, args); + if (parsed) callback(Encode(parsed)); + }; + }); +} diff --git a/client/utils/previewEntry.js b/client/utils/previewEntry.js index 0d369788c3..504cd72b05 100644 --- a/client/utils/previewEntry.js +++ b/client/utils/previewEntry.js @@ -1,6 +1,7 @@ -import { Hook, Decode, Encode } from 'console-feed'; +import { Decode, Encode } from 'console-feed'; import StackTrace from 'stacktrace-js'; import { evaluateExpression } from './evaluateExpression'; +import hookConsoleSync from './hookConsoleSync'; // should postMessage user the dispatcher? does the parent window need to // be registered as a frame? or a just a listener? @@ -56,7 +57,7 @@ window.loopProtect = { const consoleBuffer = []; const LOGWAIT = 500; -Hook(window.console, (log) => { +hookConsoleSync(window.console, (log) => { consoleBuffer.push({ log });