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

afk

v1.0.0

Published

Trigger an action when the activity status of the user changes

Downloads

25

Readme

node-afk

Trigger an action when the activity status of the user changes.

Prerequisites

  • Node.js >= 8.0.0

Installation

npm install afk

This module uses a native module that needs to be built using node-gyp.

On linux you will need to install libxss-dev and pkg-config to ensure that the native module dependency can be built.

Usage

const NodeAFK = require('node-afk');

const inactivityDuration = 1000 * 10; // the user will be considered `idle` after 10 seconds

const afk = new NodeAFK(inactivityDuration);

afk.init();

afk.on('status:idle', () => {
  // the status of the user changed from `active` to `idle`
});

afk.on('status:active', () => {
  // the status of the user changed from `idle` to `active`
})

node-afk is an event emitter and emits the following events:

  • status:idle - the status of the user changed from active to idle
  • status:active - the status of the user changed from idle to active
  • status-changed - the status of the user changed. An object is passed to the listener for this event containing details of the previous and current status.
  • error - an error occured
afk.on('status-changed', ({ previousStatus, currentStatus }) => {
  // `previousStatus` is the status of the user before their status changed
  // `currentStatus` is the status of the user after their status changed
})

You can unregister a listener from an event using the off method of the event emitter:

afk.off('status:idle', idleListener);

You can also setup a listener that is executed when the status of the user has been their current status for a certain duration:

afk.on('idle:5000', () => {
  // the user has been `idle` for 5 seconds
});

afk.on('active:15000', () => {
  // the user has been `active` for 15 seconds
});

These events will be emitted each time the status of the user is changed.

If an error occurs whilst trying to retrieve the idle time from the system an error event will be emitted:

afk.on('error', (err) => {
  // an error occurred
});

API

constructor(inactivityDuration, [pollInterval])

Create a new instance of node-afk

  • inactivityDuration - How long (in ms) until the user can be inactive until they are considered as idle
  • pollInterval - How often (in ms) should node-afk query the system to get the the amount of time that the user has been away for (1000ms by default)

on(eventName, listener)

Register a listener on an event

  • eventName - status:active, status:idle, status-changed, <status>:<time>, error
  • listener - Function to be executed when the event is emitted

off(eventName, listener)

Unregister a listener from an event

  • eventName - The name of the event
  • listener - The listener associated with the event

init()

Initalise the node-afk instance.

This is required to be called so that the poll interval is setup.

destroy()

Stops the poll interval and removes all event listeners.

Contributing

Contributions are always welcome. Make sure you write tests for anything you add or change. We also enforce AirBNB ESLint rules.

License

MIT