Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

indefinite-observable

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

indefinite-observable

<!-- TODO: update version number before releasing, here and in the script tag --> [![Current version:](https://img.shields.io/badge/v2.0.1:-222222.svg?logo=npm)](https://www.npmjs.com/package/indefinite-observable/v/2.0.1) [![Test status](https://img.shie

  • 2.0.1
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
175K
decreased by-10.78%
Maintainers
1
Weekly downloads
 
Created

What is indefinite-observable?

The indefinite-observable npm package provides a simple and lightweight way to create observables that can emit values indefinitely. It is particularly useful for scenarios where you need to handle asynchronous data streams, such as user input events, WebSocket messages, or other continuous data sources.

What are indefinite-observable's main functionalities?

Creating an Observable

This feature allows you to create an observable that emits a sequence of numbers every second. The observer's `next` method is called with the current count, and the observable can be unsubscribed from to stop the emission.

const { create } = require('indefinite-observable');

const observable = create(observer => {
  let count = 0;
  const intervalId = setInterval(() => {
    observer.next(count++);
  }, 1000);

  return () => clearInterval(intervalId);
});

const subscription = observable.subscribe(value => {
  console.log(value);
});

// To unsubscribe
// subscription.unsubscribe();

Handling Errors

This feature demonstrates how to handle errors within an observable. When the count reaches 5, an error is emitted, and the observer's `error` method is called.

const { create } = require('indefinite-observable');

const observable = create(observer => {
  let count = 0;
  const intervalId = setInterval(() => {
    if (count === 5) {
      observer.error(new Error('An error occurred at count 5'));
    } else {
      observer.next(count++);
    }
  }, 1000);

  return () => clearInterval(intervalId);
});

const subscription = observable.subscribe({
  next: value => console.log(value),
  error: err => console.error(err)
});

Completing an Observable

This feature shows how to complete an observable. When the count reaches 5, the observer's `complete` method is called, signaling that no more values will be emitted.

const { create } = require('indefinite-observable');

const observable = create(observer => {
  let count = 0;
  const intervalId = setInterval(() => {
    if (count === 5) {
      observer.complete();
    } else {
      observer.next(count++);
    }
  }, 1000);

  return () => clearInterval(intervalId);
});

const subscription = observable.subscribe({
  next: value => console.log(value),
  complete: () => console.log('Observable completed')
});

Other packages similar to indefinite-observable

FAQs

Package last updated on 13 Dec 2018

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