Skip to content

Commit

Permalink
feat(useData): enhance data update and sync logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker committed Mar 12, 2024
1 parent 7a2d73b commit 2ebdc54
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { StrictMode, createRef, useContext } from 'react'
import React, { StrictMode, createRef, useContext, useEffect } from 'react'
import {
act,
fireEvent,
Expand Down Expand Up @@ -2214,8 +2214,12 @@ describe('DataContext.Provider', () => {
</DataContext.Provider>
)

expect(nestedMockData).toHaveLength(1)
expect(nestedMockData[0]).toEqual(initialData)
expect(nestedMockData).toHaveLength(3)
expect(nestedMockData).toEqual([
initialData,
initialData,
initialData,
])

const inputElement = document.querySelector('input')
expect(inputElement).toHaveValue('bar')
Expand Down Expand Up @@ -2247,11 +2251,15 @@ describe('DataContext.Provider', () => {
</>
)

expect(sidecarMockData).toHaveLength(2)
expect(sidecarMockData).toEqual([undefined, initialData])
expect(sidecarMockData).toHaveLength(3)
expect(sidecarMockData).toEqual([
undefined,
initialData,
initialData,
])

expect(nestedMockData).toHaveLength(1)
expect(nestedMockData).toEqual([initialData])
expect(nestedMockData).toHaveLength(2)
expect(nestedMockData).toEqual([initialData, initialData])

const [sidecar, nested] = Array.from(
document.querySelectorAll('input')
Expand All @@ -2260,6 +2268,58 @@ describe('DataContext.Provider', () => {
expect(nested).toHaveValue('bar')
})

it('should be able to update data from side car', async () => {
const sidecarMockData = []
const nestedMockData = []

const SidecarMock = () => {
const { data, update } = Form.useData(identifier)

useEffect(() => {
update('/fieldA', () => 'updated A')
update('/fieldB', () => 'updated B')
}, [update])

sidecarMockData.push(data)
return null
}

const NestedMock = () => {
const { data } = Form.useData(identifier)
nestedMockData.push(data)
return <Field.String path="/fieldB" />
}

render(
<>
<SidecarMock />
<DataContext.Provider id={identifier}>
<Field.String path="/fieldA" />
<NestedMock />
</DataContext.Provider>
</>
)

expect(sidecarMockData).toHaveLength(2)
expect(sidecarMockData).toEqual([
undefined,
{ fieldA: 'updated A', fieldB: 'updated B' },
])

expect(nestedMockData).toHaveLength(2)
expect(nestedMockData).toEqual([
undefined,
{ fieldA: 'updated A', fieldB: 'updated B' },
])

const [sidecar, nested] = Array.from(
document.querySelectorAll('input')
)

expect(sidecar).toHaveValue('updated A')
expect(nested).toHaveValue('updated B')
})

it('should support StrictMode', async () => {
const initialData = { foo: 'bar' }
const sidecarMockData = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export default function useData<Data>(
pointer.set(existingData, path, newValue)

// update provider
sharedDataRef.current.update(existingData)
sharedDataRef.current.extend(existingData)
},
[]
)
Expand Down
16 changes: 9 additions & 7 deletions packages/dnb-eufemia/src/shared/helpers/useSharedState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function useSharedState<Data>(
}
}, [hasMounted])

useLayoutEffect(() => {
useEffect(() => {
if (waitForMountedRef.current) {
forceUpdate()
}
Expand Down Expand Up @@ -155,11 +155,6 @@ export function createSharedState<Data>(

const get = () => sharedStates[id].data

const extend = (newData: Data) => {
sharedStates[id].data = { ...sharedStates[id].data, ...newData }
subscribers.forEach((subscriber) => subscriber())
}

const set = (newData: Partial<Data>) => {
sharedStates[id].data = { ...newData }
}
Expand All @@ -169,8 +164,15 @@ export function createSharedState<Data>(
subscribers.forEach((subscriber) => subscriber())
}

const extend = (newData: Data) => {
sharedStates[id].data = { ...sharedStates[id].data, ...newData }
subscribers.forEach((subscriber) => subscriber())
}

const subscribe = (subscriber: Subscriber) => {
subscribers.push(subscriber)
if (!subscribers.includes(subscriber)) {
subscribers.push(subscriber)
}
}

const unsubscribe = (subscriber: Subscriber) => {
Expand Down

0 comments on commit 2ebdc54

Please sign in to comment.