setupListeners
refetchOnFocus
とrefetchOnReconnect
の動作を有効にするためのユーティリティです。ストアーのdispatch
メソッドが必要です。setupListeners(store.dispatch)
を呼び出すと、推奨される既定値を使用してリスナーが設定されますが、より詳細な制御のためにコールバックを提供することもできます。
setupListenersの既定の構成
let initialized = false
export function setupListeners(
dispatch: ThunkDispatch<any, any, any>,
customHandler?: (
dispatch: ThunkDispatch<any, any, any>,
actions: {
onFocus: typeof onFocus
onFocusLost: typeof onFocusLost
onOnline: typeof onOnline
onOffline: typeof onOffline
},
) => () => void,
) {
function defaultHandler() {
const handleFocus = () => dispatch(onFocus())
const handleFocusLost = () => dispatch(onFocusLost())
const handleOnline = () => dispatch(onOnline())
const handleOffline = () => dispatch(onOffline())
const handleVisibilityChange = () => {
if (window.document.visibilityState === 'visible') {
handleFocus()
} else {
handleFocusLost()
}
}
if (!initialized) {
if (typeof window !== 'undefined' && window.addEventListener) {
// Handle focus events
window.addEventListener(
'visibilitychange',
handleVisibilityChange,
false,
)
window.addEventListener('focus', handleFocus, false)
// Handle connection events
window.addEventListener('online', handleOnline, false)
window.addEventListener('offline', handleOffline, false)
initialized = true
}
}
const unsubscribe = () => {
window.removeEventListener('focus', handleFocus)
window.removeEventListener('visibilitychange', handleVisibilityChange)
window.removeEventListener('online', handleOnline)
window.removeEventListener('offline', handleOffline)
initialized = false
}
return unsubscribe
}
return customHandler
? customHandler(dispatch, { onFocus, onFocusLost, onOffline, onOnline })
: defaultHandler()
}
onFocus
、onFocusLost
、onOffline
、onOnline
はすべてコールバックに提供されるアクションであることに注意してください。さらに、これらのアクションはapi.internalActions
で利用でき、次のようにディスパッチすることで使用できます
手動onFocusイベント
dispatch(api.internalActions.onFocus())