1.0.2 • Published 1 year ago

@autobooks/session-extension v1.0.2

Weekly downloads
-
License
UNLICENSED
Repository
-
Last release
1 year ago

@autobooks/session-extension

This package provides support for session extension in a parent context from activity detected from the Autobooks web application.

Installation

yarn add @autobooks/session-extension

Requirements

Runtime requirements are:

  • Support for ES6
  • Support for Promise.prototype.race

At compile time, you need a bundler that supports commonjs module format.

Support

For support requests, email accountmanagementsupport@autobooks.co

Documentation

Example usage:

import { connect } from "@autobooks/session-extension";

const iframeElement = document.getElementById("app-iframe");

iframeElement.addEventListener("load", () => {
  try {
    const { startKeepAlive, disconnect } = await connect({
      iframeElement,
      targetOrigin: "https://app.autobooks.co",
      connectTimeout: 5000, // optional
    });

    const stopKeepAlive = startKeepAlive(60000, () => {
      fetch("/my-keep-alive-endpoint", { method: "POST", credentials: "include" });
    });

    setTimeout(() => {
      disconnect(); // internally calls stopKeepAlive
      stopKeepAlive();
    }, 10000);
  } catch {
    // error handling
  }
});

The package exports a single function, called connect. connect takes a single argument, an object of this shape:

type Options = {
  iframeElement: HTMLIFrameElement;
  targetOrigin: Origin;
  connectTimeout?: number;
};

It is important that connect be called after the iframeElement has loaded, otherwise it may fail to initialize.

connect returns a Promise<Connection>:

type Connection = {
  startKeepAlive: (pollIntervalMS: number, onKeepAlive: () => void) => () => void;
  disconnect: () => void;
};

You can then use this Connection to ask the Autobooks client to inform you when a user has been active in the last n milliseconds using the startKeepAlive function. Every n milliseconds, the Autobooks client will trigger the onKeepAlive callback if and only if the client detected user activity in that time period.