ts/net/NetworkMonitor.ts - StructureJS

StructureJS

0.15.2

A class based utility library for building modular and scalable web platform applications. Features opt-in classes and utilities which provide a solid foundation and toolset to build your next project.

File: ts/net/NetworkMonitor.ts

  1. import EventDispatcher from '../event/EventDispatcher';
  2. import NetworkMonitorEvent from '../event/NetworkMonitorEvent';
  3. import NavigatorEvents from '../event/native/NavigatorEvents';
  4.  
  5. /**
  6. * A helper class to detect network changes.
  7. *
  8. * @class NetworkMonitor
  9. * @constructor
  10. * @requires EventDispatcher
  11. * @requires NavigatorEvents
  12. * @requires NetworkMonitorEvent
  13. * @author Robert S. (www.codeBelt.com)
  14. * @static
  15. */
  16. class NetworkMonitor
  17. {
  18. /**
  19. * A reference to the EventDispatcher object.
  20. *
  21. * @property _eventDispatcher
  22. * @type {EventDispatcher}
  23. * @private
  24. * @static
  25. */
  26. private static _eventDispatcher:EventDispatcher = new EventDispatcher();
  27.  
  28. /**
  29. * A flag to determine if this class has already been initialized.
  30. *
  31. * @property _initialized
  32. * @type {boolean}
  33. * @private
  34. */
  35. private static _initialized:boolean = false;
  36.  
  37. constructor()
  38. {
  39. throw new Error('[NetworkMonitor] Do not instantiate the NetworkMonitor class because it is a static class.');
  40. }
  41.  
  42. /**
  43. * Returns the online status of the browser. The property returns a boolean value, with true for being online and false for being offline.
  44. * @example
  45. * NetworkMonitor.connected();
  46. * @method connected
  47. * @returns {boolean}
  48. * @static
  49. * @public
  50. */
  51. public static connected():boolean
  52. {
  53. NetworkMonitor._start();
  54.  
  55. return window.navigator.onLine;
  56. }
  57.  
  58. /**
  59. * Returns if the status type ('online' or 'offline') if computer or device is connected to the internet.
  60. * @example
  61. * NetworkMonitor.getStatus();
  62. * @method getStatus
  63. * @returns {string} Returns 'online' or 'offline'.
  64. * @public
  65. * @static
  66. */
  67. public static getStatus():string
  68. {
  69. NetworkMonitor._start();
  70.  
  71. return (this.connected()) ? NavigatorEvents.ONLINE : NavigatorEvents.OFFLINE;
  72. }
  73.  
  74. /**
  75. * Registers an event listener object with an NetworkMonitor object so that the listener receives notification of an event.
  76. * @example
  77. * NetworkMonitor.addEventListener(NetworkMonitorEvent.STATUS, this._handlerMethod, this);
  78. * _handlerMethod(event) {
  79. * console.log(event.status, event.connected);
  80. * }
  81. * @method addEventListener
  82. * @param type {String} The type of event.
  83. * @param callback {Function} The listener function that processes the event. This function must accept an Event object as its only parameter and must return nothing, as this example shows.
  84. * @param scope {any} Binds the scope to a particular object (scope is basically what "this" refers to in your function). This can be very useful in JavaScript because scope isn't generally maintained.
  85. * @param [priority=0] {int} Influences the order in which the listeners are called. Listeners with lower priorities are called after ones with higher priorities.
  86. * @static
  87. * @public
  88. */
  89. public static addEventListener(type:string, callback:Function, scope:any, priority:number = 0):void
  90. {
  91. NetworkMonitor._start();
  92.  
  93. NetworkMonitor._eventDispatcher.addEventListener(type, callback, scope, priority);
  94. }
  95.  
  96. /**
  97. * Removes a specified listener from the NetworkMonitor object.
  98. * @example
  99. * NetworkMonitor.removeEventListener(NetworkMonitorEvent.STATUS, this._handlerMethod, this);
  100. * _handlerMethod(event) {
  101. * console.log(event.status, event.connected);
  102. * }
  103. * @method removeEventListener
  104. * @param type {String} The type of event.
  105. * @param callback {Function} The listener object to remove.
  106. * @param scope {any} The scope of the listener object to be removed.
  107. * To keep things consistent this parameter is required.
  108. * @static
  109. * @public
  110. */
  111. public static removeEventListener(type:string, callback:Function, scope:any):void
  112. {
  113. NetworkMonitor._eventDispatcher.removeEventListener(type, callback, scope);
  114. }
  115.  
  116. /**
  117. * Adds the necessary event listeners to listen for the 'online' and 'offline' events.
  118. *
  119. * @method _start
  120. * @static
  121. * @private
  122. */
  123. private static _start():void
  124. {
  125. if (NetworkMonitor._initialized === true)
  126. {
  127. return;
  128. }
  129. else
  130. {
  131. NetworkMonitor._initialized = true;
  132. }
  133.  
  134. window.addEventListener(NavigatorEvents.ONLINE, NetworkMonitor._onNetworkMonitorEvent, false);
  135. window.addEventListener(NavigatorEvents.OFFLINE, NetworkMonitor._onNetworkMonitorEvent, false);
  136. }
  137.  
  138. /**
  139. * An event handler method when the native Window 'online' and 'offline' events are triggered.
  140. *
  141. * @method _onNetworkMonitorEvent
  142. * @param event
  143. * @private
  144. * @static
  145. */
  146. private static _onNetworkMonitorEvent(event):void
  147. {
  148. const type:string = (event) ? event.type : NetworkMonitor.getStatus();
  149. const networkMonitorEvent:NetworkMonitorEvent = new NetworkMonitorEvent(NetworkMonitorEvent.STATUS, false, false, type, NetworkMonitor.connected(), event);
  150.  
  151. NetworkMonitor._dispatchEvent(networkMonitorEvent);
  152. }
  153.  
  154. /**
  155. * <p>Dispatches an event within the NetworkMonitorEvent object.</p>
  156. * @method _dispatchEvent
  157. * @param event {NetworkMonitorEvent} The Event object that is dispatched into the event flow. You can create custom events, the only requirement is all events must
  158. * extend the {{#crossLink "NetworkMonitorEvent"}}{{/crossLink}}.
  159. * @static
  160. * @private
  161. */
  162. private static _dispatchEvent(event:NetworkMonitorEvent):void
  163. {
  164. NetworkMonitor._eventDispatcher.dispatchEvent(event);
  165. }
  166. }
  167.  
  168. export default NetworkMonitor;