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

raf-run

v0.1.1

Published

requestAnimationFrame based muilti-task runner and handy a setTimeout/setInterval replacement and async support

Downloads

13

Readme

raf-run

requestAnimationFrame based muilti-task runner and handy a setTimeout/setInterval replacement

Simple usage

import Runner from 'raf-run';

const runner = new Runner({ heartbeat: 250 }).start();

// Add a task
function action(task) {
  console.log(`${task.name} TASK:`, task.history.last);
}
runner.add({
  action,         // Required
  name: 'AAA',    // Optional, defaults to auto generated
  times: 5,       // Optional, defaults to infinite
  timeout: 3000,  // Optional, defaults to 0
  interval: 1000, // Optional, defaults to 0
});

// in the console, 3 seconds later...
// AAA TASK: RunHistorySnapshot {delta: 0, stamp: 1614875522153}
// AAA TASK: RunHistorySnapshot {delta: 1017, stamp: 1614875523170}
// AAA TASK: RunHistorySnapshot {delta: 1017, stamp: 1614875524187}
// AAA TASK: RunHistorySnapshot {delta: 1015, stamp: 1614875525202}
// AAA TASK: RunHistorySnapshot {delta: 1001, stamp: 1614875526203}

Runner params in TypeScript format

interface RunnerParams {
  heartbeat?: Number;
  keepAlive?: Boolean;
  historyRun?: {size?: Number};
  historyTask?: {size?: Number};
}

Runner params explanation

import Runner from 'raf-run';

const runner = new Runner({
  // This is how often tasks are evaluated.
  // A task cannot be executed faster than this global interval
  heartbeat: 250, // Optional, defaults to 1000 (ms)

  // requestAnimationFrame based means...
  // navigating to another tab stops the execution.
  // Use "true" if you need your tasks to run in the background
  keepAlive: true, // Optional, defaults to false

  // Runner history props for stats such as time deltas and stamps
  historyRun: { // Optional
    size: 35,   // Optional, history snapshot buffer size, defaults to 10
  },

  // Runner history props for stats such as time deltas and stamps
  historyTask: { // Optional
    size: 55,    // Optional, history snapshot buffer size, defaults to 10
  },
}).start();

Runner management

runner.stop();
runner.start();

Dynamic tick rate

The default interval between each Runner tick is 1000 ms. Here is how to change it

runner.heartbeat = 3000; // The setter only accepts Number

Task management

import Runner from 'raf-run';

const runner = new Runner({ heartbeat: 250 }).start();

// Add a task
function action(task) {
  console.log(`${task.name} TASK:`, task.history.last);
}
runner.add({
  action: () => console.warn('Boo hoo'),
  name: 'BBB',
  times: 15,
});

// Pause the task
runner.disableTask('BBB');

// Resume the task
runner.enableTask('BBB');

// Change how often a task runs
// Again, this value cannot be lower than the global "every"
runner.changeTaskInterval('BBB', 3000);

// Change it again
runner.changeTaskInterval('BBB', 300);

// Remove the task manually
runner.remove('BBB');

Task params in TypeScript format

interface TaskParams {
  action: Function;
  name?: String;
  times?: Number;
  timeout?: Number;
  interval?: Number;
}

Managing tasks directly - not recommended

Having references might be a really bad idea. Complete tasks are removed from the list. If you keep reference to a task that has been destroyed (or failed) are caching garbage with

const task_a = runner.items['AAA_'];

// Enable / disable task
task_a.enable();
task_a.disable();

// Change task interval.
// Only applies to the ones that have this property in the first place.
// If confused, look at "Adding tasks" again.
task_a.interval = 3000; // Task is now being executed every 3 seconds
task_a.interval = 300; // Task is now being executed every 300 ms