Skip to content

Commit

Permalink
feat(Scheduler): increase precision of layout measurement
Browse files Browse the repository at this point in the history
Attempt to optimize measurments for layouts after the
JS handlers are executed.

See #14
  • Loading branch information
garthenweb committed Oct 6, 2019
1 parent fd7001d commit ef5f641
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
10 changes: 8 additions & 2 deletions examples/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ const HooksExample = () => {
};

render(
<ViewportProvider experimentalSchedulerEnabled>
<ViewportProvider
experimentalSchedulerEnabled
experimentalSchedulerLayoutCalculatorEnabled
>
<main role="main">
<Example />
<HooksExample />
Expand All @@ -188,7 +191,10 @@ render(

setInterval(() => {
render(
<ViewportProvider experimentalSchedulerEnabled>
<ViewportProvider
experimentalSchedulerEnabled
experimentalSchedulerLayoutCalculatorEnabled
>
<main role="main">
<Example />
<HooksExample />
Expand Down
21 changes: 20 additions & 1 deletion lib/ViewportProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {

interface IProps {
experimentalSchedulerEnabled?: boolean;
experimentalSchedulerLayoutCalculatorEnabled?: boolean;
}

interface IListener extends IViewportChangeOptions {
Expand Down Expand Up @@ -95,6 +96,7 @@ export default class ViewportProvider extends React.PureComponent<
> {
static defaultProps: {
experimentalSchedulerEnabled: false;
experimentalSchedulerLayoutCalculatorEnabled: false;
};
private listeners: IListener[] = [];
private updateListenersTick?: NodeJS.Timer;
Expand All @@ -118,6 +120,7 @@ export default class ViewportProvider extends React.PureComponent<
{ scrollDidUpdate, dimensionsDidUpdate }: IViewportCollectorUpdateOptions,
options?: { isIdle?: boolean; shouldInitialize?: boolean },
) => {
const getOverallDuration = createPerformanceMarker();
const { isIdle, shouldInitialize } = Object.assign(
{ isIdle: false, shouldInitialize: false },
options,
Expand Down Expand Up @@ -168,7 +171,7 @@ export default class ViewportProvider extends React.PureComponent<
return null;
},
);

let overallJSHandlerTotalCost = 0;
updatableListeners.forEach((listener, index) => {
const { handler, averageExecutionCost, iterations } = listener;
const [layout, layoutCost] = layouts[index] || [null, 0];
Expand All @@ -182,7 +185,23 @@ export default class ViewportProvider extends React.PureComponent<
listener.averageExecutionCost = averageExecutionCost + diff / i;
listener.iterations = i;
listener.initialized = true;
overallJSHandlerTotalCost += totalCost;
});
if (
this.props.experimentalSchedulerLayoutCalculatorEnabled &&
updatableListeners.length
) {
setTimeout(() => {
const diffPerHandler =
(getOverallDuration() - overallJSHandlerTotalCost) /
updatableListeners.length;
updatableListeners.forEach(listener => {
listener.averageExecutionCost =
listener.averageExecutionCost +
diffPerHandler / listener.iterations;
});
}, 0);
}
};

addViewportChangeListener = (
Expand Down

0 comments on commit ef5f641

Please sign in to comment.