Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ienjoygit committed Mar 3, 2020
1 parent 571ed80 commit dd31d87
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
4 changes: 1 addition & 3 deletions src/UseStateLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1855,8 +1855,6 @@ class StateLinkImpl<S> implements StateLink<S>,
}
}

const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect;

function createState<S>(initial: SetInitialStateAction<S>): State {
let initialValue: S | Promise<S> = initial as (S | Promise<S>);
if (typeof initial === 'function') {
Expand Down Expand Up @@ -1889,7 +1887,7 @@ function useSubscribedStateLink<S>(
if (disabledTracking) {
link.with(Downgraded)
}
useIsomorphicLayoutEffect(() => {
React.useEffect(() => {
subscribeTarget.subscribe(link);
return () => {
link.onUpdateUsed[UnmountedCallback] = true
Expand Down
29 changes: 18 additions & 11 deletions src/__tests__/Batch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,51 +119,56 @@ test('object: should rerender used via nested batch promised', async () => {
act(() => {
expect(() => result.current.batch(() => {
executed = true;
expect(renderTimes).toStrictEqual(3);
expect(renderTimes).toStrictEqual(2);
result.current.set(10000)
expect(renderTimes).toStrictEqual(3);
expect(renderTimes).toStrictEqual(2);
})).toThrow(`StateLink is used incorrectly. Attempted 'read promised state' at '/'`)
})
expect(executed).toBeFalsy()

act(() => {
expect(() => result.current.batch(() => {
executed = true;
expect(renderTimes).toStrictEqual(3);
expect(renderTimes).toStrictEqual(2);
result.current.set(10000)
expect(renderTimes).toStrictEqual(3);
expect(renderTimes).toStrictEqual(2);
}, {
ifPromised: 'reject'
})).toThrow(`StateLink is used incorrectly. Attempted 'read promised state' at '/'`)
})
expect(executed).toBeFalsy()

executed = false;
act(() => {
expect(() => result.current.batch(() => {
executed = true;
expect(renderTimes).toStrictEqual(2);
result.current.set(10000)
expect(renderTimes).toStrictEqual(3);
}, {
ifPromised: 'execute'
})).toThrow(`StateLink is used incorrectly. Attempted 'write promised state' at '/'`)
})
expect(executed).toBeTruthy()

executed = false;
act(() => {
result.current.batch(() => {
expect(renderTimes).toStrictEqual(3);
executed = true;
expect(renderTimes).toStrictEqual(2);
result.current.set(p => p + 100)
expect(renderTimes).toStrictEqual(3);
expect(renderTimes).toStrictEqual(2);
result.current.set(p => p + 100)
expect(renderTimes).toStrictEqual(3);
expect(renderTimes).toStrictEqual(2);
}, {
ifPromised: 'discard'
});
})
expect(executed).toBeFalsy()

executed = false;
act(() => {
result.current.batch(() => {
executed = true
expect(renderTimes).toStrictEqual(3);
result.current.set(p => p + 100)
expect(renderTimes).toStrictEqual(3);
Expand All @@ -173,11 +178,13 @@ test('object: should rerender used via nested batch promised', async () => {
ifPromised: 'postpone'
});
})
expect(executed).toBeFalsy()

await act(async () => {
await promise;
})
expect(renderTimes).toStrictEqual(3);
expect(executed).toBeTruthy()
expect(renderTimes).toStrictEqual(4);
expect(result.current.promised).toStrictEqual(false);
expect(result.current.error).toEqual(undefined);
expect(result.current.value).toEqual(300);
Expand Down

0 comments on commit dd31d87

Please sign in to comment.