Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unstable scheduling behavior #217

Open
theKashey opened this issue Nov 17, 2023 · 0 comments
Open

Unstable scheduling behavior #217

theKashey opened this issue Nov 17, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@theKashey
Copy link

I've discovered a few problems with the current implementation mostly related to batch behavior.

Problem 1 - there is a bug

The problem is with boolean variables being not in the right place affecting the way batch works and behavior will change after the first call

Let me indicate the problem

export function batch(fn) {
  // if we are in node/tests or nested schedule
- not in the "nested schedule", but after the first batch call as well
  if (
    !defaults.batchUpdates ||
    !supports.scheduling() ||
    isInsideBatchedSchedule
  ) {
    return unstable_batchedUpdates(fn);
  }

-  isInsideBatchedSchedule = true;
  // 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--;
  });
}

Problem 2 - you are done, but we are not

There is a problem with information being stored in multiple places - after "update" many pieces are already seeing the correct information, but sweet-state might be a little late to the party.

The current workaround is to use batch to schedule next event

useEffect(() => {
- doSomething();
+ batch(doSomething);

or even emulate useDeferred

const state = useSweetState(realState);

const useSweetState = (state) => {
  const [value, setValue] = useState(state);
  useEffect(() => {
    // "sync" with sweet state event propagation
    batch(() => setValue(state));
  }, [state]); 
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants