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

interruptible

v0.0.2

Published

Make functions interruptible

Downloads

7

Readme

Interrruptible.js

interruptible is a library that makes asynchronous functions interruptible by using generators.

Usage

To mark an async function as interruptible, convert it to a generator, and use yield to indicate where your function can be interrupted.

From:

async function work (a, b) {
  const foo = await makeFoo(a, b)
  const result = await processFoo(foo)
  return result
}

const result = await work()

To:

import {asInterruptible, InterruptError} from 'interruptible'


async function *interruptibleWork (a, b) {
  const foo = yield makeFoo(a, b)  // Use `yield` to indicate that function can be interrupted after this async operation has resolved

  // 🚫  If operation is interrupted before this line is reached, execution will stop here.

  const result = await processFoo(foo)  // `await` wont stop execution even if operation is interrupted
  return result
}

const interruptibleTask = asInterruptible(interruptibleWork)
try {
  const result = await interruptibleTask.run(this, 'a', 'b')
} catch(error) {
  if (error instanceof InterruptError) {
    console.log('Interruptible task interrupted')
    return
  }
  console.log('Interruptible task errored')
}


// You can interrupt the task from another execution context
setTimeout(() => {
  if (someConiditon) {
    interruptibleTask.interrupt()
  }
}, 100)

Advanced Usage

Nested Generators

You can nest generators within generators, and interrupt them as well. The interrupt will buble all the way to the parent generator:


function *makeFoo(a, b) {
  // Execution can also be stopped inside this generator
  const resA = yield makeA(a)
  const resB = yield makeB(b)
  return resA + resB
}


async function *interruptibleWork (a, b) {
  const foo = yield makeFoo(a, b)

  const result = await processFoo(foo)  // `await` wont stop execution even if operation is interrupted
  return result
}

Async Generators

You may have noticed that in our example we used both await and yield keywords:


async function *interruptibleWork (a, b) {
  const foo = yield makeFoo(a, b)
              -----

  const result = await processFoo(foo)  // `await` wont stop execution even if operation is interrupted
                 -----
  return result
}

These are async generators, and are an ECMAScript Stage 3 proposal. In order to write these functions, you'll need to transpile your code with the appropriate babel plugin.

Motivation

You may want to interrupt code for different reasons, principally in order to prevent work that is no longer needed.

The motivation for writing interruptible comes from the need of having a simple abstraction to be able write complex asynchronous tasks that could be interrupted at different points of their execution by a scheduling system.

It is trivial to interrupt a single function using a conditional based on some state, but it becomes extremely cumbersome to do so systematically across large amounts of code when you want to be able to interrupt at different execution points, and it ends up littering the code with if statements.

interruptible provides a simple mechanism to do so, albeit with the cost of additional mental overhead of keeping in mind what using yield implies.

API

See libdef.js