@work-bee/inactivity

@work-bee/inactivity

Middleware that detects user inactivity and can trigger actions like cache cleanup.

Install

npm install @work-bee/inactivity

Usage

import { compileConfig, strategyCacheFirst } from "@work-bee/core";
import inactivityMiddleware from "@work-bee/inactivity";

const inactivity = inactivityMiddleware({
  inactivityAllowedInMin: 30,
});

const config = compileConfig({
  strategy: strategyCacheFirst,
  middlewares: [inactivity],
});

addEventListener("message", (event) => {
  const { data } = event;
  if (data.type === "inactivity") {
    inactivity.postMessageEvent();
  }
});

Options

OptionTypeDefaultDescription
inactivityAllowedInMinnumberMinutes of inactivity before triggering the event
inactivityEvent() => voidCallback fired when inactivity threshold is reached

Returns

{ before, after, postMessageEvent }

Client Integration

Import and initialize the client-side script to send activity events to the ServiceWorker:

import initInactivityClient from "@work-bee/inactivity/client.js";

// Use default events (keypress, mousedown, mousemove, scroll, touch, etc.)
initInactivityClient();

// Or specify custom events
initInactivityClient(["keypress", "mousedown", "scroll"]);

This listens for user interaction events and posts { type: "inactivity" } messages to the ServiceWorker.