Skip to content

Commit

Permalink
fix(ConnectViewport): allow prop changes at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
garthenweb committed Sep 23, 2018
1 parent d826c95 commit 50791dc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 42 deletions.
44 changes: 23 additions & 21 deletions lib/ObserveViewport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ interface IContext {
handler: TViewportChangeHandler,
options: IViewportChangeOptions,
) => void;
removeViewportChangeListener: (
handler: TViewportChangeHandler,
options: IViewportChangeOptions,
) => void;
removeViewportChangeListener: (handler: TViewportChangeHandler) => void;
}

export default class ObserveViewport extends React.Component<IProps, IState> {
Expand All @@ -48,10 +45,7 @@ export default class ObserveViewport extends React.Component<IProps, IState> {
) => void)
| null;
private removeViewportChangeListener:
| ((
handler: TViewportChangeHandler,
options: IViewportChangeOptions,
) => void)
| ((handler: TViewportChangeHandler) => void)
| null;

private tickId: NodeJS.Timer;
Expand All @@ -75,10 +69,7 @@ export default class ObserveViewport extends React.Component<IProps, IState> {

componentWillUnmount() {
if (this.removeViewportChangeListener) {
this.removeViewportChangeListener(this.handleViewportUpdate, {
notifyScroll: !this.props.disableScrollUpdates,
notifyDimensions: !this.props.disableDimensionsUpdates,
});
this.removeViewportChangeListener(this.handleViewportUpdate);
}
this.removeViewportChangeListener = null;
this.addViewportChangeListener = null;
Expand All @@ -104,10 +95,18 @@ export default class ObserveViewport extends React.Component<IProps, IState> {
});
};

get optionNotifyScroll(): boolean {
return !this.props.disableScrollUpdates;
}

get optionNotifyDimensions(): boolean {
return !this.props.disableDimensionsUpdates;
}

registerViewportListeners = ({
addViewportChangeListener,
removeViewportChangeListener,
}: IContext): null => {
}: IContext): React.ReactNode => {
const shouldRegister =
this.removeViewportChangeListener !== removeViewportChangeListener &&
this.addViewportChangeListener !== addViewportChangeListener;
Expand All @@ -117,18 +116,21 @@ export default class ObserveViewport extends React.Component<IProps, IState> {
}

if (this.removeViewportChangeListener) {
this.removeViewportChangeListener(this.handleViewportUpdate, {
notifyScroll: !this.props.disableScrollUpdates,
notifyDimensions: !this.props.disableDimensionsUpdates,
recalculateLayoutBeforeUpdate: this.props.recalculateLayoutBeforeUpdate,
});
this.removeViewportChangeListener(this.handleViewportUpdate);
}

this.removeViewportChangeListener = removeViewportChangeListener;
addViewportChangeListener(this.handleViewportUpdate, {
notifyScroll: !this.props.disableScrollUpdates,
notifyDimensions: !this.props.disableDimensionsUpdates,
recalculateLayoutBeforeUpdate: this.props.recalculateLayoutBeforeUpdate,
notifyScroll: () => !this.props.disableScrollUpdates,
notifyDimensions: () => !this.props.disableDimensionsUpdates,
recalculateLayoutBeforeUpdate: (viewport: IViewport) => {
if (this.props.recalculateLayoutBeforeUpdate) {
return this.props.recalculateLayoutBeforeUpdate(viewport);
}
return null;
},
});

return null;
};

Expand Down
24 changes: 5 additions & 19 deletions lib/ViewportProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ export const SCROLL_DIR_LEFT = Symbol('SCROLL_DIR_LEFT');
export const SCROLL_DIR_RIGHT = Symbol('SCROLL_DIR_RIGHT');

const ViewportContext = React.createContext({
removeViewportChangeListener: (
handler: TViewportChangeHandler,
options: IViewportChangeOptions,
) => {},
removeViewportChangeListener: (handler: TViewportChangeHandler) => {},
addViewportChangeListener: (
handler: TViewportChangeHandler,
options: IViewportChangeOptions,
Expand Down Expand Up @@ -240,8 +237,8 @@ export default class ViewportProvider extends React.PureComponent {
const publicState = this.getPropsFromState();
const updatableListeners = this.listeners.filter(
({ notifyScroll, notifyDimensions }) =>
(notifyScroll && scrollDidUpdate) ||
(notifyDimensions && dimensionsDidUpdate),
(notifyScroll() && scrollDidUpdate) ||
(notifyDimensions() && dimensionsDidUpdate),
);
const layouts = updatableListeners.map(
({ recalculateLayoutBeforeUpdate }) => {
Expand Down Expand Up @@ -277,19 +274,8 @@ export default class ViewportProvider extends React.PureComponent {
this.listeners.push({ handler, ...options });
};

removeViewportChangeListener = (
h: TViewportChangeHandler,
options: IViewportChangeOptions,
) => {
this.listeners = this.listeners.filter(
({ handler, notifyScroll, notifyDimensions }) => {
const equals =
handler === h &&
notifyScroll === options.notifyScroll &&
notifyDimensions === options.notifyDimensions;
return !equals;
},
);
removeViewportChangeListener = (h: TViewportChangeHandler) => {
this.listeners = this.listeners.filter(({ handler }) => handler !== h);
};

render() {
Expand Down
4 changes: 2 additions & 2 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export type TViewportChangeHandler = (
) => void;

export interface IViewportChangeOptions {
notifyScroll: boolean;
notifyDimensions: boolean;
notifyScroll: () => boolean;
notifyDimensions: () => boolean;
recalculateLayoutBeforeUpdate?: (viewport: IViewport) => any;
}

0 comments on commit 50791dc

Please sign in to comment.