1
1
/*** Directives and services for responding to idle users in AngularJS
2
2
* @author Mike Grabski <me@mikegrabski.com>
3
- * @version v1.0.1
3
+ * @version v1.0.2
4
4
* @link https://github.com/HackedByChinese/ng-idle.git
5
5
* @license MIT
6
6
*/
@@ -129,6 +129,8 @@ angular.module('ngIdle.idle', ['ngIdle.keepalive', 'ngIdle.localStorage'])
129
129
countdown : null
130
130
} ;
131
131
132
+ var id = new Date ( ) . getTime ( ) ;
133
+
132
134
function startKeepalive ( ) {
133
135
if ( ! options . keepalive ) return ;
134
136
@@ -196,12 +198,14 @@ angular.module('ngIdle.idle', ['ngIdle.keepalive', 'ngIdle.localStorage'])
196
198
}
197
199
198
200
function getExpiry ( ) {
199
- return LocalStorage . get ( 'expiry' ) ;
201
+ var obj = LocalStorage . get ( 'expiry' ) ;
202
+
203
+ return obj . time ;
200
204
}
201
205
202
206
function setExpiry ( date ) {
203
207
if ( ! date ) LocalStorage . remove ( 'expiry' ) ;
204
- else LocalStorage . set ( 'expiry' , date ) ;
208
+ else LocalStorage . set ( 'expiry' , { id : id , time : date } ) ;
205
209
}
206
210
207
211
var svc = {
@@ -227,13 +231,13 @@ angular.module('ngIdle.idle', ['ngIdle.keepalive', 'ngIdle.localStorage'])
227
231
idling : function ( ) {
228
232
return state . idling ;
229
233
} ,
230
- watch : function ( ) {
234
+ watch : function ( noExpiryUpdate ) {
231
235
$interval . cancel ( state . idle ) ;
232
236
$interval . cancel ( state . timeout ) ;
233
237
234
238
// calculate the absolute expiry date, as added insurance against a browser sleeping or paused in the background
235
239
var timeout = ! options . timeout ? 0 : options . timeout ;
236
- setExpiry ( new Date ( new Date ( ) . getTime ( ) + ( ( options . idle + timeout ) * 1000 ) ) ) ;
240
+ if ( ! noExpiryUpdate ) setExpiry ( new Date ( new Date ( ) . getTime ( ) + ( ( options . idle + timeout ) * 1000 ) ) ) ;
237
241
238
242
239
243
if ( state . idling ) toggleState ( ) ; // clears the idle state if currently idling
@@ -253,7 +257,7 @@ angular.module('ngIdle.idle', ['ngIdle.keepalive', 'ngIdle.localStorage'])
253
257
254
258
stopKeepalive ( ) ;
255
259
} ,
256
- interrupt : function ( ) {
260
+ interrupt : function ( noExpiryUpdate ) {
257
261
if ( ! state . running ) return ;
258
262
259
263
if ( options . timeout && this . isExpired ( ) ) {
@@ -262,7 +266,7 @@ angular.module('ngIdle.idle', ['ngIdle.keepalive', 'ngIdle.localStorage'])
262
266
}
263
267
264
268
// note: you can no longer auto resume once we exceed the expiry; you will reset state by calling watch() manually
265
- if ( options . autoResume === 'idle' || ( options . autoResume === 'notIdle' && ! state . idling ) ) this . watch ( ) ;
269
+ if ( options . autoResume === 'idle' || ( options . autoResume === 'notIdle' && ! state . idling ) ) this . watch ( noExpiryUpdate ) ;
266
270
}
267
271
} ;
268
272
@@ -271,7 +275,11 @@ angular.module('ngIdle.idle', ['ngIdle.keepalive', 'ngIdle.localStorage'])
271
275
} ) ;
272
276
273
277
var wrap = function ( event ) {
274
- if ( event . key === 'ngIdle.expiry' ) svc . interrupt ( ) ;
278
+ if ( event . key === 'ngIdle.expiry' && event . newValue !== event . oldValue ) {
279
+ var val = LocalStorage . parseJson ( event . newValue ) ;
280
+ if ( val . id === id ) return ;
281
+ svc . interrupt ( true ) ;
282
+ }
275
283
} ;
276
284
277
285
if ( $window . addEventListener ) $window . addEventListener ( 'storage' , wrap , false ) ;
@@ -412,6 +420,9 @@ angular.module('ngIdle.localStorage', [])
412
420
} ,
413
421
remove : function ( key ) {
414
422
storage . removeItem ( 'ngIdle.' + key ) ;
423
+ } ,
424
+ parseJson : function ( raw ) {
425
+ return tryParseJson ( raw ) ;
415
426
}
416
427
} ;
417
428
} ] ) ;
0 commit comments