@ouroboros/browser 0.1.7 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/network.js ADDED
@@ -0,0 +1,106 @@
1
+ /**
2
+ * Network
3
+ *
4
+ * Tracks whether the browser is online / offline
5
+ *
6
+ * @author Chris Nasr <chris@ouroboroscoding.com>
7
+ * @copyright Ouroboros Coding Inc.
8
+ * @created 2024-04-09
9
+ */
10
+ // Ouroboros modules
11
+ import Subscribe from '@ouroboros/subscribe';
12
+ /**
13
+ * Track Offline
14
+ *
15
+ * The actual function passed to the event so that we only have one event
16
+ * listener
17
+ *
18
+ * @name trackOffline
19
+ * @access private
20
+ */
21
+ function trackOffline() {
22
+ // Set the data so the instance notifies subscribers
23
+ network.set(false);
24
+ }
25
+ /**
26
+ * Track Online
27
+ *
28
+ * The actual function passed to the event so that we only have one event
29
+ * listener
30
+ *
31
+ * @name trackOnline
32
+ * @access private
33
+ */
34
+ function trackOnline() {
35
+ // Set the data so the instance notifies subscribers
36
+ network.set(true);
37
+ }
38
+ /**
39
+ * Network
40
+ *
41
+ * Extends the Subscribe class to be created once and exported
42
+ *
43
+ * @name Network
44
+ * @extends Subscribe
45
+ */
46
+ class Network extends Subscribe {
47
+ /**
48
+ * Constructor
49
+ *
50
+ * Creates a new instance and returns it
51
+ *
52
+ * @name Network
53
+ * @access private
54
+ * @returns Network
55
+ */
56
+ constructor() {
57
+ // Get the current values and init the instance with them
58
+ super(navigator.onLine);
59
+ }
60
+ /**
61
+ * Subscribe
62
+ *
63
+ * Overrides parent subscribe to handle adding the window event
64
+ *
65
+ * @name subscribe
66
+ * @access public
67
+ * @param callback The function to call when the state changes
68
+ * @returns object
69
+ */
70
+ subscribe(callback) {
71
+ // If we have no current subscribers
72
+ if (this.subscribeCallbacks.length === 0) {
73
+ // Add the event listeners
74
+ window.addEventListener('online', trackOnline);
75
+ window.addEventListener('offline', trackOffline);
76
+ }
77
+ // Call the parent subscribe and return
78
+ return super.subscribe(callback);
79
+ }
80
+ /**
81
+ * Unsubscribe
82
+ *
83
+ * Overrides parent unsubscribe to handle removing the window event
84
+ *
85
+ * @name unsubscribe
86
+ * @access public
87
+ * @param callback The callback to remove from the list
88
+ * @returns boolean
89
+ */
90
+ unsubscribe(callback) {
91
+ // Call the parent unsubscribe
92
+ const bRet = super.unsubscribe(callback);
93
+ // If it was successful and we have no more callbacks
94
+ if (bRet && this.subscribeCallbacks.length === 0) {
95
+ // Remove the event listeners
96
+ window.removeEventListener('online', trackOnline);
97
+ window.removeEventListener('offline', trackOffline);
98
+ }
99
+ // Return
100
+ return bRet;
101
+ }
102
+ }
103
+ // Create an instance of the class
104
+ const network = new Network();
105
+ // Default export
106
+ export default network;
package/network.ts ADDED
@@ -0,0 +1,127 @@
1
+ /**
2
+ * Network
3
+ *
4
+ * Tracks whether the browser is online / offline
5
+ *
6
+ * @author Chris Nasr <chris@ouroboroscoding.com>
7
+ * @copyright Ouroboros Coding Inc.
8
+ * @created 2024-04-09
9
+ */
10
+
11
+ // Ouroboros modules
12
+ import Subscribe, {
13
+ SubscribeCallback,
14
+ SubscribeReturn
15
+ } from '@ouroboros/subscribe';
16
+
17
+ /**
18
+ * Track Offline
19
+ *
20
+ * The actual function passed to the event so that we only have one event
21
+ * listener
22
+ *
23
+ * @name trackOffline
24
+ * @access private
25
+ */
26
+ function trackOffline(): void {
27
+
28
+ // Set the data so the instance notifies subscribers
29
+ network.set(false);
30
+ }
31
+
32
+ /**
33
+ * Track Online
34
+ *
35
+ * The actual function passed to the event so that we only have one event
36
+ * listener
37
+ *
38
+ * @name trackOnline
39
+ * @access private
40
+ */
41
+ function trackOnline(): void {
42
+
43
+ // Set the data so the instance notifies subscribers
44
+ network.set(true);
45
+ }
46
+
47
+ /**
48
+ * Network
49
+ *
50
+ * Extends the Subscribe class to be created once and exported
51
+ *
52
+ * @name Network
53
+ * @extends Subscribe
54
+ */
55
+ class Network extends Subscribe {
56
+
57
+ /**
58
+ * Constructor
59
+ *
60
+ * Creates a new instance and returns it
61
+ *
62
+ * @name Network
63
+ * @access private
64
+ * @returns Network
65
+ */
66
+ constructor() {
67
+
68
+ // Get the current values and init the instance with them
69
+ super(navigator.onLine);
70
+ }
71
+
72
+ /**
73
+ * Subscribe
74
+ *
75
+ * Overrides parent subscribe to handle adding the window event
76
+ *
77
+ * @name subscribe
78
+ * @access public
79
+ * @param callback The function to call when the state changes
80
+ * @returns object
81
+ */
82
+ subscribe(callback: SubscribeCallback): SubscribeReturn {
83
+
84
+ // If we have no current subscribers
85
+ if(this.subscribeCallbacks.length === 0) {
86
+
87
+ // Add the event listeners
88
+ window.addEventListener('online', trackOnline);
89
+ window.addEventListener('offline', trackOffline);
90
+ }
91
+
92
+ // Call the parent subscribe and return
93
+ return super.subscribe(callback);
94
+ }
95
+
96
+ /**
97
+ * Unsubscribe
98
+ *
99
+ * Overrides parent unsubscribe to handle removing the window event
100
+ *
101
+ * @name unsubscribe
102
+ * @access public
103
+ * @param callback The callback to remove from the list
104
+ * @returns boolean
105
+ */
106
+ unsubscribe(callback: SubscribeCallback): boolean {
107
+
108
+ // Call the parent unsubscribe
109
+ const bRet = super.unsubscribe(callback);
110
+
111
+ // If it was successful and we have no more callbacks
112
+ if(bRet && this.subscribeCallbacks.length === 0) {
113
+
114
+ // Remove the event listeners
115
+ window.removeEventListener('online', trackOnline);
116
+ window.removeEventListener('offline', trackOffline);
117
+ }
118
+
119
+ // Return
120
+ return bRet;
121
+ }
122
+ }
123
+
124
+ // Create an instance of the class
125
+ const network = new Network();
126
+ // Default export
127
+ export default network;
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@ouroboros/browser",
3
- "version": "0.1.7",
3
+ "version": "0.3.0",
4
4
  "description": "Package to handle common browser functionality.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
7
7
  "scripts": {
8
8
  "build": "tslint -p tsconfig.json && tsc",
9
9
  "clean": "rm *.js *.d.ts",
10
+ "for_local": "npm install; tslint -p tsconfig.json && tsc; rm -Rf node_modules",
10
11
  "test": "echo \"Error: no test specified.\" && exit 1"
11
12
  },
12
13
  "repository": {
@@ -20,6 +21,7 @@
20
21
  "cookies",
21
22
  "hash",
22
23
  "local storage",
24
+ "network",
23
25
  "page visibility"
24
26
  ],
25
27
  "author": "Chris Nasr <chris@ouroboroscoding.com>",
@@ -34,6 +36,7 @@
34
36
  },
35
37
  "dependencies": {
36
38
  "@ouroboros/clone": "^1.0.1",
37
- "@ouroboros/tools": "^0.4.1"
39
+ "@ouroboros/subscribe": "^1.2.2",
40
+ "@ouroboros/tools": "^0.5.3"
38
41
  }
39
42
  }
@@ -7,42 +7,48 @@
7
7
  * @copyright Ouroboros Coding Inc.
8
8
  * @created 2018-08-01
9
9
  */
10
- type Callback = (...args: any[]) => {};
10
+ import Subscribe, { SubscribeCallback, SubscribeReturn } from '@ouroboros/subscribe';
11
11
  /**
12
- * Get
13
- *
14
- * Returns the current state of visibility
15
- *
16
- * @name get
17
- * @access public
18
- * @return {object | false}
19
- */
20
- export declare function get(): object | false;
21
- /**
22
- * Subscribe
23
- *
24
- * Track changes on page visibility
25
- *
26
- * @name subscribe
27
- * @access public
28
- * @param {function} callback Function to call on visibility changes
29
- * @return {boolean}
30
- */
31
- export declare function subscribe(callback: Callback): boolean;
32
- /**
33
- * Ubsubscribe
12
+ * Page Visibility
34
13
  *
35
- * Stop tracking changes in page visibility
14
+ * Extends the Subscribe class to be created once and exported
36
15
  *
37
- * @name unsubscribe
38
- * @access public
39
- * @param {function} callback
40
- * @return {boolean}
16
+ * @name PageVisibility
17
+ * @extends Subscribe
41
18
  */
42
- export declare function unsubscribe(callback: Callback): boolean;
43
- declare const pageVisibility: {
44
- get: typeof get;
45
- subscribe: typeof subscribe;
46
- unsubscribe: typeof unsubscribe;
47
- };
19
+ declare class PageVisibility extends Subscribe {
20
+ /**
21
+ * Constructor
22
+ *
23
+ * Creates a new instance and returns it
24
+ *
25
+ * @name PageVisibility
26
+ * @access private
27
+ * @returns PageVisibility
28
+ */
29
+ constructor();
30
+ /**
31
+ * Subscribe
32
+ *
33
+ * Overrides parent subscribe to handle adding the window event
34
+ *
35
+ * @name subscribe
36
+ * @access public
37
+ * @param callback The function to call when the state changes
38
+ * @returns object
39
+ */
40
+ subscribe(callback: SubscribeCallback): SubscribeReturn;
41
+ /**
42
+ * Unsubscribe
43
+ *
44
+ * Overrides parent unsubscribe to handle removing the window event
45
+ *
46
+ * @name unsubscribe
47
+ * @access public
48
+ * @param callback The callback to remove from the list
49
+ * @returns boolean
50
+ */
51
+ unsubscribe(callback: SubscribeCallback): boolean;
52
+ }
53
+ declare const pageVisibility: PageVisibility;
48
54
  export default pageVisibility;
package/pageVisibility.js CHANGED
@@ -7,30 +7,30 @@
7
7
  * @copyright Ouroboros Coding Inc.
8
8
  * @created 2018-08-01
9
9
  */
10
+ // Ouroboros modules
11
+ import Subscribe from '@ouroboros/subscribe';
10
12
  // Init the visibility property name and event values
11
- let bVis = false;
12
- const dVis = {
13
+ let apiAvailable = false;
14
+ const properties = {
13
15
  property: 'hidden',
14
16
  event: 'visibilitychange',
15
17
  state: 'visibilityState'
16
18
  };
17
- const lCallbacks = [];
18
- // Figure out the name of the visibility property and event
19
19
  // Valid browser prefixes
20
- const lPrefixes = ['moz', 'ms', 'o', 'webkit'];
21
- // If a prefix is required
20
+ const prefixes = ['moz', 'ms', 'o', 'webkit'];
21
+ // If a prefix is not required
22
22
  if ('hidden' in document) {
23
- bVis = true;
23
+ apiAvailable = true;
24
24
  }
25
25
  // Else, loop through the prefixes
26
26
  else {
27
- for (const prefix of lPrefixes) {
27
+ for (const prefix of prefixes) {
28
28
  // If the prefixes version exists
29
29
  if ((prefix + 'Hidden') in document) {
30
- dVis.property = prefix + 'Hidden';
31
- dVis.event = prefix + 'visibilitychange';
32
- dVis.state = prefix + 'VisibilityState';
33
- bVis = true;
30
+ properties.property = prefix + 'Hidden';
31
+ properties.event = prefix + 'visibilitychange';
32
+ properties.state = prefix + 'VisibilityState';
33
+ apiAvailable = true;
34
34
  break;
35
35
  }
36
36
  }
@@ -38,95 +38,90 @@ else {
38
38
  /**
39
39
  * Track
40
40
  *
41
- * The actual function passed to the event so that we only have one event listener
41
+ * The actual function passed to the event so that we only have one event
42
+ * listener
42
43
  *
43
44
  * @name track
44
45
  * @access private
45
- * @return {void}
46
46
  */
47
47
  function track() {
48
- // Call the callbacks and pass them bool and state values
49
- for (const f of lCallbacks) {
50
- f(document[dVis.property], document[dVis.state]);
51
- }
48
+ // Set the data so the instance notifies subscribers
49
+ pageVisibility.set({
50
+ property: document[properties.property],
51
+ state: document[properties.state]
52
+ });
52
53
  }
53
54
  /**
54
- * Get
55
- *
56
- * Returns the current state of visibility
57
- *
58
- * @name get
59
- * @access public
60
- * @return {object | false}
61
- */
62
- export function get() {
63
- // If the library is available
64
- if (bVis) {
65
- return {
66
- property: document[dVis.property],
67
- state: document[dVis.state]
68
- };
69
- }
70
- else {
71
- console.error('Page Visibility API not available');
72
- return false;
73
- }
74
- }
75
- /**
76
- * Subscribe
55
+ * Page Visibility
77
56
  *
78
- * Track changes on page visibility
57
+ * Extends the Subscribe class to be created once and exported
79
58
  *
80
- * @name subscribe
81
- * @access public
82
- * @param {function} callback Function to call on visibility changes
83
- * @return {boolean}
59
+ * @name PageVisibility
60
+ * @extends Subscribe
84
61
  */
85
- export function subscribe(callback) {
86
- // If the Page Visibility API is not available
87
- if (bVis === false) {
88
- console.error('Page Visibility API not available');
89
- return false;
62
+ class PageVisibility extends Subscribe {
63
+ /**
64
+ * Constructor
65
+ *
66
+ * Creates a new instance and returns it
67
+ *
68
+ * @name PageVisibility
69
+ * @access private
70
+ * @returns PageVisibility
71
+ */
72
+ constructor() {
73
+ // Get the current values and init the instance with them
74
+ super(apiAvailable ? {
75
+ property: document[properties.property],
76
+ state: document[properties.state]
77
+ } : null);
90
78
  }
91
- // Save the callback for later
92
- lCallbacks.push(callback);
93
- // Attach an event listener if this is our first event
94
- if (lCallbacks.length === 1) {
95
- document.addEventListener(dVis.event, track);
96
- }
97
- // Return ok
98
- return true;
99
- }
100
- /**
101
- * Ubsubscribe
102
- *
103
- * Stop tracking changes in page visibility
104
- *
105
- * @name unsubscribe
106
- * @access public
107
- * @param {function} callback
108
- * @return {boolean}
109
- */
110
- export function unsubscribe(callback) {
111
- // If the Page Visibility API is not available
112
- if (bVis === false) {
113
- console.error('Page Visibility API not available');
114
- return false;
79
+ /**
80
+ * Subscribe
81
+ *
82
+ * Overrides parent subscribe to handle adding the window event
83
+ *
84
+ * @name subscribe
85
+ * @access public
86
+ * @param callback The function to call when the state changes
87
+ * @returns object
88
+ */
89
+ subscribe(callback) {
90
+ // If we have no current subscribers
91
+ if (apiAvailable && this.subscribeCallbacks.length === 0) {
92
+ // Add the event listener
93
+ document.addEventListener(properties.event, track);
94
+ }
95
+ // Call the parent subscribe and return
96
+ return super.subscribe(callback);
115
97
  }
116
- // Find the index of the callback
117
- const iIndex = lCallbacks.indexOf(callback);
118
- // If it exists
119
- if (iIndex > -1) {
120
- // Remove the callback
121
- lCallbacks.splice(iIndex, 1);
122
- // Remove event listener if we have no more callbacks
123
- if (lCallbacks.length === 0) {
124
- document.removeEventListener(dVis.event, track);
98
+ /**
99
+ * Unsubscribe
100
+ *
101
+ * Overrides parent unsubscribe to handle removing the window event
102
+ *
103
+ * @name unsubscribe
104
+ * @access public
105
+ * @param callback The callback to remove from the list
106
+ * @returns boolean
107
+ */
108
+ unsubscribe(callback) {
109
+ // If there's no API
110
+ if (!apiAvailable) {
111
+ return false;
112
+ }
113
+ // Call the parent unsubscribe
114
+ const bRet = super.unsubscribe(callback);
115
+ // If it was successful and we have no more callbacks
116
+ if (bRet && this.subscribeCallbacks.length === 0) {
117
+ // Remove the event listener
118
+ document.removeEventListener(properties.event, track);
125
119
  }
120
+ // Return
121
+ return bRet;
126
122
  }
127
- // Return ok
128
- return true;
129
123
  }
124
+ // Create an instance of the class
125
+ const pageVisibility = new PageVisibility();
130
126
  // Default export
131
- const pageVisibility = { get, subscribe, unsubscribe };
132
127
  export default pageVisibility;