-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1b4fe6d
commit 26352ca
Showing
6 changed files
with
117 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* eslint-env jest */ | ||
|
||
import React, { useState } from 'react'; | ||
import { mount } from 'enzyme'; | ||
|
||
import { createHook } from '../../components/hook'; | ||
import defaults from '../../defaults'; | ||
import { createStore, defaultRegistry } from '../../store'; | ||
import supports from '../../utils/supported-features'; | ||
import { batch } from '../batched-updates'; | ||
|
||
const Store = createStore({ | ||
initialState: { count: 0 }, | ||
actions: { | ||
increment: () => ({ getState, setState }) => { | ||
setState({ count: getState().count + 1 }); | ||
}, | ||
}, | ||
}); | ||
|
||
const useHook = createHook(Store); | ||
|
||
describe('batch', () => { | ||
const TestComponent = ({ children }) => { | ||
const [{ count }, actions] = useHook(); | ||
const [localCount, setLocalCount] = useState(0); | ||
const update = () => | ||
batch(() => { | ||
actions.increment(); | ||
setLocalCount(localCount + 1); | ||
}); | ||
|
||
return children(update, count, localCount); | ||
}; | ||
|
||
beforeEach(() => { | ||
defaultRegistry.stores.clear(); | ||
}); | ||
|
||
it('should batch updates with scheduling disabled', () => { | ||
const child = jest.fn().mockReturnValue(null); | ||
mount(<TestComponent>{child}</TestComponent>); | ||
const update = child.mock.calls[0][0]; | ||
update(); | ||
|
||
expect(child.mock.calls).toHaveLength(2); | ||
expect(child.mock.calls[1]).toEqual([expect.any(Function), 1, 1]); | ||
}); | ||
|
||
it('should batch updates with scheduling enabled', async () => { | ||
const supportsMock = jest | ||
.spyOn(supports, 'scheduling') | ||
.mockReturnValue(true); | ||
defaults.batchUpdates = true; | ||
|
||
const child = jest.fn().mockReturnValue(null); | ||
mount(<TestComponent>{child}</TestComponent>); | ||
const update = child.mock.calls[0][0]; | ||
update(); | ||
|
||
// scheduler uses timeouts on non-browser envs | ||
await new Promise((r) => setTimeout(r, 10)); | ||
|
||
expect(child.mock.calls).toHaveLength(2); | ||
expect(child.mock.calls[1]).toEqual([expect.any(Function), 1, 1]); | ||
|
||
supportsMock.mockRestore(); | ||
defaults.batchUpdates = false; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,33 @@ | ||
/* eslint-disable import/no-unresolved */ | ||
export { unstable_batchedUpdates } from 'react-dom'; | ||
import { unstable_batchedUpdates } from 'react-dom'; | ||
import { | ||
unstable_scheduleCallback as scheduleCallback, | ||
unstable_UserBlockingPriority as UserBlockingPriority, | ||
} from 'scheduler'; | ||
|
||
import defaults from '../defaults'; | ||
import supports from './supported-features'; | ||
|
||
let isInsideBatchedSchedule = false; | ||
|
||
export function batch(fn) { | ||
// if we are in node/tests or feature disabled or nested schedule | ||
if ( | ||
!defaults.batchUpdates || | ||
!supports.scheduling() || | ||
isInsideBatchedSchedule | ||
) { | ||
return unstable_batchedUpdates(fn); | ||
} | ||
|
||
isInsideBatchedSchedule = true; | ||
// Use UserBlockingPriority as it has max 250ms timeout | ||
// https://github.com/facebook/react/blob/master/packages/scheduler/src/forks/SchedulerNoDOM.js#L47 | ||
return scheduleCallback( | ||
UserBlockingPriority, | ||
function scheduleBatchedUpdates() { | ||
unstable_batchedUpdates(fn); | ||
isInsideBatchedSchedule = false; | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
/* eslint-disable import/no-unresolved */ | ||
export { unstable_batchedUpdates } from 'react-native'; | ||
export { unstable_batchedUpdates as batch } from 'react-native'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters