Socket
Socket
Sign inDemoInstall

@repeaterjs/repeater

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@repeaterjs/repeater

The missing constructor function for creating safe async iterators


Version published
Maintainers
1
Created

What is @repeaterjs/repeater?

The @repeaterjs/repeater package provides a way to create and work with async iterators and generators in JavaScript. It allows for the creation of push-based streams that can be used with async/await and for..of loops, making it easier to handle asynchronous data flows.

What are @repeaterjs/repeater's main functionalities?

Creating Repeaters

This feature allows you to create a new Repeater instance. The constructor takes an executor function that receives two arguments: push and stop. You can push values to the repeater and stop it when needed.

const { Repeater } = require('@repeaterjs/repeater');

const repeater = new Repeater(async (push, stop) => {
  push('Hello, Repeater!');
  stop();
});

(async () => {
  for await (const item of repeater) {
    console.log(item);
  }
})();

Handling Asynchronous Events

This code sample demonstrates how to create a repeater that listens for and handles asynchronous events, such as DOM events. The repeater pushes click events to the consuming async iterator.

const { Repeater } = require('@repeaterjs/repeater');

const eventRepeater = new Repeater(async (push, stop) => {
  document.addEventListener('click', push);
  await stop;
  document.removeEventListener('click', push);
});

(async () => {
  for await (const event of eventRepeater) {
    console.log('Clicked at:', event.clientX, event.clientY);
  }
})();

Combining Multiple Sources

This example shows how to combine multiple asynchronous sources into a single repeater. In this case, a timeout is used to push a value after a delay, but other async sources could be combined in a similar manner.

const { Repeater } = require('@repeaterjs/repeater');

const combinedRepeater = new Repeater(async (push, stop) => {
  const timeoutId = setTimeout(push, 1000, 'Timeout!');
  try {
    await stop;
  } finally {
    clearTimeout(timeoutId);
  }
});

(async () => {
  for await (const item of combinedRepeater) {
    console.log(item);
  }
})();

Other packages similar to @repeaterjs/repeater

FAQs

Package last updated on 02 Sep 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc