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

hertzscript-dispatcher

v0.0.19

Published

Executes preemptible JavaScript coroutines which conform to the HertzScript specification.

Downloads

12

Readme

HertzScript Coroutine Dispatcher

NPM

The HertzScript coroutine dispatcher executes code that was compiled by the HertzScript Compiler.

See the HertzScript Specification repository for more information.

There are multiple ways to use this dispatcher.

  • Command-line executor
  • Dispatcher module

The dispatcher module can be run in multiple different execution modes:

  • Run-To-Completion
  • Synchronous quantum/time-slicing
  • Asynchronous quantum/time-slicing

Command-Line Interfaces

hzs / hzscript

Execute source code with optional hot-compiling via stdin, argument, or file input.

Command-Line Options

-i path (--input)

  • This option supplies the text from the given filepath as the input JavaScript source code you would like to execute. If this option is set to nothing, or is not set at all, then the source code is consumed via the standard input of the terminal.

-s code (--source)

  • If set, this option specifies the tring which follows as the input JavaScript source code which you would like to execute. If this option is set to nothing, or is not set at all, then the source code is consumed via the standard input of the terminal.

-a quantum (--async)

  • If set, this option specifies the time slice quantum of the dispatcher so that it cooperates with the built-in asynchronous event loop. The dispatcher will pause the running code when the duration of its execution reaches or exceeds the time slice, allowing the event loop to continue operating. 5 milliseconds after pausing, the dispatcher will resume execution until the time slice quantum is reached again.

-c (--compile)

  • If set, then hertzscript-compiler will pre-process the input source code before it is executed. Useful for quick testing without having to manually compile to another file every time you want to execute the script.

--spawn

  • If set in addition to the --compile option, then hertzscript-compiler will be used to detect and compile the spawn keyword. If the --compile option is not present, then this option will do nothing.

Dispatcher Module

The dispatcher itself is the main module, so you can import it simply like this:

const Dispatcher = require("hertzscript-dispatcher");
// Instantiate the Class
const dispatcher = new Dispatcher();

Dispatcher.prototype.import

Imports a pre-compiled HertzScript Module into the dispatcher, spawning it as a new coroutine. The module is not immediately executed with this method and is only enqueued for later execution.

Function Parameters

dispatcher.import( hzModule );

hzModule Function

  • A Function which was previously compiled with hertzscript-compiler in module mode.

Dispatcher.prototype.exec

This method immediately executes the given pre-compiled function or HzModule in run-to-completion mode.

Return Value

The returned value is the last value returned by the top-most function.

Function Parameters

dispatcher.exec( functor [, thisArg = null [, args = null ]] );

functor Function

  • A function or HzModule which was previously compiled with hertzscript-compiler.

thisArg (Optional) Any

  • An optional value to set the this variable to when calling functor.

args (Optional) Array

  • An optional array to supply as arguments when calling functor.

Dispatcher.prototype.spawn

Enqueues a new coroutine. The coroutine is not immediately executed with this method and is only enqueued for later execution.

Function Parameters

dispatcher.spawn( functor [, thisArg = null [, args = null ]] );

functor Function

  • A function which was previously compiled with hertzscript-compiler.

thisArg (Optional) Any

  • An optional value to set the this variable to when calling functor.

args (Optional) Array

  • An optional array to supply as arguments when calling functor.

Dispatcher.prototype.enqueue

Adds a pre-compiled function to the end of the active coroutine's call stack, or creates a new coroutine for it if there is no active coroutine. The coroutine is not immediately executed with this method and is only enqueued for later execution.

Function Parameters

dispatcher.enqueue( functor [, thisArg = null [, args = null ]] );

functor Function

  • A function which was previously compiled with hertzscript-compiler.

thisArg (Optional) Any

  • An optional value to set the this variable to when calling functor.

args (Optional) Array

  • An optional array to supply as arguments when calling functor.

Dispatcher.prototype.cycle

If the dispatcher is running, then this method cycles the dispatcher for a given duration.

Return Value

The returned value is the last value returned in the most recent cycle.

Function Parameters

dispatcher.cycle( [ quantum = null [, throwUp = false ]] );

quantum (Optional) Number

  • A timeslice quantum, in milliseconds, which specifies the approximate maximum length of time the dispatcher should cycle.

throwUp (Optional) Boolean

  • This argument affects how the dispatcher handles uncaught errors. If set to true then the dispatcher will stop cycling and re-throw any uncaught errors, otherwise it will terminate the active coroutine and continue cycling.

Dispatcher.prototype.runSync

Sets the dispatcher to running state and cycles it for a given duration.

Return Value

The returned value is the last value returned in the most recent cycle.

Function Parameters

dispatcher.runSync( [ quantum = null [, throwUp = false ]] );

quantum (Optional) Number or Boolean

  • A timeslice quantum, in milliseconds, which specifies the approximate maximum length of time the dispatcher should cycle. If set to false then the dispatcher will continue cycling until all coroutines have finished executing.

throwUp (Optional) Boolean

  • This argument affects how the dispatcher handles uncaught errors. If set to true then the dispatcher will stop cycling and re-throw any uncaught errors, otherwise it will terminate the active coroutine and continue cycling.

Dispatcher.prototype.runAsync

Sets the dispatcher to running state and cycles it for a given duration in a given interval, scheduling cycles in the acynchronous event loop using setTimeout. Can be stopped via Dispatcher.prototype.stop.

Return Value

Either a Promise which resolves with the returned value is the last value returned by the top-most function, or undefined if this method is already running.

Function Parameters

dispatcher.runAsync( [ interval = 30 [,quantum = null [, throwUp = false ]]] );

interval (Optional) Number

  • A cycle delay, in milliseconds, which specifies the length of time to wait in between cycles. During this time, other functions in the asynchronous event loop may execute.

quantum (Optional) Number

  • A timeslice quantum, in milliseconds, which specifies the approximate maximum length of time the dispatcher should cycle per-interval.

throwUp (Optional) Boolean

  • This argument affects how the dispatcher handles uncaught errors. If set to true then the dispatcher will stop cycling and re-throw any uncaught errors, otherwise it will terminate the active coroutine and continue cycling.

Dispatcher.prototype.runComplete

Sets the dispatcher to running state and cycles it in run-to-completion mode, only returning when all coroutines have finished executing.

Return Value

The returned value is the last value returned by the top-most function.

Function Parameters

dispatcher.runComplete( [ throwUp = false ] );

throwUp (Optional) Boolean

  • This argument affects how the dispatcher handles uncaught errors. If set to true then the dispatcher will stop cycling and re-throw any uncaught errors, otherwise it will terminate the active coroutine and continue cycling.

Dispatcher.prototype.stop

Stops the dispatcher from cycling, then resets the runqueue's blockIndex and activeBlock. Can be used to stop runAsync.