Skip to content

Issue #7487 fix: (query-broadcast-client-experimental) - removing query from one tab doesn't remove it from all tabs #9017

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

Merged
merged 6 commits into from
May 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/query-broadcast-client-experimental/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
"test:types:ts56": "node ../../node_modules/typescript56/lib/tsc.js --build",
"test:types:ts57": "node ../../node_modules/typescript57/lib/tsc.js --build",
"test:types:tscurrent": "tsc --build",
"test:lib": "vitest",
"test:lib:dev": "pnpm run test:lib --watch",
"test:build": "publint --strict && attw --pack",
"build": "tsup --tsconfig tsconfig.prod.json"
},
Expand Down Expand Up @@ -60,6 +62,8 @@
"broadcast-channel": "^7.0.0"
},
"devDependencies": {
"@testing-library/react": "^16.1.0",
"@vitejs/plugin-react": "^4.3.4",
"npm-run-all2": "^5.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { QueryClient } from '@tanstack/query-core'
import { beforeEach, describe, expect, it } from 'vitest'
import { broadcastQueryClient } from '..'
import type { QueryCache } from '@tanstack/query-core'

describe('broadcastQueryClient', () => {
let queryClient: QueryClient
let queryCache: QueryCache

beforeEach(() => {
queryClient = new QueryClient()
queryCache = queryClient.getQueryCache()
})

it('should subscribe to the query cache', async () => {
broadcastQueryClient({
queryClient,
broadcastChannel: 'test_channel',
})
expect(queryCache.hasListeners()).toBe(true)
})

it('should not have any listeners after cleanup', async () => {
const unsubscribe = broadcastQueryClient({
queryClient,
broadcastChannel: 'test_channel',
})
unsubscribe()
expect(queryCache.hasListeners()).toBe(false)
})
})
39 changes: 31 additions & 8 deletions packages/query-broadcast-client-experimental/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
queryClient,
broadcastChannel = 'tanstack-query',
options,
}: BroadcastQueryClientOptions) {
}: BroadcastQueryClientOptions): () => void {
let transaction = false
const tx = (cb: () => void) => {
transaction = true
Expand All @@ -27,13 +27,13 @@

const queryCache = queryClient.getQueryCache()

queryClient.getQueryCache().subscribe((queryEvent) => {
const unsubscribe = queryClient.getQueryCache().subscribe((queryEvent) => {
if (transaction) {
return
}

const {
query: { queryHash, queryKey, state },
query: { queryHash, queryKey, state, observers },
} = queryEvent

if (queryEvent.type === 'updated' && queryEvent.action.type === 'success') {
Expand All @@ -45,13 +45,21 @@
})
}

if (queryEvent.type === 'removed') {
if (queryEvent.type === 'removed' && observers.length > 0) {
channel.postMessage({
type: 'removed',
queryHash,
queryKey,
})
}

if (queryEvent.type === 'added') {
channel.postMessage({

Check warning on line 57 in packages/query-broadcast-client-experimental/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/query-broadcast-client-experimental/src/index.ts#L57

Added line #L57 was not covered by tests
type: 'added',
queryHash,
queryKey,
})
}
})

channel.onmessage = (action) => {
Expand All @@ -62,9 +70,9 @@
tx(() => {
const { type, queryHash, queryKey, state } = action

if (type === 'updated') {
const query = queryCache.get(queryHash)
const query = queryCache.get(queryHash)

Check warning on line 73 in packages/query-broadcast-client-experimental/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/query-broadcast-client-experimental/src/index.ts#L73

Added line #L73 was not covered by tests

if (type === 'updated') {
if (query) {
query.setState(state)
return
Expand All @@ -79,12 +87,27 @@
state,
)
} else if (type === 'removed') {
const query = queryCache.get(queryHash)

if (query) {
queryCache.remove(query)
}
} else if (type === 'added') {
if (query) {
query.setState(state)
return

Check warning on line 96 in packages/query-broadcast-client-experimental/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/query-broadcast-client-experimental/src/index.ts#L95-L96

Added lines #L95 - L96 were not covered by tests
}
queryCache.build(

Check warning on line 98 in packages/query-broadcast-client-experimental/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/query-broadcast-client-experimental/src/index.ts#L98

Added line #L98 was not covered by tests
queryClient,
{
queryKey,
queryHash,
},
state,
)
}
})
}
return () => {
unsubscribe()
channel.close()
}
}
14 changes: 14 additions & 0 deletions packages/query-broadcast-client-experimental/test-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import '@testing-library/jest-dom/vitest'
import { act, cleanup as cleanupRTL } from '@testing-library/react'
import { afterEach } from 'vitest'
import { notifyManager } from '@tanstack/query-core'

// https://testing-library.com/docs/react-testing-library/api#cleanup
afterEach(() => {
cleanupRTL()
})

// Wrap notifications with act to make sure React knows about React Query updates
notifyManager.setNotifyFunction((fn) => {
act(fn)
})
30 changes: 30 additions & 0 deletions packages/query-broadcast-client-experimental/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { defineConfig } from 'vitest/config'
import react from '@vitejs/plugin-react'

import packageJson from './package.json'

export default defineConfig({
plugins: [react()],
// fix from https://github.com/vitest-dev/vitest/issues/6992#issuecomment-2509408660
resolve: {
conditions: ['@tanstack/custom-condition'],
},
environments: {
ssr: {
resolve: {
conditions: ['@tanstack/custom-condition'],
},
},
},
test: {
name: packageJson.name,
dir: './src',
watch: false,
environment: 'jsdom',
setupFiles: ['test-setup.ts'],
coverage: { enabled: true, provider: 'istanbul', include: ['src/**/*'] },
typecheck: { enabled: true },
restoreMocks: true,
retry: process.env.CI ? 3 : 0,
},
})
10 changes: 8 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading