Skip to content

Commit

Permalink
Ability to access window.event in development (#11687) (#11696)
Browse files Browse the repository at this point in the history
Before this change in development window.event was overridden
in invokeGuardedCallback.

After this change window.event is preserved in the browsers that
support it.
ConradIrwin authored and gaearon committed Aug 14, 2018

Verified

This commit was signed with the committer’s verified signature.
rm3l Armel Soro
1 parent ade4dd3 commit 69e2a0d
Showing 2 changed files with 70 additions and 0 deletions.
53 changes: 53 additions & 0 deletions fixtures/dom/src/components/fixtures/error-handling/index.js
Original file line number Diff line number Diff line change
@@ -152,6 +152,44 @@ class SilenceErrors extends React.Component {
);
}
}
class GetEventTypeDuringUpdate extends React.Component {
state = {};

onClick = () => {
this.expectUpdate = true;
this.forceUpdate();
};

componentDidUpdate() {
if (this.expectUpdate) {
this.expectUpdate = false;
this.setState({eventType: window.event.type});
setTimeout(() => {
this.setState({cleared: !window.event});
});
}
}

render() {
return (
<div className="test-fixture">
<button onClick={this.onClick}>Trigger callback in event.</button>
{this.state.eventType ? (
<p>
Got <b>{this.state.eventType}</b> event.
</p>
) : (
<p>Got no event</p>
)}
{this.state.cleared ? (
<p>Event cleared correctly.</p>
) : (
<p>Event failed to clear.</p>
)}
</div>
);
}
}

class SilenceRecoverableError extends React.Component {
render() {
@@ -318,6 +356,21 @@ export default class ErrorHandlingTestCases extends React.Component {
</TestCase.ExpectedResult>
<TrySilenceFatalError />
</TestCase>

{window.hasOwnProperty('event') ? (
<TestCase
title="Error handling does not interfere with window.event"
description="">
<TestCase.Steps>
<li>Click the "Trigger callback in event" button</li>
</TestCase.Steps>
<TestCase.ExpectedResult>
You should see "Got <b>click</b> event" and "Event cleared
successfully" below.
</TestCase.ExpectedResult>
<GetEventTypeDuringUpdate />
</TestCase>
) : null}
</FixtureSet>
);
}
17 changes: 17 additions & 0 deletions packages/shared/invokeGuardedCallback.js
Original file line number Diff line number Diff line change
@@ -96,6 +96,11 @@ if (__DEV__) {
// the error event at all.
let didError = true;

// Keeps track of the value of window.event so that we can reset it
// during the callback to let user code access window.event in the
// browsers that support it.
let windowEvent = window.event;

// Create an event handler for our fake event. We will synchronously
// dispatch our fake event using `dispatchEvent`. Inside the handler, we
// call the user-provided callback.
@@ -106,6 +111,18 @@ if (__DEV__) {
// nested call would trigger the fake event handlers of any call higher
// in the stack.
fakeNode.removeEventListener(evtType, callCallback, false);

// We check for window.hasOwnProperty('event') to prevent the
// window.event assignment in both IE <= 10 as they throw an error
// "Member not found" in strict mode, and in Firefox which does not
// support window.event.
if (
typeof window.event !== 'undefined' &&
window.hasOwnProperty('event')
) {
window.event = windowEvent;
}

func.apply(context, funcArgs);
didError = false;
}

0 comments on commit 69e2a0d

Please sign in to comment.