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

hexnut-sequence

v0.2.2

Published

Sequencing middleware for the HexNut framework

Downloads

1

Readme

hexnut-sequence

hexnut-sequence is a hexnut middleware for creating synchronised, sequenced conversations between server and client. It can also be used with hexnut-client.

hexnut-sequence uses generators to structure code that sends and receives specific messages over time in a synchronous way, much like async/await does for promises.

There are currently 3 kinds of sequences:

  1. connection sequences: These run on when a client first connects
  2. interruptible sequences: These sequences can be interrupted and continued later on
  3. uninterruptible sequences: These sequences cannot be interrupted, and will lose state is a message is not matched correctly

Installing

npm i hexnut-sequence

Usage

Connection Sequence

sequence.onConnect(generatorFunction)

Note: The generatorFunction receives the same arguments as a regular middleware (ctx, next)

const sequence = require('hexnut-sequence');

app.use(sequence.onConnect(function* (ctx) {
  // Client connected, perhaps send them an initial message
  ctx.send('handshake part 1');

  // Wait for the corresponding handshake message
  // Should be in the form: "handshake part 2:<the users name>"
  const handshakePart2 = yield sequence.matchMessage(msg => {
    return msg.startsWith('handshake part 2:');
  });

  const namePart = handshakePart2.split(':')[1];

  // Sequence should fail if the name is not provided
  yield sequence.assert(restOfMessage.length > 0);

  // Set the name on the ctx object
  ctx.usersName = namePart;

  // Send a final message
  ctx.send(`Welcome ${ctx.usersName}`);
}));

Interruptible Sequence

sequence.interruptible(generatorFunction)

Note: The generatorFunction receives the same arguments as a regular middleware (ctx, next)

const sequence = require('hexnut-sequence');
const bodyparser = require('hexnut-bodyparser');

app.use(bodyparser.json());

app.use(sequence.interruptible(function* (ctx) {
  // A sequence where the user starts a fitness session and
  // records data, and then finalises their session.
  // During that time, they might send other kinds of data,
  // So an interruptible sequence makes sense.

  yield sequence.matchMessage(msg => msg.type === 'startFitnessSession');

  ctx.send('Starting fitness session');

  while (true) {
    const message = yield sequence.matchMessage(msg => {
      return msg.type === 'logExercise'
          || msg.type === 'logHeartRate'
          || msg.type === 'endFitnessSession';
    });

    if (msg.type === 'logExercise') {
      yield sequence.await(() => saveExerciseLogToDb(msg.value));
    } else if (msg.type === 'logHeartRate') {
      yield sequence.await(() => saveHeartRateMeasurementToDb(msg.value));
    } else {
      // End the session
      break;
    }
  }

  ctx.send('Ending fitness session');
}));

Uninterruptible Sequence

sequence.uninterruptible(generatorFunction)

Note: The generatorFunction receives the same arguments as a regular middleware (ctx, next)

const sequence = require('hexnut-sequence');

app.use(sequence.uninterruptible(function* (ctx) {
  // A sequence where the user sends the konami code
  // If at any point the sequence is broken it must restart from the beginning

  yield sequence.matchMessage(msg => msg === 'up');
  yield sequence.matchMessage(msg => msg === 'up');
  yield sequence.matchMessage(msg => msg === 'down');
  yield sequence.matchMessage(msg => msg === 'down');
  yield sequence.matchMessage(msg => msg === 'left');
  yield sequence.matchMessage(msg => msg === 'right');
  yield sequence.matchMessage(msg => msg === 'left');
  yield sequence.matchMessage(msg => msg === 'right');
  yield sequence.matchMessage(msg => msg === 'b');
  yield sequence.matchMessage(msg => msg === 'a');

  ctx.send('Code Accepted');
}));