UNPKG

@atlaskit/editor-plugin-interactivity

Version:

Interactivity plugin for @atlaskit/editor-core

66 lines (65 loc) 2.46 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.LifecycleObserver = void 0; var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _bindEventListener = require("bind-event-listener"); var _browserApis = require("@atlaskit/browser-apis"); /** * Reports the page lifecycle signals that force an extra snapshot: the tab being hidden or * shown again, and the page being unloaded or restored from the back/forward cache. * * None of them is guaranteed to arrive — a mobile browser being killed raises nothing at all. * Snapshots carry session-to-date values, so losing the last one costs only the tail of that * session. */ var LifecycleObserver = exports.LifecycleObserver = /*#__PURE__*/function () { function LifecycleObserver(handlers) { (0, _classCallCheck2.default)(this, LifecycleObserver); this.handlers = handlers; } return (0, _createClass2.default)(LifecycleObserver, [{ key: "start", value: function start() { var _this = this; var doc = (0, _browserApis.getDocument)(); var unbindVisibilityChange = doc ? (0, _bindEventListener.bind)(doc, { type: 'visibilitychange', listener: function listener() { if (doc.visibilityState === 'hidden') { _this.handlers.onHidden(); } else { _this.handlers.onVisible(); } } }) : undefined; var unbindPageHide = (0, _bindEventListener.bind)(window, { type: 'pagehide', listener: function listener() { return _this.handlers.onPageHide(); } }); var unbindPageShow = (0, _bindEventListener.bind)(window, { type: 'pageshow', listener: function listener() { return _this.handlers.onPageShow(); } }); this.unbind = function () { unbindVisibilityChange === null || unbindVisibilityChange === void 0 || unbindVisibilityChange(); unbindPageHide(); unbindPageShow(); }; } }, { key: "stop", value: function stop() { var _this$unbind; (_this$unbind = this.unbind) === null || _this$unbind === void 0 || _this$unbind.call(this); this.unbind = undefined; } }]); }();