Skip to content

Commit

Permalink
revamp batching implementation to batch in longer sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey committed Dec 11, 2023
1 parent 4b0c136 commit 8a7f08d
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions src/utils/batched-updates.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,43 @@
/* eslint-disable import/no-unresolved */
import { unstable_batchedUpdates } from 'react-dom';
import {unstable_batchedUpdates} from 'react-dom';

Check warning on line 2 in src/utils/batched-updates.js

View workflow job for this annotation

GitHub Actions / build (20)

Replace `unstable_batchedUpdates` with `·unstable_batchedUpdates·`
import {
unstable_scheduleCallback as scheduleCallback,
unstable_ImmediatePriority as ImmediatePriority,
unstable_scheduleCallback as scheduleCallback,

Check warning on line 4 in src/utils/batched-updates.js

View workflow job for this annotation

GitHub Actions / build (20)

Replace `····` with `··`
unstable_ImmediatePriority as ImmediatePriority,

Check warning on line 5 in src/utils/batched-updates.js

View workflow job for this annotation

GitHub Actions / build (20)

Delete `··`
} from 'scheduler';

import defaults from '../defaults';
import supports from './supported-features';

let isInsideBatchedSchedule = 0;
let batchedScheduled = false;

let batched = [];

const executeBatched = () => {
unstable_batchedUpdates(() => {

Check warning on line 16 in src/utils/batched-updates.js

View workflow job for this annotation

GitHub Actions / build (20)

Delete `··`
while (batched.length) {

Check warning on line 17 in src/utils/batched-updates.js

View workflow job for this annotation

GitHub Actions / build (20)

Delete `····`
const currentBatched = batched;

Check warning on line 18 in src/utils/batched-updates.js

View workflow job for this annotation

GitHub Actions / build (20)

Replace `············` with `······`
batched = [];

Check warning on line 19 in src/utils/batched-updates.js

View workflow job for this annotation

GitHub Actions / build (20)

Delete `······`
currentBatched.forEach(fn => fn());

Check warning on line 20 in src/utils/batched-updates.js

View workflow job for this annotation

GitHub Actions / build (20)

Replace `············currentBatched.forEach(fn` with `······currentBatched.forEach((fn)`
}

Check warning on line 21 in src/utils/batched-updates.js

View workflow job for this annotation

GitHub Actions / build (20)

Delete `····`
// important to reset it before exiting this function

Check warning on line 22 in src/utils/batched-updates.js

View workflow job for this annotation

GitHub Actions / build (20)

Replace `········` with `····`
// as React will dump everything right after
batchedScheduled = false;
});
}

export function batch(fn) {
// if we are in node/tests or nested schedule
if (
!defaults.batchUpdates ||
!supports.scheduling() ||
isInsideBatchedSchedule
) {
return unstable_batchedUpdates(fn);
}
// if we are in node/tests or nested schedule
if (
!defaults.batchUpdates ||
!supports.scheduling()
) {
return unstable_batchedUpdates(fn);
}

batched.push(fn);

isInsideBatchedSchedule = 0;
// Use ImmediatePriority as it has -1ms timeout
// https://github.com/facebook/react/blob/main/packages/scheduler/src/forks/Scheduler.js#L65
return scheduleCallback(ImmediatePriority, function scheduleBatchedUpdates() {
isInsideBatchedSchedule++;
unstable_batchedUpdates(fn);
isInsideBatchedSchedule--;
});
if (!batchedScheduled) {
batchedScheduled = true;
return scheduleCallback(ImmediatePriority, executeBatched);
}
}

0 comments on commit 8a7f08d

Please sign in to comment.