Skip to content

Commit

Permalink
Make use of useCallback to prevent unnecessary unsubscribe/re-subscri…
Browse files Browse the repository at this point in the history
…be calls
  • Loading branch information
jlfwong committed May 23, 2020
1 parent 8f0715b commit c5858f9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
39 changes: 21 additions & 18 deletions src/views/application-container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,33 @@ import {getProfileToView, getCanvasContext} from '../store/getters'
import {actions} from '../store/actions'
import {useActionCreator} from '../lib/preact-redux'
import {memo} from 'preact/compat'
import {useCallback} from 'preact/hooks'
import {useAppSelector} from '../store'

export const ApplicationContainer = memo(() => {
const appState = useAppSelector(state => state)
const canvasContext = useAppSelector(state =>
state.glCanvas ? getCanvasContext(state.glCanvas) : null,
const appState = useAppSelector(useCallback(state => state, []))
const canvasContext = useAppSelector(
useCallback(state => (state.glCanvas ? getCanvasContext(state.glCanvas) : null), []),
)

const activeProfileState: ActiveProfileState | null = useAppSelector(state => {
const {profileGroup} = state
if (!profileGroup) return null
if (profileGroup.indexToView >= profileGroup.profiles.length) return null
const activeProfileState: ActiveProfileState | null = useAppSelector(
useCallback(state => {
const {profileGroup} = state
if (!profileGroup) return null
if (profileGroup.indexToView >= profileGroup.profiles.length) return null

const index = profileGroup.indexToView
const profileState = profileGroup.profiles[index]
return {
...profileGroup.profiles[profileGroup.indexToView],
profile: getProfileToView({
profile: profileState.profile,
flattenRecursion: state.flattenRecursion,
}),
index: profileGroup.indexToView,
}
})
const index = profileGroup.indexToView
const profileState = profileGroup.profiles[index]
return {
...profileGroup.profiles[profileGroup.indexToView],
profile: getProfileToView({
profile: profileState.profile,
flattenRecursion: state.flattenRecursion,
}),
index: profileGroup.indexToView,
}
}, []),
)

return (
<Application
Expand Down
5 changes: 3 additions & 2 deletions src/views/callee-flamegraph-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {FlamechartWrapper} from './flamechart-wrapper'
import {useAppSelector} from '../store'
import {h} from 'preact'
import {memo} from 'preact/compat'
import {useCallback} from 'preact/hooks'

const getCalleeProfile = memoizeByShallowEquality<
{
Expand Down Expand Up @@ -50,8 +51,8 @@ const getCalleeFlamegraphRenderer = createMemoizedFlamechartRenderer()
export const CalleeFlamegraphView = memo((ownProps: FlamechartViewContainerProps) => {
const {activeProfileState} = ownProps
const {index, profile, sandwichViewState} = activeProfileState
const flattenRecursion = useAppSelector(state => state.flattenRecursion)
const glCanvas = useAppSelector(state => state.glCanvas)
const flattenRecursion = useAppSelector(useCallback(state => state.flattenRecursion, []))
const glCanvas = useAppSelector(useCallback(state => state.glCanvas, []))

if (!profile) throw new Error('profile missing')
if (!glCanvas) throw new Error('glCanvas missing')
Expand Down
5 changes: 3 additions & 2 deletions src/views/inverted-caller-flamegraph-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {useAppSelector} from '../store'
import {FlamechartWrapper} from './flamechart-wrapper'
import {h} from 'preact'
import {memo} from 'preact/compat'
import {useCallback} from 'preact/hooks'

const getInvertedCallerProfile = memoizeByShallowEquality(
({
Expand Down Expand Up @@ -56,8 +57,8 @@ const getInvertedCallerFlamegraphRenderer = createMemoizedFlamechartRenderer({in
export const InvertedCallerFlamegraphView = memo((ownProps: FlamechartViewContainerProps) => {
const {activeProfileState} = ownProps
let {profile, sandwichViewState, index} = activeProfileState
const flattenRecursion = useAppSelector(state => state.flattenRecursion)
const glCanvas = useAppSelector(state => state.glCanvas)
const flattenRecursion = useAppSelector(useCallback(state => state.flattenRecursion, []))
const glCanvas = useAppSelector(useCallback(state => state.glCanvas, []))

if (!profile) throw new Error('profile missing')
if (!glCanvas) throw new Error('glCanvas missing')
Expand Down
2 changes: 1 addition & 1 deletion src/views/profile-table-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ export const ProfileTableViewContainer = memo((ownProps: ProfileTableViewContain
const {activeProfileState} = ownProps
const {profile, sandwichViewState, index} = activeProfileState
if (!profile) throw new Error('profile missing')
const tableSortMethod = useAppSelector(state => state.tableSortMethod)
const tableSortMethod = useAppSelector(useCallback(state => state.tableSortMethod, []))
const {callerCallee} = sandwichViewState
const selectedFrame = callerCallee ? callerCallee.selectedFrame : null
const frameToColorBucket = getFrameToColorBucket(profile)
Expand Down

0 comments on commit c5858f9

Please sign in to comment.