Skip to content

Commit a057829

Browse files
committed
refactor: explicitly mark package side effects
1 parent c6e6cdf commit a057829

17 files changed

+176
-104
lines changed

.eslintrc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"parser": "@typescript-eslint/parser",
3-
"plugins": ["@typescript-eslint"],
3+
"plugins": ["@typescript-eslint", "import"],
44
"extends": [
55
"plugin:@typescript-eslint/eslint-recommended",
66
"plugin:@typescript-eslint/recommended",
7+
"plugin:import/typescript",
78
"react-app",
89
"prettier"
910
],
@@ -26,6 +27,7 @@
2627
"ignoreParameters": true
2728
}
2829
],
29-
"no-shadow": "error"
30+
"no-shadow": "error",
31+
"import/no-cycle": "error"
3032
}
3133
}

package.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
"unpkg": "dist/react-query.development.js",
1515
"types": "types/index.d.ts",
1616
"module": "es/index.js",
17+
"sideEffects": [
18+
"es/react/setBatchUpdatesFn.js",
19+
"es/react/setLogger.js",
20+
"lib/react/setBatchUpdatesFn.js",
21+
"lib/react/setLogger.js"
22+
],
1723
"scripts": {
1824
"test": "is-ci \"test:ci\" \"test:dev\"",
1925
"test:dev": "npm run test:types && npm run test:eslint && jest --watch",
@@ -22,8 +28,8 @@
2228
"test:types": "tsc",
2329
"test:eslint": "eslint --ext .ts,.tsx ./src",
2430
"build": "yarn build:commonjs && yarn build:es && yarn build:umd && yarn build:types",
25-
"build:commonjs": "rimraf ./lib && cross-env BABEL_ENV=commonjs babel --extensions .ts,.tsx --ignore ./src/**/*.test.tsx ./src --out-dir lib",
26-
"build:es": "rimraf ./es && babel --extensions .ts,.tsx --ignore ./src/**/*.test.tsx ./src --out-dir es",
31+
"build:commonjs": "rimraf ./lib && cross-env BABEL_ENV=commonjs babel --extensions .ts,.tsx --ignore ./src/**/tests/**/* ./src --out-dir lib",
32+
"build:es": "rimraf ./es && babel --extensions .ts,.tsx --ignore ./src/**/tests/**/* ./src --out-dir es",
2733
"build:umd": "rimraf ./dist && cross-env NODE_ENV=production rollup -c && rollup-plugin-visualizer stats-react.json",
2834
"build:types": "rimraf ./types && tsc --project ./tsconfig.types.json && replace 'import type' 'import' ./types -r --silent && replace 'export type' 'export' ./types -r --silent",
2935
"now-build": "yarn && cd www && yarn && yarn build",
@@ -88,7 +94,7 @@
8894
"eslint-config-standard": "^14.1.1",
8995
"eslint-config-standard-react": "^9.2.0",
9096
"eslint-plugin-flowtype": "5.x",
91-
"eslint-plugin-import": "2.x",
97+
"eslint-plugin-import": "^2.22.1",
9298
"eslint-plugin-jsx-a11y": "6.x",
9399
"eslint-plugin-node": "^11.1.0",
94100
"eslint-plugin-prettier": "^3.1.3",

src/core/focusHandler.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { isServer, noop } from './utils'
2+
3+
let focusCallback: () => void = noop
4+
let removePreviousHandler: (() => void) | void
5+
6+
export function setFocusHandler(callback: (handler: () => void) => void) {
7+
if (removePreviousHandler) {
8+
removePreviousHandler()
9+
}
10+
removePreviousHandler = callback(() => {
11+
focusCallback()
12+
})
13+
}
14+
15+
export function initFocusHandler(callback: () => void) {
16+
// Set the callback to execute on focus
17+
focusCallback = callback
18+
19+
// Set a default focus handler if needed
20+
if (!removePreviousHandler)
21+
setFocusHandler(handleFocus => {
22+
if (!isServer && window?.addEventListener) {
23+
// Listen to visibillitychange and focus
24+
window.addEventListener('visibilitychange', handleFocus, false)
25+
window.addEventListener('focus', handleFocus, false)
26+
27+
return () => {
28+
// Be sure to unsubscribe if a new handler is set
29+
window.removeEventListener('visibilitychange', handleFocus)
30+
window.removeEventListener('focus', handleFocus)
31+
}
32+
}
33+
})
34+
}

src/core/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ export {
88
setUpdateFn,
99
} from './notifyManager'
1010
export { setLogger } from './logger'
11-
export { setFocusHandler } from './setFocusHandler'
12-
export { setOnlineHandler } from './setOnlineHandler'
11+
export { setFocusHandler } from './focusHandler'
12+
export { setOnlineHandler } from './onlineHandler'
1313
export {
1414
CancelledError,
1515
hashQueryKey,

src/core/onlineHandler.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { isServer, noop } from './utils'
2+
3+
let onlineCallback: () => void = noop
4+
let removePreviousHandler: (() => void) | void
5+
6+
export function setOnlineHandler(callback: (handler: () => void) => void) {
7+
if (removePreviousHandler) {
8+
removePreviousHandler()
9+
}
10+
removePreviousHandler = callback(() => {
11+
onlineCallback()
12+
})
13+
}
14+
15+
export function initOnlineHandler(callback: () => void) {
16+
// Set the callback to execute on online
17+
onlineCallback = callback
18+
19+
// Set a default focus handler if needed
20+
if (!removePreviousHandler) {
21+
setOnlineHandler(handleOnline => {
22+
if (!isServer && window?.addEventListener) {
23+
// Listen to online
24+
window.addEventListener('online', handleOnline, false)
25+
26+
return () => {
27+
// Be sure to unsubscribe if a new handler is set
28+
window.removeEventListener('online', handleOnline)
29+
}
30+
}
31+
})
32+
}
33+
}

src/core/queryClient.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ import type {
2121
QueryOptions,
2222
RefetchOptions,
2323
} from './types'
24-
import { notifyManager } from './notifyManager'
24+
import type { QueryState } from './query'
25+
import type { QueryCache } from './queryCache'
2526
import { QueriesObserver } from './queriesObserver'
26-
import { QueryCache } from './queryCache'
2727
import { QueryObserver } from './queryObserver'
28-
import { QueryState } from './query'
28+
import { initFocusHandler } from './focusHandler'
29+
import { initOnlineHandler } from './onlineHandler'
30+
import { notifyManager } from './notifyManager'
2931

3032
// TYPES
3133

@@ -55,6 +57,8 @@ export class QueryClient {
5557

5658
mount(): void {
5759
mountedClients.push(this)
60+
initFocusHandler(onFocus)
61+
initOnlineHandler(onOnline)
5862
}
5963

6064
unmount(): void {
@@ -338,7 +342,15 @@ export class QueryClient {
338342

339343
const mountedClients: QueryClient[] = []
340344

341-
export function onExternalEvent(type: 'focus' | 'online') {
345+
function onFocus() {
346+
onExternalEvent('focus')
347+
}
348+
349+
function onOnline() {
350+
onExternalEvent('online')
351+
}
352+
353+
function onExternalEvent(type: 'focus' | 'online') {
342354
if (isDocumentVisible() && isOnline()) {
343355
notifyManager.batch(() => {
344356
uniq(mountedClients.map(x => x.getCache())).forEach(cache => {

src/core/queryObserver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import type {
1919
ResultOptions,
2020
} from './types'
2121
import type { Query, QueryState, Action, FetchOptions } from './query'
22-
import { QueryClient } from './queryClient'
22+
import type { QueryClient } from './queryClient'
2323

2424
export interface QueryObserverConfig<
2525
TData = unknown,

src/core/setFocusHandler.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/core/setOnlineHandler.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/core/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { QueryFilters } from './utils'
1+
import type { QueryFilters } from './utils'
22

33
export type QueryKey = string | unknown[]
44

0 commit comments

Comments
 (0)