1
- // localforage
2
1
import localforage from 'localforage' ;
3
2
4
3
import UserInteractionResource from '../resources/userInteractionResource' ;
5
- import { init } from './web-worker' ;
6
4
7
- const worker = init ( ) ;
5
+ import { gzip } from './compression' ;
8
6
9
- interface StorageSettings {
7
+ declare let self : WorkerGlobalScope ;
8
+
9
+ ( function ( self ) {
10
+ console . log ( "WorkerGlobalScope: " , WorkerGlobalScope )
11
+ console . log ( "self: " , self ) ;
12
+ } ) ( self ) ;
13
+
14
+ export interface StorageSettings {
10
15
resourceLimit : number ;
16
+ ttl : number ;
11
17
apiUrl : string ;
12
18
dataKey ?: string ;
13
19
}
14
20
15
21
const STORAGE_SETTINGS_DEFAULTS : Required < StorageSettings > = {
16
22
resourceLimit : 0 ,
23
+ ttl : 5000 ,
17
24
apiUrl : "" ,
18
25
dataKey : "events" ,
26
+
19
27
}
20
28
21
- export const StorageClient = ( function ( ) {
22
- let storageSettings : Required < StorageSettings > = STORAGE_SETTINGS_DEFAULTS ;
23
-
24
- return {
25
- init : function ( options : StorageSettings ) {
26
- if ( storageSettings . resourceLimit === 0 ) {
27
- storageSettings = {
28
- ...options ,
29
- dataKey : options . dataKey ? options . dataKey : storageSettings . dataKey
30
- } ;
31
- }
29
+ let storageSettings : Required < StorageSettings > = STORAGE_SETTINGS_DEFAULTS ;
30
+ let isAppLoaded : boolean = true ;
31
+
32
+ export async function init ( options : StorageSettings ) {
33
+ if ( storageSettings . resourceLimit === 0 ) {
34
+ storageSettings = {
35
+ ...options ,
36
+ dataKey : options . dataKey ? options . dataKey : storageSettings . dataKey ,
37
+ } ;
38
+ }
32
39
33
- return this ;
34
- } ,
40
+ reAttemptSync ( storageSettings . apiUrl , storageSettings . ttl , storageSettings . dataKey ) ;
41
+ }
35
42
36
- getConfig : function ( ) {
37
- return storageSettings ;
38
- } ,
43
+ export function getConfig ( ) {
44
+ return storageSettings ;
45
+ }
46
+
47
+ export async function handle ( data : UserInteractionResource ) {
48
+ let eventsData = await retrieveData ( storageSettings . dataKey ) as any [ ] ;
49
+
50
+ if ( eventsData ) {
51
+ if ( eventsData . length < storageSettings . resourceLimit ) {
52
+ saveData ( storageSettings . dataKey , [ data , ...eventsData ] ) ;
53
+ } else {
54
+ if ( self . navigator . onLine ) {
55
+ const compressedData = gzip ( eventsData ) as Uint8Array ;
56
+
57
+ syncData ( storageSettings . apiUrl , compressedData )
58
+ . then ( ( res ) => {
59
+ clearData ( ) ;
60
+ saveData ( storageSettings . dataKey , [ data ] ) ;
61
+ } )
62
+ . catch ( err => {
63
+ saveData ( storageSettings . dataKey , [ data , ...eventsData ] )
64
+ } )
65
+ }
66
+ else {
67
+ saveData ( storageSettings . dataKey , [ data , ...eventsData ] ) ;
68
+ }
69
+ }
70
+ } else {
71
+ saveData ( storageSettings . dataKey , [ data ] ) ;
72
+ }
73
+
74
+ }
39
75
40
- handle : async function (
41
- event : React . MouseEvent < HTMLElement , MouseEvent > , data : UserInteractionResource
42
- ) {
43
- let eventsData = await retrieveData ( storageSettings . dataKey ) as any [ ] ;
76
+ export function onAppClose ( ) {
77
+ isAppLoaded = false ;
78
+ reAttemptSync ( storageSettings . apiUrl , storageSettings . ttl , storageSettings . dataKey ) ;
79
+ }
44
80
81
+ function reAttemptSync ( url : string , ttl : number , key : string ) {
82
+
83
+ async function compressAndSend ( ) {
84
+ if ( self . navigator . onLine ) {
85
+ const eventsData = await retrieveData ( key ) as any [ ] ;
45
86
if ( eventsData ) {
46
- if ( eventsData . length < storageSettings . resourceLimit ) {
47
- saveData ( storageSettings . dataKey , [ data , ...eventsData ] ) ;
48
- } else {
49
- if ( window . navigator . onLine ) {
50
- const compressedData = await worker . gzip ( eventsData ) as Uint8Array ;
51
-
52
- syncData ( storageSettings . apiUrl , compressedData )
53
- . then ( ( res ) => {
54
- clearData ( ) ;
55
- saveData ( storageSettings . dataKey , [ data ] ) ;
56
- } )
57
- . catch ( err => {
58
- saveData ( storageSettings . dataKey , [ data , ...eventsData ] )
59
- } )
60
- }
61
- else {
62
- saveData ( storageSettings . dataKey , [ data , ...eventsData ] ) ;
63
- }
64
- }
87
+ const compressedData = gzip ( eventsData ) as Uint8Array ;
88
+
89
+ syncData ( url , compressedData )
90
+ . then ( ( res ) => {
91
+ clearData ( ) ;
92
+ } )
93
+ . catch ( err => {
94
+ console . log ( "error is" , err )
95
+ } ) ;
65
96
} else {
66
- saveData ( storageSettings . dataKey , [ data ] ) ;
97
+ if ( ! isAppLoaded ) {
98
+ // @ts -ignore
99
+ self . close ( ) ; // kill web worker
100
+ }
67
101
}
68
-
69
102
}
70
- } ;
71
- } ) ( ) ;
103
+ }
104
+
105
+ const interval = setInterval ( compressAndSend , ttl ) ;
72
106
107
+ }
73
108
74
- function retrieveData ( key : string ) {
109
+ export function retrieveData ( key : string ) {
75
110
return localforage . getItem ( key )
76
111
}
77
112
78
- function saveData ( key : string , data : UserInteractionResource [ ] ) : Promise < void > {
113
+ export function saveData ( key : string , data : UserInteractionResource [ ] ) : Promise < void > {
79
114
return localforage . setItem ( key , data )
80
115
. then ( ( result ) => {
81
116
// console.log(result)
@@ -85,11 +120,8 @@ function saveData(key: string, data: UserInteractionResource[]) : Promise<void>
85
120
} ) ;
86
121
}
87
122
88
- function clearData ( ) {
123
+ export function clearData ( ) {
89
124
localforage . clear ( )
90
- . then ( ( data ) => {
91
- console . log ( "data has been deleted successfully" , data )
92
- } )
93
125
}
94
126
95
127
export async function syncData ( url : string , data : Uint8Array ) {
@@ -107,4 +139,4 @@ export async function syncData(url: string, data: Uint8Array) {
107
139
body : data
108
140
} ) ;
109
141
return response . json ( ) ;
110
- }
142
+ }
0 commit comments