@atlaskit/editor-plugin-interactivity
Version:
Interactivity plugin for @atlaskit/editor-core
46 lines • 1.52 kB
JavaScript
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 class LifecycleObserver {
constructor(handlers) {
this.handlers = handlers;
}
start() {
const doc = getDocument();
const unbindVisibilityChange = doc ? bind(doc, {
type: 'visibilitychange',
listener: () => {
if (doc.visibilityState === 'hidden') {
this.handlers.onHidden();
} else {
this.handlers.onVisible();
}
}
}) : undefined;
const unbindPageHide = bind(window, {
type: 'pagehide',
listener: () => this.handlers.onPageHide()
});
const unbindPageShow = bind(window, {
type: 'pageshow',
listener: () => this.handlers.onPageShow()
});
this.unbind = () => {
unbindVisibilityChange === null || unbindVisibilityChange === void 0 ? void 0 : unbindVisibilityChange();
unbindPageHide();
unbindPageShow();
};
}
stop() {
var _this$unbind;
(_this$unbind = this.unbind) === null || _this$unbind === void 0 ? void 0 : _this$unbind.call(this);
this.unbind = undefined;
}
}