Skip to content

Commit

Permalink
Update dispatch queue for sagas
Browse files Browse the repository at this point in the history
  • Loading branch information
azmenak committed Dec 20, 2019
1 parent 51b7cfd commit ed69064
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
44 changes: 41 additions & 3 deletions src/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,35 @@ describe('useSagaReducer()', () => {
}
}

if (action.type === 'SET') {
return {
count: action.payload
}
}

return state
})

function* increment() {
const {count} = yield select()
testState = count
}

function* incrementAsync() {
const state = yield select()
testState = state
yield put({
type: 'SET',
payload: state.count + 1
})

yield delay(0)
const {count} = yield select()
testState = count
}

function* testSaga() {
yield takeEvery('INCREMENT', increment)
yield takeEvery('INCREMENT_ASYNC', incrementAsync)
}

function TestUseSagaReducer() {
Expand All @@ -139,22 +158,41 @@ describe('useSagaReducer()', () => {
>
TEST
</button>
<button
data-testid='button-async'
onClick={() => {
dispatch({
type: 'INCREMENT_ASYNC'
})
}}
>
TEST
</button>
</div>
)
}

const {getByTestId} = render(<TestUseSagaReducer />)
const button = getByTestId('button')
const buttonAsync = getByTestId('button-async')

fireEvent.click(button)
await act(flushPromiseQueue)

expect(testState).toEqual({count: 2})
expect(testState).toEqual(2)

fireEvent.click(button)
await act(flushPromiseQueue)

expect(testState).toEqual({count: 3})
expect(testState).toEqual(3)

fireEvent.click(buttonAsync)
await act(flushPromiseQueue)
// Add a second flush here, once to wait for microtasks queue from put,
// second one to wait for timer queue from delay task
await act(flushPromiseQueue)

expect(testState).toEqual(4)
})

it('provides context values in sagas passed to provider', async () => {
Expand Down
10 changes: 4 additions & 6 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import React, {
useMemo,
useEffect,
useContext,
useLayoutEffect,
Reducer,
ReducerState,
ReducerAction,
Expand All @@ -29,9 +28,6 @@ export const SagaProvider: React.FC<SagaProdiderProps> = (props) => {
return <SagaContext.Provider {...props} />
}

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

export function useSagaReducer<
S extends Saga<never[]>,
R extends Reducer<any, any>,
Expand All @@ -50,7 +46,7 @@ export function useSagaReducer<
)

const stateRef = useRef(state)
useIsomorphicLayoutEffect(() => {
useEffect(() => {
stateRef.current = state
}, [state])

Expand All @@ -60,8 +56,10 @@ export function useSagaReducer<
>> = useMemo(() => {
const channel = stdChannel()
const dispatch = (action: ReducerAction<R>) => {
setImmediate(channel.put, action)
reactDispatch(action)
Promise.resolve().then(() => {
channel.put(action)
})
}
const getState = () => stateRef.current

Expand Down

0 comments on commit ed69064

Please sign in to comment.