@@ -17,28 +17,24 @@ var SubscriptionStatus;
1717 SubscriptionStatus [ "BLOCKED" ] = "blocked" ;
1818} ) ( SubscriptionStatus || ( SubscriptionStatus = { } ) ) ;
1919var WebPush = /** @class */ ( function ( ) {
20- function WebPush ( appId , publicKey , controller , localStorageKey ) {
21- var _this = this ;
20+ function WebPush ( appId , publicKey , controller , syncInterval ) {
2221 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+ } ;
2427 this . appId = appId ;
2528 this . publicKey = publicKey ;
2629 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 ;
3831 }
39- WebPush . prototype . setupRegistration = function ( swPath ) {
32+ WebPush . prototype . setupRegistration = function ( swPath , successCb , failureCb , shouldMigrate ) {
4033 var _this = this ;
4134 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 ; } ; }
4238 if ( ! ( "serviceWorker" in navigator ) ) {
4339 this . log ( "Service workers are not supported by this browser" ) ;
4440 return false ;
@@ -55,11 +51,16 @@ var WebPush = /** @class */ (function () {
5551 this . log ( "Notifications are denied by the user" ) ;
5652 return false ;
5753 }
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+ }
6162 } , function ( err ) {
62- this . log ( "ServiceWorker registration failed: " , err ) ;
63+ _this . log ( "ServiceWorker registration failed: " , err ) ;
6364 } ) ;
6465 } ;
6566 WebPush . prototype . checkSubscription = function ( successCb , failureCb , shouldMigrate ) {
@@ -115,7 +116,7 @@ var WebPush = /** @class */ (function () {
115116 . then ( function ( subscription ) {
116117 // Subscription was successful
117118 // create subscription on your server
118- localStorage . setItem ( _this . localStorageKey , subscription . endpoint ) ;
119+ localStorage . setItem ( _this . LS . ENDPOINT , subscription . endpoint ) ;
119120 return _this . sync ( subscription , "POST" ) ;
120121 } )
121122 . then ( function ( subscription ) { return success ( subscription ) ; } ) // update your UI
@@ -157,7 +158,7 @@ var WebPush = /** @class */ (function () {
157158 } )
158159 . then ( function ( subscription ) { return subscription . unsubscribe ( ) ; } )
159160 . then ( function ( ) {
160- localStorage . removeItem ( _this . localStorageKey ) ;
161+ localStorage . removeItem ( _this . LS . ENDPOINT ) ;
161162 success ( ) ;
162163 } ) [ "catch" ] ( function ( e ) {
163164 // We failed to unsubscribe, this can lead to
@@ -173,7 +174,7 @@ var WebPush = /** @class */ (function () {
173174 var contentEncoding = ( PushManager . supportedContentEncodings || [
174175 "aesgcm"
175176 ] ) [ 0 ] ;
176- localStorage . setItem ( this . localStorageKey , subscription . endpoint ) ;
177+ localStorage . setItem ( this . LS . ENDPOINT , subscription . endpoint ) ;
177178 return new Promise ( function ( resolve , reject ) {
178179 $ . ajax ( {
179180 url : _this . controller + "/sync?appId=" + _this . appId ,
0 commit comments