UNPKG

@atlaskit/editor-plugin-interactivity

Version:

Interactivity plugin for @atlaskit/editor-core

59 lines 2.04 kB
import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import { bind } from 'bind-event-listener'; import { getDocument } from '@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. */ export var LifecycleObserver = /*#__PURE__*/function () { function LifecycleObserver(handlers) { _classCallCheck(this, LifecycleObserver); this.handlers = handlers; } return _createClass(LifecycleObserver, [{ key: "start", value: function start() { var _this = this; var doc = getDocument(); var unbindVisibilityChange = doc ? bind(doc, { type: 'visibilitychange', listener: function listener() { if (doc.visibilityState === 'hidden') { _this.handlers.onHidden(); } else { _this.handlers.onVisible(); } } }) : undefined; var unbindPageHide = bind(window, { type: 'pagehide', listener: function listener() { return _this.handlers.onPageHide(); } }); var unbindPageShow = 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; } }]); }();