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

feat(useDataEngine): make engine instance a referential constant #934

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
20 changes: 20 additions & 0 deletions services/data/src/react/hooks/useDataEngine.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { renderHook } from '@testing-library/react-hooks'
import * as React from 'react'
import { CustomDataProvider } from '../components/CustomDataProvider'
import { useDataEngine } from './useDataEngine'

describe('useDataEngine', () => {
it('should preserve the referential identity of the engine', () => {
const wrapper = ({ children }) => (
<CustomDataProvider data={{}}>{children}</CustomDataProvider>
)

const { result, rerender } = renderHook(useDataEngine, { wrapper })
const { current: firstEngineResult } = result

rerender()
const { current: secondEngineResult } = result

expect(firstEngineResult).toBe(secondEngineResult)
})
})
14 changes: 10 additions & 4 deletions services/data/src/react/hooks/useDataEngine.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { useContext } from 'react'
import { useContext, useState } from 'react'
import { DataEngine } from '../../engine'
import { DataContext } from '../context/DataContext'

export const useDataEngine = () => {
const context = useContext(DataContext)
function useConst<T>(initializer: () => T): T {
const [value] = useState(initializer)
return value
}
Comment on lines +5 to +8
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
function useConst<T>(initializer: () => T): T {
const [value] = useState(initializer)
return value
}
function useInstanceVariable<T>(initializer: () => T, dependencies: any[]): T {
const [value, setValue] = useState(initializer)
const initializedRef = useRef(false)
useEffect(() => {
if (!initializedRef.current) {
initializedRef.current = true
return
}
setValue(initializer())
}, dependencies)
return value
}

Copy link
Member

Choose a reason for hiding this comment

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

Maybe something like this instead, to support updates when deps change?

Copy link
Member

Choose a reason for hiding this comment

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

Note however that this will cause double-renders on dependency updates...

Copy link
Contributor

Choose a reason for hiding this comment

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

useInstanceVariable would be used in DataProvider and CustomDataProvider instead of in useDataEngine right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds a lot like we could just use useMemo instead of useConst?

Copy link
Member

Choose a reason for hiding this comment

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

@Mohammer5 this is super old... but no, there's a difference because useConst guarantees referential integrity while useMemo might re-initialize in certain situations since it is only considered a performance optimization.

Copy link
Contributor Author

@Mohammer5 Mohammer5 Dec 16, 2021

Choose a reason for hiding this comment

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

Yeah, I also figured out the other day that useMemo does not guarantee that..
I think for simplicity's sake, the following might be the most pragmatic with the least amount of abstractions:

function useDataEngine = (): DataEngine => {
    const context = useContext(DataContext)
    const [instance, setInstance] = useState(context.engine)
    
   useEffect(() => {
        setInstance(context.engine)
    }, [context.engine])

    return value
}


return context.engine
export const useDataEngine = (): DataEngine => {
const context = useContext(DataContext)
const engine = useConst<DataEngine>(() => context.engine)
return engine
}