@@ -17,28 +17,24 @@ var SubscriptionStatus;
17
17
SubscriptionStatus [ "BLOCKED" ] = "blocked" ;
18
18
} ) ( SubscriptionStatus || ( SubscriptionStatus = { } ) ) ;
19
19
var WebPush = /** @class */ ( function ( ) {
20
- function WebPush ( appId , publicKey , controller , localStorageKey ) {
21
- var _this = this ;
20
+ function WebPush ( appId , publicKey , controller , syncInterval ) {
22
21
if ( controller === void 0 ) { controller = "/wpn/default" ; }
23
- if ( localStorageKey === void 0 ) { localStorageKey = "yii2-wpn-endpoint" ; }
22
+ if ( syncInterval === void 0 ) { syncInterval = 1000 * 60 * 10 ; }
23
+ this . LS = {
24
+ ENDPOINT : "yii2-wpn-endpoint" ,
25
+ LAST_SYNC : "yii2-wpn-last_sync"
26
+ } ;
24
27
this . appId = appId ;
25
28
this . publicKey = publicKey ;
26
29
this . controller = controller ;
27
- this . localStorageKey = localStorageKey ;
28
- var wpnBroadcast = new BroadcastChannel ( "yii2-web-push-notifications" ) ;
29
- wpnBroadcast . onmessage = function ( event ) {
30
- var _a = event . data , action = _a . action , campaignId = _a . campaignId ;
31
- $ . post ( _this . controller + "/report?appId=" + _this . appId , __assign ( { endpoint : localStorage . getItem ( _this . localStorageKey ) , action : action ,
32
- campaignId : campaignId } , _this . getCsrfParams ( ) ) , function ( ) {
33
- _this . log ( "Action reported" , action ) ;
34
- } ) . fail ( function ( error ) {
35
- _this . log ( "Action reporting failed" , action , error ) ;
36
- } ) ;
37
- } ;
30
+ this . syncInterval = syncInterval ;
38
31
}
39
- WebPush . prototype . setupRegistration = function ( swPath ) {
32
+ WebPush . prototype . setupRegistration = function ( swPath , successCb , failureCb , shouldMigrate ) {
40
33
var _this = this ;
41
34
if ( swPath === void 0 ) { swPath = "/sw.js" ; }
35
+ if ( successCb === void 0 ) { successCb = function ( status ) { } ; }
36
+ if ( failureCb === void 0 ) { failureCb = function ( error ) { } ; }
37
+ if ( shouldMigrate === void 0 ) { shouldMigrate = function ( context ) { return false ; } ; }
42
38
if ( ! ( "serviceWorker" in navigator ) ) {
43
39
this . log ( "Service workers are not supported by this browser" ) ;
44
40
return false ;
@@ -55,11 +51,16 @@ var WebPush = /** @class */ (function () {
55
51
this . log ( "Notifications are denied by the user" ) ;
56
52
return false ;
57
53
}
58
- navigator . serviceWorker . register ( swPath ) . then ( function ( registration ) {
59
- _this . log ( "ServiceWorker registration success: " , registration ) ;
60
- _this . checkSubscription ( ) ;
54
+ navigator . serviceWorker . register ( swPath ) . then ( function ( ) {
55
+ var lastSync = parseInt ( localStorage . getItem ( _this . LS . LAST_SYNC ) ) ;
56
+ var now = Date . now ( ) ;
57
+ if ( ! lastSync || ( now - lastSync > _this . syncInterval ) ) {
58
+ console . log ( now , lastSync , now - lastSync , _this . syncInterval ) ;
59
+ _this . checkSubscription ( successCb , failureCb , shouldMigrate ) ;
60
+ localStorage . setItem ( _this . LS . LAST_SYNC , String ( now ) ) ;
61
+ }
61
62
} , function ( err ) {
62
- this . log ( "ServiceWorker registration failed: " , err ) ;
63
+ _this . log ( "ServiceWorker registration failed: " , err ) ;
63
64
} ) ;
64
65
} ;
65
66
WebPush . prototype . checkSubscription = function ( successCb , failureCb , shouldMigrate ) {
@@ -115,7 +116,7 @@ var WebPush = /** @class */ (function () {
115
116
. then ( function ( subscription ) {
116
117
// Subscription was successful
117
118
// create subscription on your server
118
- localStorage . setItem ( _this . localStorageKey , subscription . endpoint ) ;
119
+ localStorage . setItem ( _this . LS . ENDPOINT , subscription . endpoint ) ;
119
120
return _this . sync ( subscription , "POST" ) ;
120
121
} )
121
122
. then ( function ( subscription ) { return success ( subscription ) ; } ) // update your UI
@@ -157,7 +158,7 @@ var WebPush = /** @class */ (function () {
157
158
} )
158
159
. then ( function ( subscription ) { return subscription . unsubscribe ( ) ; } )
159
160
. then ( function ( ) {
160
- localStorage . removeItem ( _this . localStorageKey ) ;
161
+ localStorage . removeItem ( _this . LS . ENDPOINT ) ;
161
162
success ( ) ;
162
163
} ) [ "catch" ] ( function ( e ) {
163
164
// We failed to unsubscribe, this can lead to
@@ -173,7 +174,7 @@ var WebPush = /** @class */ (function () {
173
174
var contentEncoding = ( PushManager . supportedContentEncodings || [
174
175
"aesgcm"
175
176
] ) [ 0 ] ;
176
- localStorage . setItem ( this . localStorageKey , subscription . endpoint ) ;
177
+ localStorage . setItem ( this . LS . ENDPOINT , subscription . endpoint ) ;
177
178
return new Promise ( function ( resolve , reject ) {
178
179
$ . ajax ( {
179
180
url : _this . controller + "/sync?appId=" + _this . appId ,
0 commit comments