Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The pjs.session.onEnd function can be defined in any PJS module and allows users to execute custom code when a browser session is ended. This function is useful in situations where you need to perform cleanup operations, log data, or trigger any specific actions after the browser session ends.

Example

This example shows how the pjs.session.onEnd hook can be used to clean up resources when the browser is closed.

Code Block
const { spawn } = require("child_process");

const childProcesses = [];

function main() {
  pjs.defineDisplay("display", "pjs-1118-ws.json");

  const fields = { exit: false };

  pjs.session.onEnd = onEnd;

  while (!fields.exit) {
    display.screen.execute(fields);
    if (fields.spawn) {
      const cp = spawn("node", ["modules/pjs-1118-ws/interval.js"], { stdio: "inherit" });
      childProcesses.push(cp);
    }
  }
  flags.LR = true;
}


function onEnd() {
  while (childProcesses.length > 0) {
    const childProcess = childProcesses[0];
    if (childProcess) {
      childProcess.kill("SIGKILL");
    }
    childProcesses.shift();
  }
}