@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/README.md +24 -0
- package/hash.d.ts +4 -16
- package/hash.js +74 -69
- package/hash.ts +96 -83
- 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/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);
|