-
Notifications
You must be signed in to change notification settings - Fork 380
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(react-core): optimize position context calculation (#1813)
- Loading branch information
1 parent
1cc4343
commit b2ea6e7
Showing
2 changed files
with
59 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 39 additions & 18 deletions
57
packages/dx-react-core/src/plugin-based/plugin-indexer.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,45 @@ | ||
import * as React from 'react'; | ||
import { PluginPositionFn } from '@devexpress/dx-core'; | ||
import { PositionContext } from './contexts'; | ||
|
||
type MemoizedPluginPositionFn = | ||
(index: number, positionContext: PluginPositionFn) => PluginPositionFn; | ||
|
||
type PluginIndexerProps = { | ||
children: React.ReactNode; | ||
}; | ||
|
||
/** @internal */ | ||
export const PluginIndexer = ({ children }) => ( | ||
<PositionContext.Consumer> | ||
{positionContext => ( | ||
React.Children.map(children, (child, index) => { | ||
if (!child || !child.type) return child; | ||
export class PluginIndexer extends React.PureComponent<PluginIndexerProps> { | ||
indexes: { [index: number]: PluginPositionFn } = {}; | ||
memoize: MemoizedPluginPositionFn = (index, positionContext) => { | ||
if (this.indexes[index]) return this.indexes[index]; | ||
|
||
const fn: PluginPositionFn = () => { | ||
const calculatedPosition = positionContext(); | ||
return [...calculatedPosition, index]; | ||
}; | ||
this.indexes[index] = fn; | ||
|
||
const childPosition = () => { | ||
const calculatedPosition = (positionContext && positionContext()) || []; | ||
return [...calculatedPosition, index]; | ||
}; | ||
return fn; | ||
} | ||
render() { | ||
const { children } = this.props; | ||
return ( | ||
<PositionContext.Consumer> | ||
{positionContext => ( | ||
React.Children.map(children, (child: any, index: number) => { | ||
if (!child || !child.type) return child; | ||
const childPosition = this.memoize(index, positionContext); | ||
|
||
return ( | ||
<PositionContext.Provider value={childPosition}> | ||
{child} | ||
</PositionContext.Provider> | ||
); | ||
}) | ||
)} | ||
</PositionContext.Consumer> | ||
); | ||
return ( | ||
<PositionContext.Provider value={childPosition}> | ||
{child} | ||
</PositionContext.Provider> | ||
); | ||
}) | ||
)} | ||
</PositionContext.Consumer> | ||
); | ||
} | ||
} |