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
- All tabs receive a random ID.
- When any activity* happens in a tab, it's marked as active.
- 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!'))