11/*** Directives and services for responding to idle users in AngularJS
22* @author Mike Grabski <me@mikegrabski.com>
3- * @version v1.0.1
3+ * @version v1.0.2
44* @link https://github.com/HackedByChinese/ng-idle.git
55* @license MIT
66*/
@@ -129,6 +129,8 @@ angular.module('ngIdle.idle', ['ngIdle.keepalive', 'ngIdle.localStorage'])
129129 countdown : null
130130 } ;
131131
132+ var id = new Date ( ) . getTime ( ) ;
133+
132134 function startKeepalive ( ) {
133135 if ( ! options . keepalive ) return ;
134136
@@ -196,12 +198,14 @@ angular.module('ngIdle.idle', ['ngIdle.keepalive', 'ngIdle.localStorage'])
196198 }
197199
198200 function getExpiry ( ) {
199- return LocalStorage . get ( 'expiry' ) ;
201+ var obj = LocalStorage . get ( 'expiry' ) ;
202+
203+ return obj . time ;
200204 }
201205
202206 function setExpiry ( date ) {
203207 if ( ! date ) LocalStorage . remove ( 'expiry' ) ;
204- else LocalStorage . set ( 'expiry' , date ) ;
208+ else LocalStorage . set ( 'expiry' , { id : id , time : date } ) ;
205209 }
206210
207211 var svc = {
@@ -227,13 +231,13 @@ angular.module('ngIdle.idle', ['ngIdle.keepalive', 'ngIdle.localStorage'])
227231 idling : function ( ) {
228232 return state . idling ;
229233 } ,
230- watch : function ( ) {
234+ watch : function ( noExpiryUpdate ) {
231235 $interval . cancel ( state . idle ) ;
232236 $interval . cancel ( state . timeout ) ;
233237
234238 // calculate the absolute expiry date, as added insurance against a browser sleeping or paused in the background
235239 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 ) ) ) ;
237241
238242
239243 if ( state . idling ) toggleState ( ) ; // clears the idle state if currently idling
@@ -253,7 +257,7 @@ angular.module('ngIdle.idle', ['ngIdle.keepalive', 'ngIdle.localStorage'])
253257
254258 stopKeepalive ( ) ;
255259 } ,
256- interrupt : function ( ) {
260+ interrupt : function ( noExpiryUpdate ) {
257261 if ( ! state . running ) return ;
258262
259263 if ( options . timeout && this . isExpired ( ) ) {
@@ -262,7 +266,7 @@ angular.module('ngIdle.idle', ['ngIdle.keepalive', 'ngIdle.localStorage'])
262266 }
263267
264268 // 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 ) ;
266270 }
267271 } ;
268272
@@ -271,7 +275,11 @@ angular.module('ngIdle.idle', ['ngIdle.keepalive', 'ngIdle.localStorage'])
271275 } ) ;
272276
273277 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+ }
275283 } ;
276284
277285 if ( $window . addEventListener ) $window . addEventListener ( 'storage' , wrap , false ) ;
@@ -412,6 +420,9 @@ angular.module('ngIdle.localStorage', [])
412420 } ,
413421 remove : function ( key ) {
414422 storage . removeItem ( 'ngIdle.' + key ) ;
423+ } ,
424+ parseJson : function ( raw ) {
425+ return tryParseJson ( raw ) ;
415426 }
416427 } ;
417428 } ] ) ;
0 commit comments