@yorkjs/web-page
Advanced tools
Comparing version 0.0.3 to 0.0.4
/** | ||
* web-page.js v0.0.3 | ||
* web-page.js v0.0.4 | ||
* (c) 2021-2022 musicode | ||
@@ -12,3 +12,4 @@ * Released under the MIT License. | ||
let ready = false; | ||
let isInitCalled = false; | ||
let isPageAlive = false; | ||
let visible; | ||
@@ -49,6 +50,5 @@ let persisted; | ||
events[type] = data; | ||
if (!ready) { | ||
return; | ||
if (isInitCalled && isPageAlive) { | ||
fireEventData(type, data); | ||
} | ||
fireEventData(type, data); | ||
} | ||
@@ -87,17 +87,21 @@ function fireEventData(type, data) { | ||
} | ||
function onPageEnter(event) { | ||
// 页面是否从浏览器缓存读取 | ||
const onPageEnter = debounceListener(function (event) { | ||
if (isPageAlive) { | ||
return; | ||
} | ||
isPageAlive = true; | ||
// @ts-ignore | ||
if (typeof event.persisted === 'boolean') { | ||
// @ts-ignore | ||
persisted = event.persisted; | ||
} | ||
persisted = event.persisted === true; | ||
fireEvent(ENTER, event); | ||
} | ||
}, 200); | ||
const onPageLeave = debounceListener(function (event) { | ||
if (!isPageAlive) { | ||
return; | ||
} | ||
fireEvent(LEAVE, event); | ||
isPageAlive = false; | ||
}, 200); | ||
function init() { | ||
isInitCalled = true; | ||
updateVisible(); | ||
ready = true; | ||
} | ||
@@ -126,5 +130,3 @@ function addEventListener(type, listener) { | ||
} | ||
else { | ||
addDOMEventListener(window, 'load', onPageEnter); | ||
} | ||
addDOMEventListener(window, 'load', onPageEnter); | ||
if (supportEvent(window, 'pagehide')) { | ||
@@ -138,5 +140,5 @@ addDOMEventListener(window, 'pagehide', onPageLeave); | ||
*/ | ||
const version = "0.0.3"; | ||
const version = "0.0.4"; | ||
export { ENTER, HIDE, LEAVE, SHOW, addEventListener, init, version }; | ||
//# sourceMappingURL=web-page.esm.js.map |
@@ -1,2 +0,2 @@ | ||
const n="show",e="hide",t="enter",i="leave";let o,c,s=!1;const d={},u={};function r(n,e){return`on${e}`in n}function a(n,e,t){n.addEventListener?n.addEventListener(e,t,!0):n.attachEvent&&n.attachEvent("on"+e,t)}function f(n,e){const t={event:e,visible:o,persisted:c};d[n]=t,s&&l(n,t)}function l(n,e){const t=u[n];if(t)for(let n=0,i=t.length;n<i;n++)t[n]&&t[n](e)}function h(){const n=document.visibilityState;o="visible"===n||"hidden"!==n&&void 0}function w(n){"boolean"==typeof n.persisted&&(c=n.persisted),f("enter",n)}const v=function(n,e){let t;return function(){t||(n(arguments[0]),t=setTimeout((function(){t=void 0}),e))}}((function(n){f("leave",n)}),200);function p(){h(),s=!0}function g(n,e){const t=u[n]||(u[n]=[]);return t.push(e),"enter"===n&&d[n]&&l(n,d[n]),{remove(){for(let n=0,i=t.length;n<i;n++)t[n]===e&&t.splice(n,1)}}}r(document,"visibilitychange")&&a(document,"visibilitychange",(function(n){h(),!0===o?f("show",n):!1===o&&f("hide",n)})),r(window,"pageshow")?a(window,"pageshow",w):a(window,"load",w),r(window,"pagehide")&&a(window,"pagehide",v),a(window,"beforeunload",v);const b="0.0.3";export{t as ENTER,e as HIDE,i as LEAVE,n as SHOW,g as addEventListener,p as init,b as version}; | ||
const n="show",e="hide",t="enter",i="leave";let o,c,s=!1,d=!1;const u={},r={};function a(n,e){return`on${e}`in n}function f(n,e,t){n.addEventListener?n.addEventListener(e,t,!0):n.attachEvent&&n.attachEvent("on"+e,t)}function h(n,e){let t;return function(){t||(n(arguments[0]),t=setTimeout((function(){t=void 0}),e))}}function l(n,e){const t={event:e,visible:o,persisted:c};u[n]=t,s&&d&&w(n,t)}function w(n,e){const t=r[n];if(t)for(let n=0,i=t.length;n<i;n++)t[n]&&t[n](e)}function v(){const n=document.visibilityState;o="visible"===n||"hidden"!==n&&void 0}const p=h((function(n){d||(d=!0,c=!0===n.persisted,l("enter",n))}),200),g=h((function(n){d&&(l("leave",n),d=!1)}),200);function b(){s=!0,v()}function m(n,e){const t=r[n]||(r[n]=[]);return t.push(e),"enter"===n&&u[n]&&w(n,u[n]),{remove(){for(let n=0,i=t.length;n<i;n++)t[n]===e&&t.splice(n,1)}}}a(document,"visibilitychange")&&f(document,"visibilitychange",(function(n){v(),!0===o?l("show",n):!1===o&&l("hide",n)})),a(window,"pageshow")&&f(window,"pageshow",p),f(window,"load",p),a(window,"pagehide")&&f(window,"pagehide",g),f(window,"beforeunload",g);const E="0.0.4";export{t as ENTER,e as HIDE,i as LEAVE,n as SHOW,m as addEventListener,b as init,E as version}; | ||
//# sourceMappingURL=web-page.esm.min.js.map |
/** | ||
* web-page.js v0.0.3 | ||
* web-page.js v0.0.4 | ||
* (c) 2021-2022 musicode | ||
@@ -11,3 +11,3 @@ * Released under the MIT License. | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.WebPage = {})); | ||
}(this, (function (exports) { 'use strict'; | ||
})(this, (function (exports) { 'use strict'; | ||
@@ -19,3 +19,4 @@ var SHOW = 'show'; | ||
var ready = false; | ||
var isInitCalled = false; | ||
var isPageAlive = false; | ||
var visible; | ||
@@ -56,6 +57,5 @@ var persisted; | ||
events[type] = data; | ||
if (!ready) { | ||
return; | ||
if (isInitCalled && isPageAlive) { | ||
fireEventData(type, data); | ||
} | ||
fireEventData(type, data); | ||
} | ||
@@ -94,17 +94,21 @@ function fireEventData(type, data) { | ||
} | ||
function onPageEnter(event) { | ||
// 页面是否从浏览器缓存读取 | ||
var onPageEnter = debounceListener(function (event) { | ||
if (isPageAlive) { | ||
return; | ||
} | ||
isPageAlive = true; | ||
// @ts-ignore | ||
if (typeof event.persisted === 'boolean') { | ||
// @ts-ignore | ||
persisted = event.persisted; | ||
} | ||
persisted = event.persisted === true; | ||
fireEvent(ENTER, event); | ||
} | ||
}, 200); | ||
var onPageLeave = debounceListener(function (event) { | ||
if (!isPageAlive) { | ||
return; | ||
} | ||
fireEvent(LEAVE, event); | ||
isPageAlive = false; | ||
}, 200); | ||
function init() { | ||
isInitCalled = true; | ||
updateVisible(); | ||
ready = true; | ||
} | ||
@@ -133,5 +137,3 @@ function addEventListener(type, listener) { | ||
} | ||
else { | ||
addDOMEventListener(window, 'load', onPageEnter); | ||
} | ||
addDOMEventListener(window, 'load', onPageEnter); | ||
if (supportEvent(window, 'pagehide')) { | ||
@@ -145,3 +147,3 @@ addDOMEventListener(window, 'pagehide', onPageLeave); | ||
*/ | ||
var version = "0.0.3"; | ||
var version = "0.0.4"; | ||
@@ -158,3 +160,3 @@ exports.ENTER = ENTER; | ||
}))); | ||
})); | ||
//# sourceMappingURL=web-page.js.map |
@@ -1,2 +0,2 @@ | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).WebPage={})}(this,(function(e){"use strict";var n,i,t="show",o="hide",d="enter",f="leave",a=!1,s={},r={};function u(e,n){return"on"+n in e}function c(e,n,i){e.addEventListener?e.addEventListener(n,i,!0):e.attachEvent&&e.attachEvent("on"+n,i)}function v(e,t){var o={event:t,visible:n,persisted:i};s[e]=o,a&&l(e,o)}function l(e,n){var i=r[e];if(i)for(var t=0,o=i.length;t<o;t++)i[t]&&i[t](n)}function p(){var e=document.visibilityState;n="visible"===e||"hidden"!==e&&void 0}function h(e){"boolean"==typeof e.persisted&&(i=e.persisted),v(d,e)}var w,b,g,E=(w=function(e){v(f,e)},b=200,function(){g||(w(arguments[0]),g=setTimeout((function(){g=void 0}),b))});u(document,"visibilitychange")&&c(document,"visibilitychange",(function(e){p(),!0===n?v(t,e):!1===n&&v(o,e)})),u(window,"pageshow")?c(window,"pageshow",h):c(window,"load",h),u(window,"pagehide")&&c(window,"pagehide",E),c(window,"beforeunload",E);e.ENTER=d,e.HIDE=o,e.LEAVE=f,e.SHOW=t,e.addEventListener=function(e,n){var i=r[e]||(r[e]=[]);return i.push(n),e===d&&s[e]&&l(e,s[e]),{remove:function(){for(var e=0,t=i.length;e<t;e++)i[e]===n&&i.splice(e,1)}}},e.init=function(){p(),a=!0},e.version="0.0.3",Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).WebPage={})}(this,(function(e){"use strict";var n,i,t="show",o="hide",d="enter",f="leave",r=!1,u=!1,a={},s={};function c(e,n){return"on"+n in e}function v(e,n,i){e.addEventListener?e.addEventListener(n,i,!0):e.attachEvent&&e.attachEvent("on"+n,i)}function l(e,n){var i;return function(){i||(e(arguments[0]),i=setTimeout((function(){i=void 0}),n))}}function h(e,t){var o={event:t,visible:n,persisted:i};a[e]=o,r&&u&&p(e,o)}function p(e,n){var i=s[e];if(i)for(var t=0,o=i.length;t<o;t++)i[t]&&i[t](n)}function w(){var e=document.visibilityState;n="visible"===e||"hidden"!==e&&void 0}var b=l((function(e){u||(u=!0,i=!0===e.persisted,h(d,e))}),200),g=l((function(e){u&&(h(f,e),u=!1)}),200);c(document,"visibilitychange")&&v(document,"visibilitychange",(function(e){w(),!0===n?h(t,e):!1===n&&h(o,e)})),c(window,"pageshow")&&v(window,"pageshow",b),v(window,"load",b),c(window,"pagehide")&&v(window,"pagehide",g),v(window,"beforeunload",g);e.ENTER=d,e.HIDE=o,e.LEAVE=f,e.SHOW=t,e.addEventListener=function(e,n){var i=s[e]||(s[e]=[]);return i.push(n),e===d&&a[e]&&p(e,a[e]),{remove:function(){for(var e=0,t=i.length;e<t;e++)i[e]===n&&i.splice(e,1)}}},e.init=function(){r=!0,w()},e.version="0.0.4",Object.defineProperty(e,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=web-page.min.js.map |
{ | ||
"name": "@yorkjs/web-page", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "web page life cycle", | ||
@@ -5,0 +5,0 @@ "main": "dist/web-page.js", |
# web-page | ||
监听网页 `show`、`hide`、`enter`、``leave` 事件。 | ||
监听网页 `show`、`hide`、`enter`、`leave` 事件。 | ||
@@ -5,0 +5,0 @@ ## 安装 |
@@ -6,2 +6,2 @@ export { SHOW, HIDE, ENTER, LEAVE, } from './constant'; | ||
*/ | ||
export declare const version = "0.0.3"; | ||
export declare const version = "0.0.4"; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14120
310