Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "refactor(core): unify focusManager and onlineManager"
This reverts commit f1cb1a6.
- Loading branch information
Showing
5 changed files
with
152 additions
and
140 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,76 @@ | ||
import { createEventManager } from './eventManager' | ||
|
||
export const createFocusManager = () => { | ||
const { setEventListener, subscribe, ...manager } = createEventManager([ | ||
'visibilitychange', | ||
'focus', | ||
]) | ||
|
||
return { | ||
subscribe, | ||
setEventListener, | ||
setFocused: manager.setValue, | ||
isFocused: (): boolean => { | ||
const value = manager.getValue() | ||
if (typeof value === 'boolean') { | ||
return value | ||
} | ||
import { Subscribable } from './subscribable' | ||
import { isServer } from './utils' | ||
|
||
class FocusManager extends Subscribable { | ||
private focused?: boolean | ||
private removeEventListener?: () => void | ||
|
||
protected onSubscribe(): void { | ||
if (!this.removeEventListener) { | ||
this.setDefaultEventListener() | ||
} | ||
} | ||
|
||
// document global can be unavailable in react native | ||
if (typeof document === 'undefined') { | ||
return true | ||
setEventListener( | ||
setup: (setFocused: (focused?: boolean) => void) => () => void | ||
): void { | ||
if (this.removeEventListener) { | ||
this.removeEventListener() | ||
} | ||
this.removeEventListener = setup(focused => { | ||
if (typeof focused === 'boolean') { | ||
this.setFocused(focused) | ||
} else { | ||
this.onFocus() | ||
} | ||
}) | ||
} | ||
|
||
setFocused(focused?: boolean): void { | ||
this.focused = focused | ||
|
||
if (focused) { | ||
this.onFocus() | ||
} | ||
} | ||
|
||
onFocus(): void { | ||
this.listeners.forEach(listener => { | ||
listener() | ||
}) | ||
} | ||
|
||
isFocused(): boolean { | ||
if (typeof this.focused === 'boolean') { | ||
return this.focused | ||
} | ||
|
||
// document global can be unavailable in react native | ||
if (typeof document === 'undefined') { | ||
return true | ||
} | ||
|
||
return [undefined, 'visible', 'prerender'].includes( | ||
document.visibilityState | ||
) | ||
} | ||
|
||
private setDefaultEventListener() { | ||
if (!isServer && window?.addEventListener) { | ||
this.setEventListener(onFocus => { | ||
const listener = () => onFocus() | ||
// Listen to visibillitychange and focus | ||
window.addEventListener('visibilitychange', listener, false) | ||
window.addEventListener('focus', listener, false) | ||
|
||
return [undefined, 'visible', 'prerender'].includes( | ||
document.visibilityState | ||
) | ||
}, | ||
return () => { | ||
// Be sure to unsubscribe if a new handler is set | ||
window.removeEventListener('visibilitychange', listener) | ||
window.removeEventListener('focus', listener) | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
|
||
export const focusManager = createFocusManager() | ||
export const focusManager = new FocusManager() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,76 @@ | ||
import { createEventManager } from './eventManager' | ||
|
||
export const createOnlineManager = () => { | ||
const { setEventListener, subscribe, ...manager } = createEventManager([ | ||
'online', | ||
'offline', | ||
]) | ||
|
||
return { | ||
subscribe, | ||
setEventListener, | ||
setOnline: manager.setValue, | ||
isOnline: (): boolean => { | ||
const value = manager.getValue() | ||
if (typeof value === 'boolean') { | ||
return value | ||
} | ||
import { Subscribable } from './subscribable' | ||
import { isServer } from './utils' | ||
|
||
class OnlineManager extends Subscribable { | ||
private online?: boolean | ||
private removeEventListener?: () => void | ||
|
||
protected onSubscribe(): void { | ||
if (!this.removeEventListener) { | ||
this.setDefaultEventListener() | ||
} | ||
} | ||
|
||
if ( | ||
typeof navigator === 'undefined' || | ||
typeof navigator.onLine === 'undefined' | ||
) { | ||
return true | ||
setEventListener( | ||
setup: (setOnline: (online?: boolean) => void) => () => void | ||
): void { | ||
if (this.removeEventListener) { | ||
this.removeEventListener() | ||
} | ||
this.removeEventListener = setup((online?: boolean) => { | ||
if (typeof online === 'boolean') { | ||
this.setOnline(online) | ||
} else { | ||
this.onOnline() | ||
} | ||
}) | ||
} | ||
|
||
setOnline(online?: boolean): void { | ||
this.online = online | ||
|
||
if (online) { | ||
this.onOnline() | ||
} | ||
} | ||
|
||
onOnline(): void { | ||
this.listeners.forEach(listener => { | ||
listener() | ||
}) | ||
} | ||
|
||
isOnline(): boolean { | ||
if (typeof this.online === 'boolean') { | ||
return this.online | ||
} | ||
|
||
if ( | ||
typeof navigator === 'undefined' || | ||
typeof navigator.onLine === 'undefined' | ||
) { | ||
return true | ||
} | ||
|
||
return navigator.onLine | ||
} | ||
|
||
private setDefaultEventListener() { | ||
if (!isServer && window?.addEventListener) { | ||
this.setEventListener(onOnline => { | ||
const listener = () => onOnline() | ||
// Listen to online | ||
window.addEventListener('online', listener, false) | ||
window.addEventListener('offline', listener, false) | ||
|
||
return navigator.onLine | ||
}, | ||
return () => { | ||
// Be sure to unsubscribe if a new handler is set | ||
window.removeEventListener('online', listener) | ||
window.removeEventListener('offline', listener) | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
|
||
export const onlineManager = createOnlineManager() | ||
export const onlineManager = new OnlineManager() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters