@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/pageVisibility.ts CHANGED
@@ -8,37 +8,38 @@
8
8
  * @created 2018-08-01
9
9
  */
10
10
 
11
- // Callback type
12
- type Callback = (...args: any[]) => {};
11
+ // Ouroboros modules
12
+ import Subscribe, {
13
+ SubscribeCallback,
14
+ SubscribeReturn
15
+ } from '@ouroboros/subscribe';
13
16
 
14
17
  // Init the visibility property name and event values
15
- let bVis = false;
16
- const dVis = {
18
+ let apiAvailable = false;
19
+ const properties = {
17
20
  property: 'hidden',
18
21
  event: 'visibilitychange',
19
22
  state: 'visibilityState'
20
23
  };
21
- const lCallbacks: Callback[] = []
22
24
 
23
- // Figure out the name of the visibility property and event
24
25
  // Valid browser prefixes
25
- const lPrefixes = ['moz', 'ms', 'o', 'webkit'];
26
+ const prefixes = ['moz', 'ms', 'o', 'webkit'];
26
27
 
27
- // If a prefix is required
28
+ // If a prefix is not required
28
29
  if('hidden' in document) {
29
- bVis = true;
30
+ apiAvailable = true;
30
31
  }
31
32
 
32
33
  // Else, loop through the prefixes
33
34
  else {
34
- for(const prefix of lPrefixes) {
35
+ for(const prefix of prefixes) {
35
36
 
36
37
  // If the prefixes version exists
37
38
  if((prefix + 'Hidden') in document) {
38
- dVis.property = prefix + 'Hidden';
39
- dVis.event = prefix + 'visibilitychange';
40
- dVis.state = prefix + 'VisibilityState';
41
- bVis = true;
39
+ properties.property = prefix + 'Hidden';
40
+ properties.event = prefix + 'visibilitychange';
41
+ properties.state = prefix + 'VisibilityState';
42
+ apiAvailable = true;
42
43
  break;
43
44
  }
44
45
  }
@@ -47,110 +48,105 @@ else {
47
48
  /**
48
49
  * Track
49
50
  *
50
- * The actual function passed to the event so that we only have one event listener
51
+ * The actual function passed to the event so that we only have one event
52
+ * listener
51
53
  *
52
54
  * @name track
53
55
  * @access private
54
- * @return {void}
55
56
  */
56
57
  function track(): void {
57
58
 
58
- // Call the callbacks and pass them bool and state values
59
- for(const f of lCallbacks) {
60
- f(document[dVis.property as keyof Document], document[dVis.state as keyof Document]);
61
- }
59
+ // Set the data so the instance notifies subscribers
60
+ pageVisibility.set({
61
+ property: document[properties.property as keyof Document],
62
+ state: document[properties.state as keyof Document]
63
+ });
62
64
  }
63
65
 
64
66
  /**
65
- * Get
66
- *
67
- * Returns the current state of visibility
68
- *
69
- * @name get
70
- * @access public
71
- * @return {object | false}
72
- */
73
- export function get(): object | false {
74
-
75
- // If the library is available
76
- if(bVis) {
77
- return {
78
- property: document[dVis.property as keyof Document],
79
- state: document[dVis.state as keyof Document]
80
- }
81
- } else {
82
- console.error('Page Visibility API not available');
83
- return false;
84
- }
85
- }
86
-
87
- /**
88
- * Subscribe
67
+ * Page Visibility
89
68
  *
90
- * Track changes on page visibility
69
+ * Extends the Subscribe class to be created once and exported
91
70
  *
92
- * @name subscribe
93
- * @access public
94
- * @param {function} callback Function to call on visibility changes
95
- * @return {boolean}
71
+ * @name PageVisibility
72
+ * @extends Subscribe
96
73
  */
97
- export function subscribe(callback: Callback): boolean {
98
-
99
- // If the Page Visibility API is not available
100
- if(bVis === false) {
101
- console.error('Page Visibility API not available');
102
- return false;
74
+ class PageVisibility extends Subscribe {
75
+
76
+ /**
77
+ * Constructor
78
+ *
79
+ * Creates a new instance and returns it
80
+ *
81
+ * @name PageVisibility
82
+ * @access private
83
+ * @returns PageVisibility
84
+ */
85
+ constructor() {
86
+
87
+ // Get the current values and init the instance with them
88
+ super(apiAvailable ? {
89
+ property: document[properties.property as keyof Document],
90
+ state: document[properties.state as keyof Document]
91
+ } : null);
103
92
  }
104
93
 
105
- // Save the callback for later
106
- lCallbacks.push(callback);
107
-
108
- // Attach an event listener if this is our first event
109
- if(lCallbacks.length === 1) {
110
- document.addEventListener(dVis.event, track);
111
- }
112
-
113
- // Return ok
114
- return true;
115
- }
116
-
117
- /**
118
- * Ubsubscribe
119
- *
120
- * Stop tracking changes in page visibility
121
- *
122
- * @name unsubscribe
123
- * @access public
124
- * @param {function} callback
125
- * @return {boolean}
126
- */
127
- export function unsubscribe(callback: Callback): boolean {
94
+ /**
95
+ * Subscribe
96
+ *
97
+ * Overrides parent subscribe to handle adding the window event
98
+ *
99
+ * @name subscribe
100
+ * @access public
101
+ * @param callback The function to call when the state changes
102
+ * @returns object
103
+ */
104
+ subscribe(callback: SubscribeCallback): SubscribeReturn {
105
+
106
+ // If we have no current subscribers
107
+ if(apiAvailable && this.subscribeCallbacks.length === 0) {
108
+
109
+ // Add the event listener
110
+ document.addEventListener(properties.event, track);
111
+ }
128
112
 
129
- // If the Page Visibility API is not available
130
- if(bVis === false) {
131
- console.error('Page Visibility API not available');
132
- return false;
113
+ // Call the parent subscribe and return
114
+ return super.subscribe(callback);
133
115
  }
134
116
 
135
- // Find the index of the callback
136
- const iIndex = lCallbacks.indexOf(callback);
117
+ /**
118
+ * Unsubscribe
119
+ *
120
+ * Overrides parent unsubscribe to handle removing the window event
121
+ *
122
+ * @name unsubscribe
123
+ * @access public
124
+ * @param callback The callback to remove from the list
125
+ * @returns boolean
126
+ */
127
+ unsubscribe(callback: SubscribeCallback): boolean {
128
+
129
+ // If there's no API
130
+ if(!apiAvailable) {
131
+ return false;
132
+ }
137
133
 
138
- // If it exists
139
- if(iIndex > -1) {
134
+ // Call the parent unsubscribe
135
+ const bRet = super.unsubscribe(callback);
140
136
 
141
- // Remove the callback
142
- lCallbacks.splice(iIndex, 1);
137
+ // If it was successful and we have no more callbacks
138
+ if(bRet && this.subscribeCallbacks.length === 0) {
143
139
 
144
- // Remove event listener if we have no more callbacks
145
- if(lCallbacks.length === 0) {
146
- document.removeEventListener(dVis.event, track);
140
+ // Remove the event listener
141
+ document.removeEventListener(properties.event, track);
147
142
  }
148
- }
149
143
 
150
- // Return ok
151
- return true;
144
+ // Return
145
+ return bRet;
146
+ }
152
147
  }
153
148
 
149
+ // Create an instance of the class
150
+ const pageVisibility = new PageVisibility();
154
151
  // Default export
155
- const pageVisibility = { get, subscribe, unsubscribe };
156
- export default pageVisibility;
152
+ export default pageVisibility;
@@ -16,22 +16,22 @@
16
16
  *
17
17
  * @name bool
18
18
  * @access public
19
- * @param {string} name The name of the local var to fetch
20
- * @param {string} defaultReturn The value to return if the var is not found
21
- * @return {boolean}
19
+ * @param name The name of the local var to fetch
20
+ * @param defaultReturn The value to return if the var is not found
21
+ * @returns The boolean value found in storage
22
22
  */
23
23
  export declare function bool(name: string, defaultReturn?: boolean): boolean;
24
24
  /**
25
- * json
25
+ * JSON
26
26
  *
27
27
  * Fetches a value from local storage or returns the default if no value is
28
28
  * found. Assumes data is stored in JSON
29
29
  *
30
30
  * @name json
31
31
  * @access public
32
- * @param {string} name The name of the local var to fetch
33
- * @param {string} defaultReturn The value to return if the var is not found
34
- * @return {any}
32
+ * @param name The name of the local var to fetch
33
+ * @param defaultReturn The value to return if the var is not found
34
+ * @returns The object or other JS value found in storage
35
35
  */
36
36
  export declare function json(name: string, defaultReturn: any): any;
37
37
  /**
@@ -42,9 +42,9 @@ export declare function json(name: string, defaultReturn: any): any;
42
42
  *
43
43
  * @name string
44
44
  * @access public
45
- * @param {string} name The name of the local var to fetch
46
- * @param {string} defaultReturn The value to return if the var is not found
47
- * @return {string}
45
+ * @param name The name of the local var to fetch
46
+ * @param defaultReturn The value to return if the var is not found
47
+ * @returns The string found in storage
48
48
  */
49
49
  export declare function string(name: string, defaultReturn: string): string;
50
50
  declare const safeLocaleStorage: {
@@ -16,25 +16,25 @@
16
16
  *
17
17
  * @name bool
18
18
  * @access public
19
- * @param {string} name The name of the local var to fetch
20
- * @param {string} defaultReturn The value to return if the var is not found
21
- * @return {boolean}
19
+ * @param name The name of the local var to fetch
20
+ * @param defaultReturn The value to return if the var is not found
21
+ * @returns The boolean value found in storage
22
22
  */
23
23
  export function bool(name, defaultReturn = false) {
24
24
  const value = localStorage.getItem(name);
25
25
  return value === null ? defaultReturn : (value === '' ? false : true);
26
26
  }
27
27
  /**
28
- * json
28
+ * JSON
29
29
  *
30
30
  * Fetches a value from local storage or returns the default if no value is
31
31
  * found. Assumes data is stored in JSON
32
32
  *
33
33
  * @name json
34
34
  * @access public
35
- * @param {string} name The name of the local var to fetch
36
- * @param {string} defaultReturn The value to return if the var is not found
37
- * @return {any}
35
+ * @param name The name of the local var to fetch
36
+ * @param defaultReturn The value to return if the var is not found
37
+ * @returns The object or other JS value found in storage
38
38
  */
39
39
  export function json(name, defaultReturn) {
40
40
  const value = localStorage.getItem(name);
@@ -48,9 +48,9 @@ export function json(name, defaultReturn) {
48
48
  *
49
49
  * @name string
50
50
  * @access public
51
- * @param {string} name The name of the local var to fetch
52
- * @param {string} defaultReturn The value to return if the var is not found
53
- * @return {string}
51
+ * @param name The name of the local var to fetch
52
+ * @param defaultReturn The value to return if the var is not found
53
+ * @returns The string found in storage
54
54
  */
55
55
  export function string(name, defaultReturn) {
56
56
  const value = localStorage.getItem(name);
@@ -17,11 +17,11 @@
17
17
  *
18
18
  * @name bool
19
19
  * @access public
20
- * @param {string} name The name of the local var to fetch
21
- * @param {string} defaultReturn The value to return if the var is not found
22
- * @return {boolean}
20
+ * @param name The name of the local var to fetch
21
+ * @param defaultReturn The value to return if the var is not found
22
+ * @returns The boolean value found in storage
23
23
  */
24
- export function bool(name: string, defaultReturn=false): boolean {
24
+ export function bool(name: string, defaultReturn = false): boolean {
25
25
  const value = localStorage.getItem(name);
26
26
  return value === null ? defaultReturn : (
27
27
  value === '' ? false : true
@@ -29,16 +29,16 @@ export function bool(name: string, defaultReturn=false): boolean {
29
29
  }
30
30
 
31
31
  /**
32
- * json
32
+ * JSON
33
33
  *
34
34
  * Fetches a value from local storage or returns the default if no value is
35
35
  * found. Assumes data is stored in JSON
36
36
  *
37
37
  * @name json
38
38
  * @access public
39
- * @param {string} name The name of the local var to fetch
40
- * @param {string} defaultReturn The value to return if the var is not found
41
- * @return {any}
39
+ * @param name The name of the local var to fetch
40
+ * @param defaultReturn The value to return if the var is not found
41
+ * @returns The object or other JS value found in storage
42
42
  */
43
43
  export function json(name: string, defaultReturn: any): any {
44
44
  const value = localStorage.getItem(name);
@@ -53,9 +53,9 @@ export function json(name: string, defaultReturn: any): any {
53
53
  *
54
54
  * @name string
55
55
  * @access public
56
- * @param {string} name The name of the local var to fetch
57
- * @param {string} defaultReturn The value to return if the var is not found
58
- * @return {string}
56
+ * @param name The name of the local var to fetch
57
+ * @param defaultReturn The value to return if the var is not found
58
+ * @returns The string found in storage
59
59
  */
60
60
  export function string(name: string, defaultReturn: string): string {
61
61
  const value = localStorage.getItem(name);