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

iterator-worker

v1.0.0

Published

A class for running an (async or sync) iterator on the current event loop, with methods for starting and stopping its execution.

Downloads

4

Readme

IteratorWorker

A class for running an (async or sync) iterator on the current event loop, with methods for starting and stopping its execution.

Beware

The iterator will be run in the same event loop as the calling code. This means your worker can affect the performance of the rest of your code, including causing it to block your program's execution completely.

It is advised you only use this library in situations that do not have high performance requirements, such as development environments and user scripts. Otherwise you should be running your worker in a separate thread.

Example

import IteratorWorker from 'iterator-worker';

// Use a sleep functon to simulate waiting on i/o
const sleep = n => new Promise(resolve=>setTimeout(resolve, n));

// Example worker function
// Counts upwards, one number per second
async function* count(){
	let i = 0;
	while(true) {
		await sleep(1000);
		console.log(++i);
		yield i;
	}
}

// Start the worker
const worker = IteratorWorker.start(count());

// The worker is now running and will count every second.
// Sleep our code to simulate doing work.
await sleep(10000);

// Stop the worker
await worker.stop();

You can also use the class in a for-await loop, which takes care of starting and stopping the worker automatically:

for await(const {} of new IteratorWorker(count())) {
	// This is equivalent to the previous code
	await sleep(10000);
}

The advantage of doing this is that it will also close the iterator in case your code throws an error.

Dependencies

None

iterator-worker

Iterator Worker

See: default

module.exports ⏏

Kind: Exported class

new module.exports(worker, callback)

Create an instance of IteratorWorker for a given iterator. The callback's signature is the same as general node-style callbacks: function callback(error, result){} In the case of an error the callback will be called with only one argument, and the worker will exit. Otherwise, each time the iterator yields, the callback will be called with the yeilded value as the second argument, and undefined as the first.

| Param | Type | Description | | --- | --- | --- | | worker | Iterable | AsyncIterable | An iterable object | | callback | function | A function called with each iteration result or error |

module.exports.started

True if the worker has started

Kind: instance property of module.exports

module.exports.finished

True if the worker has finished

Kind: instance property of module.exports

module.exports.start() ⇒ Boolean

Starts the iterator in the same event loop.

Kind: instance method of module.exports
Returns: Boolean - False if the iterator had been started previously, otherwise True
Throws:

  • IteratorWorkerError If the worker has already finished.

module.exports.join() ⇒ Promise

Wait for your worker to finish naturally. If your worker uses an infinite loop, this will never resolve; instead you should call stop

Kind: instance method of module.exports
Returns: Promise - Promise that resolves when the worker stops

module.exports.stop() ⇒ Promise

Stop the worker after the current iteration. and wait for it to finish.

Kind: instance method of module.exports
Returns: Promise - Promise that resolves when the worker stops

module.exports.IteratorWorkerError

An error that occurs as part of the management of an IteratorWorker

Kind: static class of module.exports

module.exports.default

A class representing an (async or sync) iterator, with methods for starting or stopping its execution.

The iterator will be run in the same event loop, meaning synchronous code in the worker can still block the execution of your program.

It is advised you only use this library in low-risk environments such as development environments and user scripts. Otherwise you should be running your worker in a separate thread.

Kind: static property of module.exports

module.exports.start()

Constructs and starts the iterator worker. Has the same signature as the regular constructor, and includes the behaviour of the start method.

Kind: static method of module.exports