@therms/web-js
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -0,1 +1,8 @@ | ||
# [2.1.0](http://bitbucket.org/thermsio/web-js/compare/v2.0.0...v2.1.0) (2022-04-14) | ||
### Features | ||
* add unsubscribe return func from onBrowserVisibilityChange() ([a73a5b3](http://bitbucket.org/thermsio/web-js/commits/a73a5b339610a0ac32e28bf71d80b64df39cdd28)) | ||
# [2.0.0](http://bitbucket.org/thermsio/web-js/compare/v1.7.2...v2.0.0) (2022-04-14) | ||
@@ -2,0 +9,0 @@ |
@@ -15,3 +15,44 @@ 'use strict'; | ||
document.hasFocus(); | ||
const checkIsBrowserVisible = () => document.visibilityState !== 'hidden'; | ||
let callbacks = []; | ||
function isVisible() { | ||
callbacks.forEach((cb) => cb(true)); | ||
} | ||
function isHidden() { | ||
callbacks.forEach((cb) => cb(true)); | ||
} | ||
// Standards: | ||
if ('hidden' in document) { | ||
document.addEventListener('visibilitychange', () => { | ||
(document.hidden ? isHidden : isVisible)(); | ||
}); | ||
} | ||
if ('mozHidden' in document) { | ||
document.addEventListener('mozvisibilitychange', () => { | ||
// @ts-ignore | ||
(document.mozHidden ? isHidden : isVisible)(); | ||
}); | ||
} | ||
if ('webkitHidden' in document) { | ||
document.addEventListener('webkitvisibilitychange', () => { | ||
// @ts-ignore | ||
(document.webkitHidden ? isHidden : isVisible)(); | ||
}); | ||
} | ||
if ('msHidden' in document) { | ||
document.addEventListener('msvisibilitychange', () => { | ||
// @ts-ignore | ||
(document.msHidden ? isHidden : isVisible)(); | ||
}); | ||
} | ||
// IE 9 and lower: | ||
if ('onfocusin' in document) { | ||
// @ts-ignore | ||
document.onfocusin = isVisible; | ||
// @ts-ignore | ||
document.onfocusout = isHidden; | ||
} | ||
// All others: | ||
window.onpageshow = window.onfocus = isVisible; | ||
window.onpagehide = window.onblur = isHidden; | ||
/** | ||
@@ -24,47 +65,8 @@ * Visibility is different than "focus"/"blur" - if the window/document is "visible" to the user or not, ie: the window | ||
function onBrowserVisibilityChange(callback) { | ||
if (!callback) { | ||
throw new Error('no callback given'); | ||
} | ||
function focused() { | ||
callback((true)); | ||
} | ||
function unfocused() { | ||
callback((false)); | ||
} | ||
// Standards: | ||
if ('hidden' in document) { | ||
document.addEventListener('visibilitychange', () => { | ||
(document.hidden ? unfocused : focused)(); | ||
}); | ||
} | ||
if ('mozHidden' in document) { | ||
document.addEventListener('mozvisibilitychange', () => { | ||
// @ts-ignore | ||
(document.mozHidden ? unfocused : focused)(); | ||
}); | ||
} | ||
if ('webkitHidden' in document) { | ||
document.addEventListener('webkitvisibilitychange', () => { | ||
// @ts-ignore | ||
(document.webkitHidden ? unfocused : focused)(); | ||
}); | ||
} | ||
if ('msHidden' in document) { | ||
document.addEventListener('msvisibilitychange', () => { | ||
// @ts-ignore | ||
(document.msHidden ? unfocused : focused)(); | ||
}); | ||
} | ||
// IE 9 and lower: | ||
if ('onfocusin' in document) { | ||
// @ts-ignore | ||
document.onfocusin = focused; | ||
// @ts-ignore | ||
document.onfocusout = unfocused; | ||
} | ||
// All others: | ||
window.onpageshow = window.onfocus = focused; | ||
window.onpagehide = window.onblur = unfocused; | ||
callbacks.push(callback); | ||
// unsubscribe | ||
return () => { | ||
callbacks = callbacks.filter((cb) => cb !== callback); | ||
}; | ||
} | ||
const checkIsBrowserVisible = () => document.hasFocus(); | ||
@@ -71,0 +73,0 @@ let flashInterval; |
@@ -0,1 +1,2 @@ | ||
export declare const checkIsBrowserVisible: () => boolean; | ||
/** | ||
@@ -7,3 +8,2 @@ * Visibility is different than "focus"/"blur" - if the window/document is "visible" to the user or not, ie: the window | ||
*/ | ||
export declare function onBrowserVisibilityChange(callback: (visible: boolean) => void): void; | ||
export declare const checkIsBrowserVisible: () => boolean; | ||
export declare function onBrowserVisibilityChange(callback: (visible: boolean) => void): () => void; |
@@ -1,2 +0,43 @@ | ||
document.hasFocus(); | ||
const checkIsBrowserVisible = () => document.visibilityState !== 'hidden'; | ||
let callbacks = []; | ||
function isVisible() { | ||
callbacks.forEach((cb) => cb(true)); | ||
} | ||
function isHidden() { | ||
callbacks.forEach((cb) => cb(true)); | ||
} | ||
// Standards: | ||
if ('hidden' in document) { | ||
document.addEventListener('visibilitychange', () => { | ||
(document.hidden ? isHidden : isVisible)(); | ||
}); | ||
} | ||
if ('mozHidden' in document) { | ||
document.addEventListener('mozvisibilitychange', () => { | ||
// @ts-ignore | ||
(document.mozHidden ? isHidden : isVisible)(); | ||
}); | ||
} | ||
if ('webkitHidden' in document) { | ||
document.addEventListener('webkitvisibilitychange', () => { | ||
// @ts-ignore | ||
(document.webkitHidden ? isHidden : isVisible)(); | ||
}); | ||
} | ||
if ('msHidden' in document) { | ||
document.addEventListener('msvisibilitychange', () => { | ||
// @ts-ignore | ||
(document.msHidden ? isHidden : isVisible)(); | ||
}); | ||
} | ||
// IE 9 and lower: | ||
if ('onfocusin' in document) { | ||
// @ts-ignore | ||
document.onfocusin = isVisible; | ||
// @ts-ignore | ||
document.onfocusout = isHidden; | ||
} | ||
// All others: | ||
window.onpageshow = window.onfocus = isVisible; | ||
window.onpagehide = window.onblur = isHidden; | ||
/** | ||
@@ -9,49 +50,10 @@ * Visibility is different than "focus"/"blur" - if the window/document is "visible" to the user or not, ie: the window | ||
function onBrowserVisibilityChange(callback) { | ||
if (!callback) { | ||
throw new Error('no callback given'); | ||
} | ||
function focused() { | ||
callback((true)); | ||
} | ||
function unfocused() { | ||
callback((false)); | ||
} | ||
// Standards: | ||
if ('hidden' in document) { | ||
document.addEventListener('visibilitychange', () => { | ||
(document.hidden ? unfocused : focused)(); | ||
}); | ||
} | ||
if ('mozHidden' in document) { | ||
document.addEventListener('mozvisibilitychange', () => { | ||
// @ts-ignore | ||
(document.mozHidden ? unfocused : focused)(); | ||
}); | ||
} | ||
if ('webkitHidden' in document) { | ||
document.addEventListener('webkitvisibilitychange', () => { | ||
// @ts-ignore | ||
(document.webkitHidden ? unfocused : focused)(); | ||
}); | ||
} | ||
if ('msHidden' in document) { | ||
document.addEventListener('msvisibilitychange', () => { | ||
// @ts-ignore | ||
(document.msHidden ? unfocused : focused)(); | ||
}); | ||
} | ||
// IE 9 and lower: | ||
if ('onfocusin' in document) { | ||
// @ts-ignore | ||
document.onfocusin = focused; | ||
// @ts-ignore | ||
document.onfocusout = unfocused; | ||
} | ||
// All others: | ||
window.onpageshow = window.onfocus = focused; | ||
window.onpagehide = window.onblur = unfocused; | ||
callbacks.push(callback); | ||
// unsubscribe | ||
return () => { | ||
callbacks = callbacks.filter((cb) => cb !== callback); | ||
}; | ||
} | ||
const checkIsBrowserVisible = () => document.hasFocus(); | ||
export { checkIsBrowserVisible, onBrowserVisibilityChange }; | ||
//# sourceMappingURL=browser-visibility.js.map |
{ | ||
"name": "@therms/web-js", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Common web/JS tools & utilities", | ||
@@ -5,0 +5,0 @@ "main": "./dist/cjs.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
98747
1484