Skip to content
Source
Sign inSign up
TanStack/query

feat(notifyManager): batch method configuration #6600

MergedNathanHuisman(GitHub profile, opens in a new tab)merged 19 commits intomain fromrefs/pull/6600/headmerged by TkDodo View on GitHub

As discussed previously in #6592, adding a way to configure the batch scheduling behavior can be useful, e.g. to prevent unwanted rerenders in solid-js (although I haven't fully gotten to the bottom of why solid-query does this).

This pull request adds notifyManager.setBatchMethod(...)

Sets the method for batching. There are four methods:

  • { type: 'timer', timeout?: number }: The default is to use setTimeout with a timeout of 0. This has the effect of scheduling a javascript task in the event loop some time later.
  • { type: 'raf' }: This batches all updates in a requestAnimationFrame, so that it runs before the next frame is rendered by the browser.
  • { type: 'tick' }: This uses queueMicrotask to schedule a microtask to batch updates, which runs immediately after the current task is done.
  • { type: 'custom', fn: (() => void) => void}: The custom method allows you to specify your own way of scheduling batches

UPDATE: The API is now the following:

import { notifyManager } from '@tanstack/react-query'
// Schedule batches in the next microtask
notifyManager.setScheduler(queueMicrotask)
// Schedule batches before the next frame is rendered
notifyManager.setScheduler(requestAnimationFrame)
// Schedule batches some time in the future
notifyManager.setScheduler((cb) => setTimeout(cb, 10))

This API was inspired by the redux autoBatchEnhancer API

I added some tests, and documented the notifyManager while I was at it. I would appreciate feedback, because I've not written many docs before and I'm not 100% sure if I documented all the functionality correctly.

I also added a demonstration in the solid-start-streaming example to demonstrate how setTimeout (the default batch method) can sometimes result in layout shifts solidjs. I don't know if this is due to the way how solid works or if solid-query can adapt to prevent this from happening. Either way, changing the batching method to 'raf' or 'tick' also prevents it from happening.

    1. The latest updates on your projects. Learn more about Vercel for Git ↗︎

      1 Ignored Deployment
      NameStatusPreviewCommentsUpdated (UTC)
      query⬜️ Ignored (Inspect)Visit PreviewDec 31, 2023 0:14am
    2. This pull request is automatically built and testable in CodeSandbox.

      To see build info of the built libraries, click here or the icon next to each commit SHA.

      Latest deployment of this branch, based on commit c68a985eca4a76c79e33a01371bbf8258aba72c4:

      SandboxSource
      @tanstack/query-example-angular-basicConfiguration
      @tanstack/query-example-react-basic-typescriptConfiguration
      @tanstack/query-example-solid-basic-typescriptConfiguration
      @tanstack/query-example-svelte-basicConfiguration
      @tanstack/query-example-vue-basicConfiguration
    3. Although I would need to look deeper into this to be sure I suspect this may be beneficial for the Angular adapter too. setTimeout triggers change detection and queueMicrotask may well be more efficient. At the very least it's good for adapters to have options.

    4. ☁️ Nx Cloud Report

      CI is running/has finished running commands for commit c68a985eca4a76c79e33a01371bbf8258aba72c4. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

      📂 See all runs for this CI Pipeline Execution


      ✅ Successfully ran 1 target

    5. Sent with 💌 from NxCloud.

  • Codecov Report

    All modified and coverable lines are covered by tests :white_check_mark:

    Comparison is base (50230c5) 39.36% compared to head (c68a985) 56.41%.

    :exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

    Additional details and impacted files
    @@             Coverage Diff             @@
    ##             main    #6600       +/-   ##
    ===========================================
    + Coverage   39.36%   56.41%   +17.04%     
    ===========================================
      Files         163      101       -62     
      Lines        6632     4116     -2516     
      Branches     1319     1021      -298     
    ===========================================
    - Hits         2611     2322      -289     
    + Misses       3670     1575     -2095     
    + Partials      351      219      -132     

    :umbrella: View full report in Codecov by Sentry. :loudspeaker: Have feedback on the report? Share it here.

  • TkDodo reviewed

    great work, thank you 🙏

    OutdatedResolvedby NathanHuisman(GitHub profile, opens in a new tab)
    OutdatedResolvedby NathanHuisman(GitHub profile, opens in a new tab)
  • TkDodo reviewed

    OutdatedResolvedby NathanHuisman(GitHub profile, opens in a new tab)
  • OutdatedResolvedby NathanHuisman(GitHub profile, opens in a new tab)
  • TkDodo approved these changes

    Thanks. I just added a tiny refactoring to initialize scheduleFn where we declare it rather than calling setScheduler.

    https://github.com/TanStack/query/pull/6600/commits/c68a985eca4a76c79e33a01371bbf8258aba72c4

    I think this is ready to go 🚀 . Great work 🙌

  • TkDodo merged this3265ec3

  • TkDodo closed this

  • Sign in to join this conversation.