1- // localforage
21import localforage from 'localforage' ;
32
43import UserInteractionResource from '../resources/userInteractionResource' ;
5- import { init } from './web-worker' ;
64
7- const worker = init ( ) ;
5+ import { gzip } from './compression' ;
86
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 {
1015 resourceLimit : number ;
16+ ttl : number ;
1117 apiUrl : string ;
1218 dataKey ?: string ;
1319}
1420
1521const STORAGE_SETTINGS_DEFAULTS : Required < StorageSettings > = {
1622 resourceLimit : 0 ,
23+ ttl : 5000 ,
1724 apiUrl : "" ,
1825 dataKey : "events" ,
26+
1927}
2028
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+ }
3239
33- return this ;
34- } ,
40+ reAttemptSync ( storageSettings . apiUrl , storageSettings . ttl , storageSettings . dataKey ) ;
41+ }
3542
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+ }
3975
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+ }
4480
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 [ ] ;
4586 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+ } ) ;
6596 } else {
66- saveData ( storageSettings . dataKey , [ data ] ) ;
97+ if ( ! isAppLoaded ) {
98+ // @ts -ignore
99+ self . close ( ) ; // kill web worker
100+ }
67101 }
68-
69102 }
70- } ;
71- } ) ( ) ;
103+ }
104+
105+ const interval = setInterval ( compressAndSend , ttl ) ;
72106
107+ }
73108
74- function retrieveData ( key : string ) {
109+ export function retrieveData ( key : string ) {
75110 return localforage . getItem ( key )
76111}
77112
78- function saveData ( key : string , data : UserInteractionResource [ ] ) : Promise < void > {
113+ export function saveData ( key : string , data : UserInteractionResource [ ] ) : Promise < void > {
79114 return localforage . setItem ( key , data )
80115 . then ( ( result ) => {
81116 // console.log(result)
@@ -85,11 +120,8 @@ function saveData(key: string, data: UserInteractionResource[]) : Promise<void>
85120 } ) ;
86121}
87122
88- function clearData ( ) {
123+ export function clearData ( ) {
89124 localforage . clear ( )
90- . then ( ( data ) => {
91- console . log ( "data has been deleted successfully" , data )
92- } )
93125}
94126
95127export async function syncData ( url : string , data : Uint8Array ) {
@@ -107,4 +139,4 @@ export async function syncData(url: string, data: Uint8Array) {
107139 body : data
108140 } ) ;
109141 return response . json ( ) ;
110- }
142+ }
0 commit comments