npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

visibility-listener

v1.0.0

Published

Track document visibility state seamlessly across browsers

Downloads

17

Readme

visibility-listener

npm version GitHub license

Track document visibility state seamlessly across browsers.

Installation

You can install the package using npm/yarn/pnpm:

npm install visibility-listener
# or
yarn add visibility-listener
# or
pnpm add visibility-listener

Examples

import createVisibilityStateListener from 'visibility-listener';

const listener = createVisibilityStateListener();

listener.on('update', (state) => {
  console.log('🔄️ current state =>', state);

  if (state === 'visible') {
    // do something
  } else if (state === 'hidden') {
    // do another thing
  }

  const lastChangeTime = listener.getLastStateChangeTime();
  console.log('🕒 last change time =>', new Date(lastChangeTime));
});


listener.start();

API

createVisibilityStateListener(opts?: VisibilityStateListenerOptions): VisibilityStateListener

Creates a visibility state listener instance.

  • opts (optional): Options object to customize the listener. Available options:
    • window: Custom window object to use for the listener.
    • document: Custom document object to use for the listener.
    • eventNames: Custom event names to use. Supported key:
      • update: Custom name for the update event.

Returns a VisibilityStateListener object with the following methods:

on(eventName: string, handler: Function): void

Adds an event listener for the specified event.

  • eventName (required): The name of the event to listen for.
  • handler (required): The event handler function.

start(): boolean

Starts the visibility state listener.

Returns true if the listener started successfully, or false if there was an error during initialization.

pause(): boolean

Pauses the visibility state listener, preventing the emission of events.

Returns true if the listener was paused successfully, or false if there was an error.

hasError(): boolean

Checks if there is an error with the listener.

Returns true if there is an error, or false if there is no error.

getError(): string | null

Gets the error message associated with the listener, if any.

Returns the error message as a string, or null if there is no error.

getState(): string

Gets the current visibility state.

Returns the current visibility state as a string.

getLastStateChangeTime(): number

Gets the timestamp of the last state change in milliseconds.

Returns the timestamp of the last state change as a number.

getStateChangeCount(): number

Gets the total number of state changes since the listener started.

Returns the total number of state changes as a number.


License

This package is released under the MIT License.