are-they-here

0.1.0 • Public • Published

are-they-here

are-they-here is a simple package that determines the last active tab of the domain. Because localStorage is persisted across all tabs of a single domain, this kind of tracking is possible.

How it works

  1. All tabs receive a random ID.
  2. When any activity* happens in a tab, it's marked as active.
  3. There can only be one active tab for a domain in a given time.

That is, if there's only one tab of example.com is open, it's always considered active. When two or more tabs of example.com are open and user switches between them, active tab changes.

* - "any activity" means:

  • window received focus;
  • mouse movement detected;
  • document has been scrolled;
  • key press detected.

Installation

Install with yarn:

$ yarn add are-they-here

Usage

are they here provides four functions:

  • areTheyHere() - true if current tab is active, false otherwise;
  • ifTheyAreHere(cb) - perform callback (cb) if current tab is active;
  • unlessTheyAreHere(cb) - perform callback (cb) if current tab is NOT active;
  • whenTheyCome(cb) - perform callback (cb) every time current tab becomes active.

Example

import { unlessTheyAreHere, whenTheyCome } from 'are-they-here'

document.addEventListener('DOMContentLoaded', () => {
  unlessTheyAreHere(() => console.log('You just missed DOMContentLoaded! :/'))
})

whenTheyCome(() => console.log('Greetings to this tab!'))

Package Sidebar

Install

npm i are-they-here

Weekly Downloads

0

Version

0.1.0

License

MIT

Last publish

Collaborators

  • akxcv