Skip to content

Commit

Permalink
Avoid crash if setTimeout/clearTimeout is referenced
Browse files Browse the repository at this point in the history
In some cases, React (or other libraries) will assume that setTimeout
and clearTimeout are defined on the global scope, without invoking it.

We should still throw an error in case a consumer expects this to just
work on the server (it won't), but we can avoid a crash by defining the
function on the global scope.

For context: reactjs#555
  • Loading branch information
dustinsoftware committed Jun 29, 2018
1 parent 8fdb2c0 commit 18d608d
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/React.Core/Resources/shims.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

var global = global || {};
var React, ReactDOM, ReactDOMServer;
var React, ReactDOM, ReactDOMServer, setTimeout, clearTimeout;

// Basic console shim. Caches all calls to console methods.
function MockConsole() {
Expand Down Expand Up @@ -68,6 +68,20 @@ function ReactNET_initReact() {
return false;
}

setTimeout = setTimeout || global.setTimeout;
if (setTimeout === undefined) {
setTimeout = function() {
throw new Error('setTimeout is not supported in server-rendered Javascript.');
}
}

clearTimeout = clearTimeout || global.clearTimeout;
if (clearTimeout === undefined) {
clearTimeout = function() {
throw new Error('clearTimeout is not supported in server-rendered Javascript.');
}
}

/**
* Polyfill for engines that do not support Object.assign
*/
Expand All @@ -94,4 +108,4 @@ if (typeof Object.assign !== 'function') {
}
return to;
};
}
}

0 comments on commit 18d608d

Please sign in to comment.