-
Notifications
You must be signed in to change notification settings - Fork 240
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
Beta Part 4: Part of Mega Dynamic Load/Unload support #1781
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,104 @@ | ||
import { getWindow, getDocument, getNavigator, isUndefined, isNullOrUndefined, attachEvent } from "@microsoft/applicationinsights-core-js"; | ||
import dynamicProto from "@microsoft/dynamicproto-js"; | ||
import { getWindow, getDocument, getNavigator, isUndefined, isNullOrUndefined, createUniqueNamespace, mergeEvtNamespace, eventOn, eventOff } from "@microsoft/applicationinsights-core-js"; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The refactoring here is mostly just removing the internal Offline class (for minufication purposes), the functionality does not change. |
||
export interface IOfflineListener { | ||
isOnline: () => boolean; | ||
isListening: () => boolean; | ||
unload: () => void; | ||
} | ||
|
||
function _disableEvents(target: any, evtNamespace: string | string[]) { | ||
eventOff(target, null, null, evtNamespace); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will remove ALL of the events on the target object that match the provided namespace. |
||
} | ||
|
||
/** | ||
* @description Monitors browser for offline events | ||
* @export default - Offline: Static instance of OfflineListener | ||
* @class OfflineListener | ||
* Create a new OfflineListener instance to monitor browser online / offline events | ||
* @param parentEvtNamespace - The parent event namespace to append to any specific events for this instance | ||
*/ | ||
export class OfflineListener { | ||
public static Offline = new OfflineListener; | ||
public isListening: boolean; | ||
|
||
constructor() { | ||
let _window = getWindow(); | ||
let _document = getDocument(); | ||
let isListening = false; | ||
let _onlineStatus: boolean = true; | ||
|
||
dynamicProto(OfflineListener, this, (_self) => { | ||
try { | ||
if (_window) { | ||
if (attachEvent(_window, "online", _setOnline)) { | ||
attachEvent(_window, "offline", _setOffline); | ||
isListening = true; | ||
} | ||
} | ||
|
||
if (_document) { | ||
// Also attach to the document.body or document | ||
let target:any = _document.body || _document; | ||
|
||
if (!isUndefined(target.ononline)) { | ||
target.ononline = _setOnline; | ||
target.onoffline = _setOffline; | ||
isListening = true; | ||
|
||
} | ||
} | ||
export function createOfflineListener(parentEvtNamespace?: string | string[]): IOfflineListener { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The event namespace is part of the changes for unload support, so the events are tagged with the current instance. |
||
let _document = getDocument(); | ||
var _navigator = getNavigator(); // Gets the window.navigator or workerNavigator depending on the global | ||
let _isListening: boolean = false; | ||
let _onlineStatus: boolean = true; | ||
let _evtNamespace = mergeEvtNamespace(createUniqueNamespace("OfflineListener"), parentEvtNamespace); | ||
|
||
if (isListening) { | ||
// We are listening to events so lets set the current status rather than assuming we are online #1538 | ||
var _navigator = getNavigator(); // Gets the window.navigator or workerNavigator depending on the global | ||
if (_navigator && !isNullOrUndefined(_navigator.onLine)) { // navigator.onLine is undefined in react-native | ||
_onlineStatus = _navigator.onLine; | ||
} | ||
try { | ||
if (_enableEvents(getWindow())) { | ||
_isListening = true; | ||
} | ||
|
||
if (_document) { | ||
// Also attach to the document.body or document | ||
let target:any = _document.body || _document; | ||
|
||
if (target.ononline) { | ||
if (_enableEvents(target)) { | ||
_isListening = true; | ||
} | ||
} catch (e) { | ||
|
||
// this makes react-native less angry | ||
isListening = false; | ||
} | ||
|
||
_self.isListening = isListening; | ||
|
||
_self.isOnline = (): boolean => { | ||
let result = true; | ||
var _navigator = getNavigator(); | ||
if (isListening) { | ||
result = _onlineStatus | ||
} else if (_navigator && !isNullOrUndefined(_navigator.onLine)) { // navigator.onLine is undefined in react-native | ||
result = _navigator.onLine; | ||
} | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
_self.isOffline = (): boolean => { | ||
return !_self.isOnline(); | ||
}; | ||
|
||
function _setOnline() { | ||
_onlineStatus = true; | ||
if (_isListening) { | ||
// We are listening to events so lets set the current status rather than assuming we are online #1538 | ||
if (_navigator && !isNullOrUndefined(_navigator.onLine)) { // navigator.onLine is undefined in react-native | ||
_onlineStatus = _navigator.onLine; | ||
} | ||
} | ||
} catch (e) { | ||
// this makes react-native less angry | ||
_isListening = false; | ||
} | ||
|
||
function _setOffline() { | ||
_onlineStatus = false; | ||
function _enableEvents(target: any): boolean { | ||
let enabled = false; | ||
if (target) { | ||
enabled = eventOn(target, "online", _setOnline, _evtNamespace); | ||
if (enabled) { | ||
eventOn(target, "offline", _setOffline, _evtNamespace); | ||
} | ||
}); | ||
} | ||
|
||
return enabled; | ||
} | ||
|
||
public isOnline(): boolean { | ||
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging | ||
return false; | ||
function _setOnline() { | ||
_onlineStatus = true; | ||
} | ||
|
||
public isOffline(): boolean { | ||
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging | ||
return false; | ||
function _setOffline() { | ||
_onlineStatus = false; | ||
} | ||
} | ||
|
||
export const Offline = OfflineListener.Offline; | ||
function _isOnline(): boolean { | ||
let result = true; | ||
if (_isListening) { | ||
result = _onlineStatus | ||
} else if (_navigator && !isNullOrUndefined(_navigator.onLine)) { // navigator.onLine is undefined in react-native | ||
result = _navigator.onLine; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
function _unload() { | ||
let win = getWindow(); | ||
if (win && _isListening) { | ||
_disableEvents(win, _evtNamespace); | ||
|
||
if (_document) { | ||
// Also attach to the document.body or document | ||
let target:any = _document.body || _document; | ||
if (!isUndefined(target.ononline)) { | ||
_disableEvents(target, _evtNamespace); | ||
} | ||
} | ||
|
||
_isListening = false; | ||
} | ||
} | ||
|
||
return { | ||
isOnline: _isOnline, | ||
isListening: () => _isListening, | ||
unload: _unload | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to have to spend some additional time once all of the code is merged, I already have some changes to help with the size but they are quite significant in the number of changes for a review -- so delaying for now and just incrementing the current minimum.