@ouroboros/browser 0.1.6 → 0.2.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/README.md +24 -0
- package/cookies.js +1 -1
- package/cookies.ts +1 -1
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/index.ts +1 -0
- package/network.d.ts +54 -0
- package/network.js +106 -0
- package/network.ts +127 -0
- package/package.json +5 -2
- package/pageVisibility.d.ts +40 -34
- package/pageVisibility.js +82 -87
- package/pageVisibility.ts +92 -96
- package/safeLocalStorage.d.ts +10 -10
- package/safeLocalStorage.js +10 -10
- package/safeLocalStorage.ts +11 -11
- package/tsconfig.json +0 -16
- package/tslint.json +0 -10
package/README.md
CHANGED
|
@@ -48,6 +48,18 @@ or
|
|
|
48
48
|
import { hash } from '@ouroboros/browser';
|
|
49
49
|
```
|
|
50
50
|
|
|
51
|
+
Import network into your code
|
|
52
|
+
|
|
53
|
+
```javascript
|
|
54
|
+
import network from '@ouroboros/browser/network';
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
or
|
|
58
|
+
|
|
59
|
+
```javascript
|
|
60
|
+
import { network } from '@ouroboros/browser';
|
|
61
|
+
```
|
|
62
|
+
|
|
51
63
|
Import pageVisibility into your code
|
|
52
64
|
|
|
53
65
|
```javascript
|
|
@@ -58,4 +70,16 @@ or
|
|
|
58
70
|
|
|
59
71
|
```javascript
|
|
60
72
|
import { pageVisibility } from '@ouroboros/browser';
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Import safeLocalStorage into your code
|
|
76
|
+
|
|
77
|
+
```javascript
|
|
78
|
+
import safeLocalStorage from '@ouroboros/browser/safeLocalStorage';
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
or
|
|
82
|
+
|
|
83
|
+
```javascript
|
|
84
|
+
import { safeLocalStorage } from '@ouroboros/browser';
|
|
61
85
|
```
|
package/cookies.js
CHANGED
package/cookies.ts
CHANGED
|
@@ -56,7 +56,7 @@ function get(name: string, defaulReturn: string | null | undefined): string | ob
|
|
|
56
56
|
* @return {void}
|
|
57
57
|
*/
|
|
58
58
|
function remove(name: string, domain?: string, path?: string): void {
|
|
59
|
-
set(name, '', -
|
|
59
|
+
set(name, '', -86400, domain, path);
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
/**
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { default as clipboard } from './clipboard';
|
|
2
2
|
export { default as cookies } from './cookies';
|
|
3
3
|
export { default as hash } from './hash';
|
|
4
|
+
export { default as network } from './network';
|
|
4
5
|
export { default as pageVisibility } from './pageVisibility';
|
|
5
6
|
export { default as safeLocalStorage } from './safeLocalStorage';
|
package/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { default as clipboard } from './clipboard';
|
|
2
2
|
export { default as cookies } from './cookies';
|
|
3
3
|
export { default as hash } from './hash';
|
|
4
|
+
export { default as network } from './network';
|
|
4
5
|
export { default as pageVisibility } from './pageVisibility';
|
|
5
6
|
export { default as safeLocalStorage } from './safeLocalStorage';
|
package/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { default as clipboard } from './clipboard';
|
|
2
2
|
export { default as cookies } from './cookies';
|
|
3
3
|
export { default as hash } from './hash';
|
|
4
|
+
export { default as network } from './network';
|
|
4
5
|
export { default as pageVisibility } from './pageVisibility';
|
|
5
6
|
export { default as safeLocalStorage } from './safeLocalStorage';
|
package/network.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
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
|
+
import Subscribe, { SubscribeCallback, SubscribeReturn } from '@ouroboros/subscribe';
|
|
11
|
+
/**
|
|
12
|
+
* Network
|
|
13
|
+
*
|
|
14
|
+
* Extends the Subscribe class to be created once and exported
|
|
15
|
+
*
|
|
16
|
+
* @name Network
|
|
17
|
+
* @extends Subscribe
|
|
18
|
+
*/
|
|
19
|
+
declare class Network extends Subscribe {
|
|
20
|
+
/**
|
|
21
|
+
* Constructor
|
|
22
|
+
*
|
|
23
|
+
* Creates a new instance and returns it
|
|
24
|
+
*
|
|
25
|
+
* @name Network
|
|
26
|
+
* @access private
|
|
27
|
+
* @returns Network
|
|
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 network: Network;
|
|
54
|
+
export default network;
|
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.
|
|
3
|
+
"version": "0.2.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/
|
|
39
|
+
"@ouroboros/subscribe": "^1.2.2",
|
|
40
|
+
"@ouroboros/tools": "^0.5.3"
|
|
38
41
|
}
|
|
39
42
|
}
|
package/pageVisibility.d.ts
CHANGED
|
@@ -7,42 +7,48 @@
|
|
|
7
7
|
* @copyright Ouroboros Coding Inc.
|
|
8
8
|
* @created 2018-08-01
|
|
9
9
|
*/
|
|
10
|
-
|
|
10
|
+
import Subscribe, { SubscribeCallback, SubscribeReturn } from '@ouroboros/subscribe';
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
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
|
-
*
|
|
14
|
+
* Extends the Subscribe class to be created once and exported
|
|
36
15
|
*
|
|
37
|
-
* @name
|
|
38
|
-
* @
|
|
39
|
-
* @param {function} callback
|
|
40
|
-
* @return {boolean}
|
|
16
|
+
* @name PageVisibility
|
|
17
|
+
* @extends Subscribe
|
|
41
18
|
*/
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
|
12
|
-
const
|
|
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
|
|
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
|
-
|
|
23
|
+
apiAvailable = true;
|
|
24
24
|
}
|
|
25
25
|
// Else, loop through the prefixes
|
|
26
26
|
else {
|
|
27
|
-
for (const prefix of
|
|
27
|
+
for (const prefix of prefixes) {
|
|
28
28
|
// If the prefixes version exists
|
|
29
29
|
if ((prefix + 'Hidden') in document) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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
|
|
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
|
-
//
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
*
|
|
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
|
-
*
|
|
57
|
+
* Extends the Subscribe class to be created once and exported
|
|
79
58
|
*
|
|
80
|
-
* @name
|
|
81
|
-
* @
|
|
82
|
-
* @param {function} callback Function to call on visibility changes
|
|
83
|
-
* @return {boolean}
|
|
59
|
+
* @name PageVisibility
|
|
60
|
+
* @extends Subscribe
|
|
84
61
|
*/
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
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
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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;
|
package/pageVisibility.ts
CHANGED
|
@@ -8,37 +8,38 @@
|
|
|
8
8
|
* @created 2018-08-01
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
//
|
|
12
|
-
|
|
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
|
|
16
|
-
const
|
|
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
|
|
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
|
-
|
|
30
|
+
apiAvailable = true;
|
|
30
31
|
}
|
|
31
32
|
|
|
32
33
|
// Else, loop through the prefixes
|
|
33
34
|
else {
|
|
34
|
-
for(const prefix of
|
|
35
|
+
for(const prefix of prefixes) {
|
|
35
36
|
|
|
36
37
|
// If the prefixes version exists
|
|
37
38
|
if((prefix + 'Hidden') in document) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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
|
|
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
|
-
//
|
|
59
|
-
|
|
60
|
-
|
|
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
|
-
*
|
|
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
|
-
*
|
|
69
|
+
* Extends the Subscribe class to be created once and exported
|
|
91
70
|
*
|
|
92
|
-
* @name
|
|
93
|
-
* @
|
|
94
|
-
* @param {function} callback Function to call on visibility changes
|
|
95
|
-
* @return {boolean}
|
|
71
|
+
* @name PageVisibility
|
|
72
|
+
* @extends Subscribe
|
|
96
73
|
*/
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
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
|
-
|
|
130
|
-
|
|
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
|
-
|
|
136
|
-
|
|
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
|
-
|
|
139
|
-
|
|
134
|
+
// Call the parent unsubscribe
|
|
135
|
+
const bRet = super.unsubscribe(callback);
|
|
140
136
|
|
|
141
|
-
//
|
|
142
|
-
|
|
137
|
+
// If it was successful and we have no more callbacks
|
|
138
|
+
if(bRet && this.subscribeCallbacks.length === 0) {
|
|
143
139
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
document.removeEventListener(dVis.event, track);
|
|
140
|
+
// Remove the event listener
|
|
141
|
+
document.removeEventListener(properties.event, track);
|
|
147
142
|
}
|
|
148
|
-
}
|
|
149
143
|
|
|
150
|
-
|
|
151
|
-
|
|
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
|
-
|
|
156
|
-
export default pageVisibility;
|
|
152
|
+
export default pageVisibility;
|
package/safeLocalStorage.d.ts
CHANGED
|
@@ -16,22 +16,22 @@
|
|
|
16
16
|
*
|
|
17
17
|
* @name bool
|
|
18
18
|
* @access public
|
|
19
|
-
* @param
|
|
20
|
-
* @param
|
|
21
|
-
* @
|
|
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
|
-
*
|
|
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
|
|
33
|
-
* @param
|
|
34
|
-
* @
|
|
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
|
|
46
|
-
* @param
|
|
47
|
-
* @
|
|
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: {
|
package/safeLocalStorage.js
CHANGED
|
@@ -16,25 +16,25 @@
|
|
|
16
16
|
*
|
|
17
17
|
* @name bool
|
|
18
18
|
* @access public
|
|
19
|
-
* @param
|
|
20
|
-
* @param
|
|
21
|
-
* @
|
|
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
|
-
*
|
|
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
|
|
36
|
-
* @param
|
|
37
|
-
* @
|
|
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
|
|
52
|
-
* @param
|
|
53
|
-
* @
|
|
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);
|
package/safeLocalStorage.ts
CHANGED
|
@@ -17,11 +17,11 @@
|
|
|
17
17
|
*
|
|
18
18
|
* @name bool
|
|
19
19
|
* @access public
|
|
20
|
-
* @param
|
|
21
|
-
* @param
|
|
22
|
-
* @
|
|
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
|
-
*
|
|
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
|
|
40
|
-
* @param
|
|
41
|
-
* @
|
|
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
|
|
57
|
-
* @param
|
|
58
|
-
* @
|
|
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);
|
package/tsconfig.json
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"allowJs": true,
|
|
4
|
-
"allowSyntheticDefaultImports": true,
|
|
5
|
-
"declaration": true,
|
|
6
|
-
"esModuleInterop": true,
|
|
7
|
-
"isolatedModules": true,
|
|
8
|
-
"lib": ["es2019", "dom", "dom.iterable"],
|
|
9
|
-
"moduleResolution": "node",
|
|
10
|
-
"noEmit": false,
|
|
11
|
-
"strict": true,
|
|
12
|
-
"target": "esnext"
|
|
13
|
-
},
|
|
14
|
-
"include": ["./"],
|
|
15
|
-
"exclude": []
|
|
16
|
-
}
|