Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [#2409] engine 'visible' event not firing #2412

Merged
merged 3 commits into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

-
- Fixed `engine.on('visible')` event not firing

### Updates

Expand Down
19 changes: 3 additions & 16 deletions src/engine/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1109,24 +1109,11 @@ O|===|* >________________>\n\
// Issue #385 make use of the visibility api
// https://developer.mozilla.org/en-US/docs/Web/Guide/User_experience/Using_the_Page_Visibility_API

let hidden: keyof HTMLDocument, visibilityChange: string;
if (typeof document.hidden !== 'undefined') {
// Opera 12.10 and Firefox 18 and later support
hidden = 'hidden';
visibilityChange = 'visibilitychange';
} else if ('msHidden' in document) {
hidden = <keyof HTMLDocument>'msHidden';
visibilityChange = 'msvisibilitychange';
} else if ('webkitHidden' in document) {
hidden = <keyof HTMLDocument>'webkitHidden';
visibilityChange = 'webkitvisibilitychange';
}

this.browser.document.on(visibilityChange, () => {
if (document[hidden]) {
this.browser.document.on('visibilitychange', () => {
if (document.visibilityState === 'hidden') {
this.eventDispatcher.emit('hidden', new HiddenEvent(this));
this._logger.debug('Window hidden');
} else {
} else if (document.visibilityState === 'visible') {
this.eventDispatcher.emit('visible', new VisibleEvent(this));
this._logger.debug('Window visible');
}
Expand Down
20 changes: 20 additions & 0 deletions src/spec/EngineSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,26 @@ describe('The engine', () => {
expect(fired).toHaveBeenCalledTimes(2);
});

it('should emit a visible event', () => {
const fired = jasmine.createSpy('fired');
engine.on('visible', fired);

spyOnProperty(window.document, 'visibilityState').and.returnValue('visible');
window.document.dispatchEvent(new CustomEvent('visibilitychange'));

expect(fired).toHaveBeenCalled();
});

it('should emit a hidden event', () => {
const fired = jasmine.createSpy('fired');
engine.on('hidden', fired);

spyOnProperty(window.document, 'visibilityState').and.returnValue('hidden');
window.document.dispatchEvent(new CustomEvent('visibilitychange'));

expect(fired).toHaveBeenCalled();
});

it('should tell engine is running', () => {
const status = engine.isRunning();
expect(status).toBe(true);
Expand Down