Skip to content

Commit

Permalink
remove unregisters
Browse files Browse the repository at this point in the history
  • Loading branch information
shuding committed Jul 15, 2021
1 parent 0c42e06 commit 17e8792
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ export interface Configuration<
}

export type Provider = {
setupOnFocus: (cb: () => void) => () => void
setupOnReconnect: (cb: () => void) => () => void
setupOnFocus: (cb: () => void) => void
setupOnReconnect: (cb: () => void) => void
}

export type SWRHook = <Data = any, Error = any>(
Expand Down
49 changes: 14 additions & 35 deletions src/utils/web-preset.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Provider } from '../types'
import { isUndefined } from './helper'
import { isUndefined, UNDEFINED } from './helper'

/**
* Due to bug https://bugs.chromium.org/p/chromium/issues/detail?id=678075,
Expand All @@ -10,23 +10,11 @@ import { isUndefined } from './helper'
*/
let online = true
const isOnline = () => online
const createNoop = (ret?: any) => () => ret
const add = 'addEventListener'
const remove = 'removeEventListener'

type EventAction = 'addEventListener' | 'removeEventListener'

const hasWindow = typeof window !== 'undefined'
const addWindowEventListener =
typeof window !== 'undefined' ? window.addEventListener : UNDEFINED
const hasDocument = typeof document !== 'undefined'

// For node and React Native, `add/removeEventListener` doesn't exist on window.
const windowEventListener = (action: EventAction) =>
hasWindow && !isUndefined(window[action])
? window[action].bind(window)
: createNoop()
const documentEventListener = (action: EventAction) =>
hasDocument ? document[action].bind(document) : createNoop()

const isDocumentVisible = () => {
const visibilityState = hasDocument && document.visibilityState
if (!isUndefined(visibilityState)) {
Expand All @@ -37,31 +25,22 @@ const isDocumentVisible = () => {

const setupOnFocus = (cb: () => void) => {
// focus revalidate
documentEventListener(add)('visibilitychange', cb)
windowEventListener(add)('focus', cb)
return () => {
documentEventListener(remove)('visibilitychange', cb)
windowEventListener(remove)('focus', cb)
}
if (hasDocument) document.addEventListener('visibilitychange', cb)
if (addWindowEventListener) addWindowEventListener('focus', cb)
}

const setupOnReconnect = (cb: () => void) => {
const onOnline = () => {
online = true
cb()
}
const onOffline = () => {
online = false
}
// reconnect revalidate
windowEventListener(add)('online', onOnline)
if (addWindowEventListener)
addWindowEventListener('online', () => {
online = true
cb()
})
// nothing to revalidate, just update the status
windowEventListener(add)('offline', onOffline)

return () => {
windowEventListener(remove)('online', onOnline)
windowEventListener(remove)('offline', onOffline)
}
if (addWindowEventListener)
addWindowEventListener('offline', () => {
online = false
})
}

export const preset = {
Expand Down

0 comments on commit 17e8792

Please sign in to comment.