mirror of
https://github.com/ai/visibilityjs.git
synced 2026-04-19 03:00:02 -04:00
Add Visibility.notPrerender(callback) to run code not in prerender state
This commit is contained in:
@@ -40,6 +40,10 @@
|
||||
// visible.
|
||||
_onVisibleCallbacks: [],
|
||||
|
||||
// Callbacks from `notPrerender` method, that wait when visibility state
|
||||
// change from “prerender”.
|
||||
_notPrerenderCallbacks: [],
|
||||
|
||||
// Last timer number.
|
||||
_lastTimer: 0,
|
||||
|
||||
@@ -118,6 +122,13 @@
|
||||
this._onVisibleCallbacks = [];
|
||||
}
|
||||
|
||||
if ( 'prerender' != this.state() ) {
|
||||
for ( var i = 0; i < this._notPrerenderCallbacks.length; i++ ) {
|
||||
this._notPrerenderCallbacks[i]();
|
||||
}
|
||||
this._notPrerenderCallbacks = [];
|
||||
}
|
||||
|
||||
this._hiddenBefore = isHidden;
|
||||
},
|
||||
|
||||
@@ -244,6 +255,19 @@
|
||||
this._setListener();
|
||||
},
|
||||
|
||||
// Call `callback` in any state, expect “prerender”. If current state
|
||||
// is “prerender” it will wait until state will be changed.
|
||||
// If Page Visibility API doesn’t supported, it will call `callback`
|
||||
// immediately.
|
||||
notPrerender: function (callback) {
|
||||
if ( !this.support() || 'prerender' != this.state() ) {
|
||||
callback();
|
||||
return true;
|
||||
}
|
||||
this._notPrerenderCallbacks.push(callback);
|
||||
this._setListener();
|
||||
},
|
||||
|
||||
// Run callback every `interval` milliseconds if page is visible and
|
||||
// every `hiddenInterval` milliseconds if page is hidden.
|
||||
//
|
||||
|
||||
@@ -341,4 +341,49 @@ describe('Visibility', function () {
|
||||
expect( Visibility._runTimer.argsForCall[2] ).toEqual(['1', false]);
|
||||
expect( Visibility._runTimer.argsForCall[3] ).toEqual(['3', false]);
|
||||
});
|
||||
|
||||
it('should run notPrerender callback now without API support', function () {
|
||||
spyOn(Visibility, 'support').andReturn(false);
|
||||
spyOn(Visibility, '_setListener');
|
||||
var callback = jasmine.createSpy();
|
||||
|
||||
Visibility.notPrerender(callback);
|
||||
|
||||
expect( callback ).toHaveBeenCalled();
|
||||
expect( Visibility._setListener ).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should run notPrerender now, if page isn’t prerended', function () {
|
||||
Visibility._chechedPrefix = 'webkit';
|
||||
document.webkitVisibilityState = 'hidden';
|
||||
spyOn(Visibility, '_setListener');
|
||||
var callback = jasmine.createSpy();
|
||||
|
||||
Visibility.notPrerender(callback);
|
||||
|
||||
expect( callback ).toHaveBeenCalled();
|
||||
expect( Visibility._setListener ).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should run notPrerender by listener on prerended page', function () {
|
||||
Visibility._chechedPrefix = 'webkit';
|
||||
document.webkitVisibilityState = 'prerender';
|
||||
spyOn(Visibility, '_setListener');
|
||||
var callback = jasmine.createSpy();
|
||||
|
||||
Visibility.notPrerender(callback);
|
||||
|
||||
expect( callback ).not.toHaveBeenCalled();
|
||||
expect( Visibility._setListener ).toHaveBeenCalled();
|
||||
|
||||
Visibility._onVisibilityChange();
|
||||
expect( callback ).not.toHaveBeenCalled();
|
||||
|
||||
document.webkitVisibilityState = 'visible';
|
||||
Visibility._onVisibilityChange();
|
||||
expect( callback ).toHaveBeenCalled();
|
||||
|
||||
Visibility._onVisibilityChange();
|
||||
expect( callback.callCount ).toEqual(1);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user