Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New context API #1462

Merged
merged 7 commits into from
Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/victory-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ export { default as Text } from "./victory-primitives/text";
export { default as CommonProps } from "./victory-util/common-props";
export { default as Wrapper } from "./victory-util/wrapper";
export { default as Axis } from "./victory-util/axis";
export { default as TimerContext } from "./victory-util/timer-context";
export { default as PortalContext } from "./victory-portal/portal-context";
26 changes: 11 additions & 15 deletions packages/victory-core/src/victory-animation/victory-animation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import PropTypes from "prop-types";
import * as d3Ease from "d3-ease";
import { victoryInterpolator } from "./util";
import Timer from "../victory-util/timer";
import TimerContext from "../victory-util/timer-context";
import isEqual from "react-fast-compare";

export default class VictoryAnimation extends React.Component {
Expand Down Expand Up @@ -66,9 +67,7 @@ export default class VictoryAnimation extends React.Component {
easing: "quadInOut"
};

static contextTypes = {
getTimer: PropTypes.func
};
static contextType = TimerContext;

constructor(props) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that React requires you to pass the context constructor argument to the super call in order for this.context to be set. Right now, this.context is always undefined.

super(props);
Expand All @@ -89,7 +88,7 @@ export default class VictoryAnimation extends React.Component {
so we bind functionToBeRunEachFrame to current instance of victory animation class
*/
this.functionToBeRunEachFrame = this.functionToBeRunEachFrame.bind(this);
this.getTimer = this.getTimer.bind(this);
this.timer = this.getTimer();
}

componentDidMount() {
Expand All @@ -103,7 +102,7 @@ export default class VictoryAnimation extends React.Component {
const equalProps = isEqual(this.props, nextProps);
if (!equalProps) {
/* cancel existing loop if it exists */
this.getTimer().unsubscribe(this.loopID);
this.timer.unsubscribe(this.loopID);

/* If an object was supplied */
if (!Array.isArray(nextProps.data)) {
Expand All @@ -124,15 +123,15 @@ export default class VictoryAnimation extends React.Component {

componentWillUnmount() {
if (this.loopID) {
this.getTimer().unsubscribe(this.loopID);
this.timer.unsubscribe(this.loopID);
} else {
this.getTimer().stop();
this.timer.stop();
}
}

getTimer() {
if (this.context.getTimer) {
return this.context.getTimer();
if (this.context && this.context.globalTimer) {
return this.context.globalTimer;
}
if (!this.timer) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the new React context, the context should always default to the globalTimer defined in timer-context.js in case it is not set. Thus, you can assume that this value is set and as such you don't need logic for constructing a timer here.

this.timer = new Timer();
Expand All @@ -156,13 +155,10 @@ export default class VictoryAnimation extends React.Component {
/* reset step to zero */
if (this.props.delay) {
setTimeout(() => {
this.loopID = this.getTimer().subscribe(
this.functionToBeRunEachFrame,
this.props.duration
);
this.loopID = this.timer.subscribe(this.functionToBeRunEachFrame, this.props.duration);
}, this.props.delay);
} else {
this.loopID = this.getTimer().subscribe(this.functionToBeRunEachFrame, this.props.duration);
this.loopID = this.timer.subscribe(this.functionToBeRunEachFrame, this.props.duration);
}
} else if (this.props.onEnd) {
this.props.onEnd();
Expand All @@ -186,7 +182,7 @@ export default class VictoryAnimation extends React.Component {
}
});
if (this.loopID) {
this.getTimer().unsubscribe(this.loopID);
this.timer.unsubscribe(this.loopID);
}
this.queue.shift();
this.traverseQueue();
Expand Down
62 changes: 29 additions & 33 deletions packages/victory-core/src/victory-container/victory-container.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import PropTypes from "prop-types";
import CustomPropTypes from "../victory-util/prop-types";
import { assign, defaults, uniqueId, isObject, isFunction } from "lodash";
import Portal from "../victory-portal/portal";
import PortalContext from "../victory-portal/portal-context";
import Timer from "../victory-util/timer";
import TimerContext from "../victory-util/timer-context";
import Helpers from "../victory-util/helpers";

export default class VictoryContainer extends React.Component {
Expand Down Expand Up @@ -37,20 +39,11 @@ export default class VictoryContainer extends React.Component {
responsive: true
};

static contextTypes = {
getTimer: PropTypes.func
};

static childContextTypes = {
portalUpdate: PropTypes.func,
portalRegister: PropTypes.func,
portalDeregister: PropTypes.func,
getTimer: PropTypes.func
};
static contextType = TimerContext;

constructor(props) {
super(props);
this.getTimer = this.getTimer.bind(this);
this.timer = this.getTimer();
this.containerId =
!isObject(props) || props.containerId === undefined
? uniqueId("victory-container-")
Expand All @@ -77,33 +70,24 @@ export default class VictoryContainer extends React.Component {
}
}

getChildContext() {
return {
portalUpdate: this.portalUpdate,
portalRegister: this.portalRegister,
portalDeregister: this.portalDeregister,
getTimer: this.getTimer
};
}

componentDidMount() {
if (this.shouldHandleWheel && this.containerRef) {
this.containerRef.addEventListener("wheel", this.handleWheel);
}
}

componentWillUnmount() {
if (!this.context.getTimer) {
this.getTimer().stop();
if (this.timer) {
this.timer.stop();
}
if (this.shouldHandleWheel && this.containerRef) {
this.containerRef.removeEventListener("wheel", this.handleWheel);
}
}

getTimer() {
if (this.context.getTimer) {
return this.context.getTimer();
if (this.context && this.context.globalTimer) {
return this.context.globalTimer;
}
if (!this.timer) {
this.timer = new Timer();
Expand Down Expand Up @@ -150,16 +134,28 @@ export default class VictoryContainer extends React.Component {
style: portalSvgStyle
};
return (
<div style={defaults({}, style, divStyle)} className={className} ref={this.saveContainerRef}>
<svg {...svgProps} style={svgStyle}>
{title ? <title id={this.getIdForElement("title")}>{title}</title> : null}
{desc ? <desc id={this.getIdForElement("desc")}>{desc}</desc> : null}
{children}
</svg>
<div style={portalDivStyle}>
{React.cloneElement(portalComponent, { ...portalProps, ref: this.savePortalRef })}
<PortalContext.Provider
value={{
portalUpdate: this.portalUpdate,
portalRegister: this.portalRegister,
portalDeregister: this.portalDeregister
}}
>
<div
style={defaults({}, style, divStyle)}
className={className}
ref={this.saveContainerRef}
>
<svg {...svgProps} style={svgStyle}>
{title ? <title id={this.getIdForElement("title")}>{title}</title> : null}
{desc ? <desc id={this.getIdForElement("desc")}>{desc}</desc> : null}
{children}
</svg>
<div style={portalDivStyle}>
{React.cloneElement(portalComponent, { ...portalProps, ref: this.savePortalRef })}
</div>
</div>
</div>
</PortalContext.Provider>
);
}

Expand Down
10 changes: 10 additions & 0 deletions packages/victory-core/src/victory-portal/portal-context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from "react";

/**
* The React context object consumers may use to access the context of the
* portal.
*/
const PortalContext = React.createContext({});
PortalContext.displayName = "PortalContext";

export default PortalContext;
7 changes: 2 additions & 5 deletions packages/victory-core/src/victory-portal/victory-portal.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import PropTypes from "prop-types";
import { defaults } from "lodash";
import Log from "../victory-util/log";
import Helpers from "../victory-util/helpers";
import PortalContext from "./portal-context";

export default class VictoryPortal extends React.Component {
static displayName = "VictoryPortal";
Expand All @@ -18,11 +19,7 @@ export default class VictoryPortal extends React.Component {
groupComponent: <g />
};

static contextTypes = {
portalDeregister: PropTypes.func,
portalRegister: PropTypes.func,
portalUpdate: PropTypes.func
};
static contextType = PortalContext;

componentDidMount() {
if (!this.checkedContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import VictoryAnimation from "../victory-animation/victory-animation";
import Collection from "../victory-util/collection";
import Helpers from "../victory-util/helpers";
import Timer from "../victory-util/timer";
import TimerContext from "../victory-util/timer-context";
import Transitions from "../victory-util/transitions";
import { defaults, isFunction, pick, isObject } from "lodash";
import isEqual from "react-fast-compare";
Expand All @@ -17,6 +18,8 @@ export default class VictoryTransition extends React.Component {
children: PropTypes.node
};

static contextType = TimerContext;

constructor(props) {
super(props);
this.state = {
Expand All @@ -27,7 +30,7 @@ export default class VictoryTransition extends React.Component {
const polar = child.props.polar;
this.continuous = !polar && child.type && child.type.continuous === true;
this.getTransitionState = this.getTransitionState.bind(this);
this.getTimer = this.getTimer.bind(this);
this.timer = this.getTimer();
}

componentDidMount() {
Expand All @@ -36,21 +39,21 @@ export default class VictoryTransition extends React.Component {

shouldComponentUpdate(nextProps) {
if (!isEqual(this.props, nextProps)) {
this.getTimer().bypassAnimation();
this.timer.bypassAnimation();
this.setState(this.getTransitionState(this.props, nextProps), () =>
this.getTimer().resumeAnimation()
this.timer.resumeAnimation()
);
}
return true;
}

componentWillUnmount() {
this.getTimer().stop();
this.timer.stop();
}

getTimer() {
if (this.context.getTimer) {
return this.context.getTimer();
if (this.context && this.context.globalTimer) {
return this.context.globalTimer;
}
if (!this.timer) {
this.timer = new Timer();
Expand Down
11 changes: 11 additions & 0 deletions packages/victory-core/src/victory-util/timer-context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from "react";
import Timer from "./timer";

/**
* The React context object consumers may use to access or override the global
* timer.
*/
const TimerContext = React.createContext({ globalTimer: new Timer() });
TimerContext.displayName = "TimerContext";

export default TimerContext;
27 changes: 2 additions & 25 deletions packages/victory-shared-events/src/victory-shared-events.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assign, isFunction, defaults, isEmpty, fromPairs } from "lodash";
import React from "react";
import PropTypes from "prop-types";
import { PropTypes as CustomPropTypes, Events, Helpers, Timer } from "victory-core";
import { PropTypes as CustomPropTypes, Events, Helpers, TimerContext } from "victory-core";
import isEqual from "react-fast-compare";

export default class VictorySharedEvents extends React.Component {
Expand Down Expand Up @@ -51,29 +51,16 @@ export default class VictorySharedEvents extends React.Component {
groupComponent: <g />
};

static contextTypes = {
getTimer: PropTypes.func
};

static childContextTypes = {
getTimer: PropTypes.func
};
static contextType = TimerContext;

constructor(props) {
super(props);
this.state = this.state || {};
this.getScopedEvents = Events.getScopedEvents.bind(this);
this.getEventState = Events.getEventState.bind(this);
this.getTimer = this.getTimer.bind(this);
this.baseProps = this.getBaseProps(props);
}

getChildContext() {
return {
getTimer: this.getTimer
};
}

shouldComponentUpdate(nextProps) {
if (!isEqual(this.props, nextProps)) {
this.baseProps = this.getBaseProps(nextProps);
Expand All @@ -83,16 +70,6 @@ export default class VictorySharedEvents extends React.Component {
return true;
}

getTimer() {
if (this.context.getTimer) {
return this.context.getTimer();
}
if (!this.timer) {
this.timer = new Timer();
}
return this.timer;
}

getAllEvents(props) {
const components = ["container", "groupComponent"];
const componentEvents = Events.getComponentEvents(props, components);
Expand Down
10 changes: 4 additions & 6 deletions packages/victory-zoom-container/src/zoom-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,10 @@ const RawZoomHelpers = {
},

handleAnimation(ctx) {
const getTimer = isFunction(ctx.getTimer) && ctx.getTimer.bind(ctx);
if (getTimer && isFunction(getTimer().bypassAnimation)) {
getTimer().bypassAnimation();
return isFunction(getTimer().resumeAnimation)
? () => getTimer().resumeAnimation()
: undefined;
const timer = ctx.context.globalTimer;
if (timer) {
timer.bypassAnimation();
return () => timer.resumeAnimation()
}
return undefined;
},
Expand Down