Skip to content

Commit eff1c2b

Browse files
committed
Propagate console.log and friends from server to client. Closes #86
1 parent b5b65ac commit eff1c2b

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/React/ReactEnvironment.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,11 @@ public virtual string GetInitJavaScript()
313313
fullScript.Append(component.RenderJavaScript());
314314
fullScript.AppendLine(";");
315315
}
316+
317+
// Also propagate any server-side console.log calls to corresponding client-side calls.
318+
var consoleCalls = Execute<string>("console.getCalls()");
319+
fullScript.Append(consoleCalls);
320+
316321
return fullScript.ToString();
317322
}
318323

src/React/Resources/shims.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,33 @@
88
*/
99

1010
var global = global || {};
11-
// TODO: Handle errors "thrown" by console.error / console.warn?
12-
var console = console || {
13-
log: function () { },
14-
error: function () { },
15-
warn: function () { }
11+
12+
// Basic console shim. Caches all calls to console methods.
13+
function MockConsole() {
14+
this._calls = [];
15+
['log', 'error', 'warn', 'debug', 'info', 'dir', 'group', 'groupEnd', 'groupCollapsed'].forEach(function (methodName) {
16+
this[methodName] = this._handleCall.bind(this, methodName);
17+
}, this);
18+
}
19+
MockConsole.prototype = {
20+
_handleCall: function(methodName/*, ...args*/) {
21+
var serializedArgs = [];
22+
for (var i = 1; i < arguments.length; i++) {
23+
serializedArgs.push(JSON.stringify(arguments[i]));
24+
}
25+
this._calls.push({
26+
method: methodName,
27+
args: serializedArgs
28+
});
29+
},
30+
_formatCall: function(call) {
31+
return 'console.' + call.method + '("[.NET]", ' + call.args.join(', ') + ');';
32+
},
33+
getCalls: function() {
34+
return this._calls.map(this._formatCall).join('\n');
35+
}
1636
};
37+
var console = new MockConsole();
1738

1839
if (!Object.freeze) {
1940
Object.freeze = function() { };

0 commit comments

Comments
 (0)