{"_id":"page-lifecycle","_rev":"306518","name":"page-lifecycle","description":"A JavaScript library to observe Page Lifecycle API states: https://github.com/WICG/page-lifecycle","dist-tags":{"latest":"0.1.2"},"maintainers":[{"name":"philipwalton","email":""}],"time":{"modified":"2021-06-03T19:33:17.000Z","created":"2018-06-27T20:45:22.982Z","0.1.2":"2019-08-28T04:03:15.360Z","0.1.1":"2018-08-04T02:00:17.842Z","0.1.0":"2018-06-27T20:45:22.982Z"},"users":{},"author":{"name":"Philip Walton","email":"philip@philipwalton.com","url":"http://philipwalton.com"},"repository":{"type":"git","url":"git+https://github.com/GoogleChromeLabs/page-lifecycle.git"},"versions":{"0.1.2":{"name":"page-lifecycle","version":"0.1.2","description":"A JavaScript library to observe Page Lifecycle API states: https://github.com/WICG/page-lifecycle","license":"Apache-2.0","main":"dist/lifecycle.es5.js","module":"dist/lifecycle.mjs","scripts":{"test":"eslint *.js src/**/* test/*.mjs","build":"rollup -c"},"repository":{"type":"git","url":"git+https://github.com/GoogleChromeLabs/page-lifecycle.git"},"bugs":{"url":"https://github.com/GoogleChromeLabs/page-lifecycle/issues"},"homepage":"https://github.com/GoogleChromeLabs/page-lifecycle#readme","keywords":["page","lifecycle","unload","beforeunload","visibilityState","visibilitychange","active","passive","hidden","frozen","discarded","terminated"],"author":{"name":"Philip Walton","email":"philip@philipwalton.com","url":"http://philipwalton.com"},"devDependencies":{"babel-core":"^6.26.3","babel-plugin-external-helpers":"^6.22.0","babel-preset-env":"^1.7.0","eslint":"^6.2.2","eslint-config-google":"^0.13.0","rollup":"^1.20.2","rollup-plugin-babel":"^4.3.3","rollup-plugin-multi-entry":"^2.1.0","rollup-plugin-replace":"^2.2.0","rollup-plugin-terser":"^5.1.1"},"eslintConfig":{"root":true,"env":{"browser":true,"es6":true,"node":true,"mocha":true},"globals":{"assert":false,"sinon":false,"safari":false},"parserOptions":{"ecmaVersion":2018,"sourceType":"module"},"extends":["eslint:recommended","google"]},"gitHead":"a32563d198a7e4f5c2338fe6a53a02fd5857992b","_id":"page-lifecycle@0.1.2","_nodeVersion":"10.15.3","_npmVersion":"6.11.2","dist":{"shasum":"f17a083c082bd5ababddd77f1025a4b1c8808012","size":26947,"noattachment":false,"key":"/page-lifecycle/-/page-lifecycle-0.1.2.tgz","tarball":"http://registry.cnpm.dingdandao.com/page-lifecycle/download/page-lifecycle-0.1.2.tgz"},"maintainers":[{"name":"philipwalton","email":""}],"_npmUser":{"name":"philipwalton","email":"philip@philipwalton.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/page-lifecycle_0.1.2_1566964995229_0.9832246822301194"},"_hasShrinkwrap":false,"publish_time":1566964995360,"_cnpm_publish_time":1566964995360},"0.1.1":{"name":"page-lifecycle","version":"0.1.1","description":"A JavaScript library to observe Page Lifecycle API states: https://github.com/WICG/page-lifecycle","license":"Apache-2.0","main":"dist/lifecycle.es5.js","module":"dist/lifecycle.mjs","scripts":{"test":"eslint *.js src/**/* test/*.mjs","build":"rollup -c"},"repository":{"type":"git","url":"git+https://github.com/GoogleChromeLabs/page-lifecycle.git"},"bugs":{"url":"https://github.com/GoogleChromeLabs/page-lifecycle/issues"},"homepage":"https://github.com/GoogleChromeLabs/page-lifecycle#readme","keywords":["page","lifecycle","unload","beforeunload","visibilityState","visibilitychange","active","passive","hidden","frozen","discarded","terminated"],"author":{"name":"Philip Walton","email":"philip@philipwalton.com","url":"http://philipwalton.com"},"devDependencies":{"babel-core":"^6.26.3","babel-plugin-external-helpers":"^6.22.0","babel-preset-env":"^1.7.0","eslint":"^4.19.1","eslint-config-google":"^0.9.1","rollup":"^0.60.4","rollup-plugin-babel":"^3.0.4","rollup-plugin-multi-entry":"^2.0.2","rollup-plugin-replace":"^2.0.0","rollup-plugin-terser":"^1.0.1"},"eslintConfig":{"root":true,"env":{"browser":true,"es6":true,"node":true,"mocha":true},"globals":{"assert":false,"sinon":false,"safari":false},"parserOptions":{"ecmaVersion":2018,"sourceType":"module"},"extends":["eslint:recommended","google"]},"gitHead":"9114d5ded4ffe2e2bea349d2b00de8342b1e9d11","_id":"page-lifecycle@0.1.1","_npmVersion":"6.3.0","_nodeVersion":"10.7.0","_npmUser":{"name":"philipwalton","email":"philip@philipwalton.com"},"dist":{"shasum":"bd1b851b304c94356612a0909d6f9aabc7f5752e","size":24717,"noattachment":false,"key":"/page-lifecycle/-/page-lifecycle-0.1.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/page-lifecycle/download/page-lifecycle-0.1.1.tgz"},"maintainers":[{"name":"philipwalton","email":""}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/page-lifecycle_0.1.1_1533348017753_0.644495783272216"},"_hasShrinkwrap":false,"publish_time":1533348017842,"_cnpm_publish_time":1533348017842},"0.1.0":{"name":"page-lifecycle","version":"0.1.0","description":"A JavaScript library to observe Page Lifecycle API states: https://github.com/WICG/page-lifecycle","license":"Apache-2.0","main":"dist/lifecycle.es5.js","module":"dist/lifecycle.mjs","scripts":{"test":"eslint *.js src/**/*","build":"rollup -c"},"repository":{"type":"git","url":"git+https://github.com/GoogleChromeLabs/page-lifecycle.git"},"bugs":{"url":"https://github.com/GoogleChromeLabs/page-lifecycle/issues"},"homepage":"https://github.com/GoogleChromeLabs/page-lifecycle#readme","keywords":["page","lifecycle","unload","beforeunload","visibilityState","visibilitychange","active","passive","hidden","frozen","discarded","terminated"],"author":{"name":"Philip Walton","email":"philip@philipwalton.com","url":"http://philipwalton.com"},"devDependencies":{"babel-core":"^6.26.3","babel-plugin-external-helpers":"^6.22.0","babel-preset-env":"^1.7.0","eslint":"^4.19.1","eslint-config-google":"^0.9.1","rollup":"^0.60.4","rollup-plugin-babel":"^3.0.4","rollup-plugin-multi-entry":"^2.0.2","rollup-plugin-replace":"^2.0.0","rollup-plugin-terser":"^1.0.1"},"eslintConfig":{"root":true,"env":{"browser":true,"es6":true,"node":true,"mocha":true},"globals":{"assert":false,"sinon":false},"parserOptions":{"ecmaVersion":2018,"sourceType":"module"},"extends":["eslint:recommended","google"]},"gitHead":"daeef77806dd820a107f1c3d69b6832465678b8a","_id":"page-lifecycle@0.1.0","_npmVersion":"6.1.0","_nodeVersion":"10.1.0","_npmUser":{"name":"philipwalton","email":"philip@philipwalton.com"},"dist":{"shasum":"c74d1103ff1666cd380c19e0adb86fd06397c6e7","size":23268,"noattachment":false,"key":"/page-lifecycle/-/page-lifecycle-0.1.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/page-lifecycle/download/page-lifecycle-0.1.0.tgz"},"maintainers":[{"name":"philipwalton","email":""}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/page-lifecycle_0.1.0_1530132322917_0.8231759231725109"},"_hasShrinkwrap":false,"publish_time":1530132322982,"_cnpm_publish_time":1530132322982}},"readme":"# PageLifecycle.js\n\n- [Overview](#overview)\n- [Installation](#installation)\n- [Usage](#usage)\n- [API](#api)\n  - [Properties](#properties)\n  - [Methods](#methods)\n  - [Events](#events)\n- [Browser Support](#browser-support)\n\n\n## Overview\n\nPageLifecycle.js is a tiny JavaScript library (<1K gzipped) that allows developers to easily observe [Page Lifecycle API](https://developers.google.com/web/updates/2018/07/page-lifecycle-api) state changes and implement [Page Lifecycle best practices](https://developers.google.com/web/updates/2018/07/page-lifecycle-api#developer-recommendations-for-each-state) consistently across all browsers.\n\n**▶️ View demo: [page-lifecycle.glitch.me](https://page-lifecycle.glitch.me/) ????**\n\n## Installation\n\nYou can install this library from npm by running:\n\n```sh\nnpm install --save-dev page-lifecycle\n```\n\n## Usage\n\nReleases of this library include three minified, production-ready versions:\n\n**1. ES5: [`dist/lifecycle.es5.js`](/dist/lifecycle.es5.js)** (UMD) ⭐\n\nUse this version for maximum compatibility with legacy browsers (that can't run ES2015+ code).\n\nAs a UMD bundle, it can be required in CommonJS or AMD environments, or it can be loaded with a script tag as the browser global `lifecycle`.\n\n```html\n<script defer src=\"/path/to/lifecycle.es5.js\"></script>\n<script defer>\nlifecycle.addEventListener('statechange', function(event) {\n  console.log(event.oldState, event.newState);\n});\n</script>\n```\n\n**2. ES2015: [`dist/lifecycle.mjs`](/dist/lifecycle.mjs)** (ESM) ????\n\nUse this version if you only support ES module-capable browsers or if you're using `<script type=\"module\">` and the `nomodule` fallback [to conditionally target modern browsers](https://philipwalton.com/articles/deploying-es2015-code-in-production-today/).\n\n```html\n<script type=\"module\">\nimport lifecycle from '/path/to/page-lifecycle.mjs';\n\nlifecycle.addEventListener('statechange', function(event) {\n  console.log(event.oldState, event.newState);\n});\n</script>\n```\n\n\n**3. ES2015 (native): [`dist/lifecycle.native.mjs`](/dist/lifecycle.native.mjs)** (ESM w/o `EventTarget` and `Event` shims) ⚠️\n\nUse this version if you're only targeting browsers that [support extending `EventTarget` and `Event` constructors](https://www.chromestatus.com/features/5721972856061952).\n\n*Note: this version is the smallest but will only work in some browsers. The implementation instructions are the same as the ES2015 version above.*\n\n## API\n\nThe PageLifecycle.js library exports a `lifecycle` object, which is a singleton instance of the [`Lifecycle`](/src/Lifecycle.mjs) class. The `Lifecycle` class has the following properties, methods, and events:\n\n### Properties\n\n<table>\n  <tr valign=\"top\">\n    <th align=\"left\">Name</th>\n    <th align=\"left\">Type</th>\n    <th align=\"left\">Description</th>\n  </tr>\n  <tr valign=\"top\">\n    <td><code>state</code></td>\n    <td><code>string</code></td>\n    <td>\n      Returns the current Page Lifecycle state.\n    </td>\n  </tr>\n  <tr valign=\"top\">\n    <td><code>pageWasDiscarded</code></td>\n    <td><code>boolean</code></td>\n    <td>\n      Returns the value of <code>document.wasDiscarded</code> (or <code>false</code> if not present).\n    </td>\n  </tr>\n</table>\n\n### Methods\n\n<table>\n  <tr valign=\"top\">\n    <th align=\"left\">Name</th>\n    <th align=\"left\">Description</th>\n  </tr>\n  <tr valign=\"top\">\n    <td><code>addEventListener</code></td>\n    <td>\n      <p><strong>Parameters:</strong></p>\n      <ul>\n        <li><em>type</em>: <code>string</code></li>\n        <li><em>listener</em>: <code>function(Event)</code></li>\n      </ul>\n      <p>Adds a callback function to be invoked whenever the passed event type is detected. <em>(Note: at the moment only the \"statechange\" event type is supported.)</em></p>\n    </td>\n  </tr>\n  <tr valign=\"top\">\n    <td><code>removeEventListener</code></td>\n    <td>\n      <p><strong>Parameters:</strong></p>\n      <ul>\n        <li><em>type</em>: <code>string</code></li>\n        <li><em>listener</em>: <code>function(Event)</code></li>\n      </ul>\n      <p>Removes a function from the current list of listeners for the passed event type. <em>(Note: at the moment only the \"statechange\" event type is supported.)</em></p>\n    </td>\n  </tr>\n  <tr valign=\"top\">\n    <td><code>addUnsavedChanges</code></td>\n    <td>\n      <p><strong>Parameters:</strong></p>\n      <ul>\n        <li><em>id</em>: <code>Object|Symbol</code></li>\n      </ul>\n      <p>Adds an item to an internal pending-changes stack. Calling this method adds a generic <code>beforeunload</code> listener to the window (if one isn't already added).</p>\n      <p>The argument passed should be unique to this state, as it can only be removed by passing the same argument to <code>removeUnsavedChanges()</code>.\n      </p>\n    </td>\n  </tr>\n  <tr valign=\"top\">\n    <td><code>removeUnsavedChanges</code></td>\n    <td>\n      <p><strong>Parameters:</strong></p>\n      <ul>\n        <li><em>id</em>: <code>Object|Symbol</code></li>\n      </ul>\n      <p>Removes an item matching the passed argument from an internal pending-changes stack. If the stack is empty, the generic <code>beforeunload</code> listener is removed from the window.</p>\n    </td>\n  </tr>\n</table>\n\n### Events\n\n<table>\n  <tr valign=\"top\">\n    <th align=\"left\">Name</th>\n    <th align=\"left\">Description</th>\n  </tr>\n  <tr valign=\"top\">\n    <td><code>statechange</code></td>\n    <td>\n      <p><strong>Properties:</strong></p>\n      <ul>\n        <li><em>newState</em>: <code>string</code> The current lifecycle state the page just transitioned to.</li>\n        <li><em>oldState</em>: <code>string</code> The previous lifecycle state the page just transitioned from.</li>\n        <li><em>originalEvent</em>: <code>Event</code> the DOM event that triggered the state change.</li>\n      </ul>\n      <p>The <code>statechange</code> event is fired whenever the page's lifecycle state changes.</p>\n    </td>\n  </tr>\n</table>\n\n\n## Browser Support\n\n<table>\n  <tr>\n    <td align=\"center\">\n      <img src=\"https://raw.githubusercontent.com/alrra/browser-logos/39.2.2/src/chrome/chrome_48x48.png\" alt=\"Chrome\"><br>\n      ✔\n    </td>\n    <td align=\"center\">\n      <img src=\"https://raw.githubusercontent.com/alrra/browser-logos/39.2.2/src/firefox/firefox_48x48.png\" alt=\"Firefox\"><br>\n      ✔\n    </td>\n    <td align=\"center\">\n      <img src=\"https://raw.githubusercontent.com/alrra/browser-logos/39.2.2/src/safari/safari_48x48.png\" alt=\"Safari\"><br>\n      ✔\n    </td>\n    <td align=\"center\">\n      <img src=\"https://raw.githubusercontent.com/alrra/browser-logos/39.2.2/src/edge/edge_48x48.png\" alt=\"Edge\"><br>\n      ✔\n    </td>\n    <td align=\"center\">\n      <img src=\"https://raw.githubusercontent.com/alrra/browser-logos/39.2.2/src/archive/internet-explorer_9-11/internet-explorer_9-11_48x48.png\" alt=\"Internet Explorer\"><br>\n      9+\n    </td>\n    <td align=\"center\">\n      <img src=\"https://raw.githubusercontent.com/alrra/browser-logos/39.2.2/src/opera/opera_48x48.png\" alt=\"Opera\"><br>\n      ✔\n    </td>\n  </tr>\n</table>\n\nPageLifecycle.js has been tested and known to work in the above browsers.\n","_attachments":{},"homepage":"https://github.com/GoogleChromeLabs/page-lifecycle#readme","bugs":{"url":"https://github.com/GoogleChromeLabs/page-lifecycle/issues"},"license":"Apache-2.0"}