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

@shmish/observable

v1.4.0

Published

ESM ES observable module, for personal use

Downloads

6

Readme

@shmish/observable

npm install @shmish/observable --save

Usage

Observable

An observable is an object that represents a collection of values arriving syncrounously or asyncrounously. Using this constructor directly is not recomended unless absolutly nessasarry. In practice, it is almost always better to use one of the static creation methods such as Observable.fromEvent and Observable.of

// Example
const myObservable = new Observable(observer => {
  setInterval(observer.next, 1000, 'some data')
})

Observable.of(...args)

Observable.of takes any number of arguments and returns a new Observable collection of the arguments (in order).

// Example
const myFirstObservable = Observable.of(2, 4, 6)
const mySecondObservable = Observable.of('hello', 3, 56, 0x10)

Observable.from(iterable)

Observable.from takes a single iterable as an argument, such as a String, Array, or Generator and returns a new Observable collection of the itterable argument.

// Example
const myFirstObservable = Observable.from([1, 2, 4])
const MySecondObservable = Observable.from('hello world')

Observable.fromEvent(emiter, ...events)

Observable.fromEvent takes an event emitter (implements on or addEventListener) and any number of events to subscribe to. The Observable will automaticly notify any attached observers when an event is triggered.

// Example
const $button = document.getElementById('button')
const clickStream = Observable.fromEvent($button, 'click', 'touch')

Observable.prototype.subscribe(observer | [functions])

Observable.prototype.subscribe takes either an Observer object or a next, error, and complete callback (in that order). .subscribe returns a Subscription object, representing the data channel between the Observable collection and the Observer

// Example: callbacks
const subsription = Observable.of(1, 2, 3).subscribe(
  val => console.log(val),     // Next
  err => console.error(err),   // Error
  () => console.log('done')    // Complete
)

//=> 1
//=> 2
//=> 3
//=> 'done'
// Example: Observer
// Note: all overrides are optional
class Logger extends Observer {
  next (val) { console.log(val) }  // Override 'next' method
  error (err) { console.error(err) }  // Override 'error' method
  complete () { console.log('done') }  // Override 'complete' method
}

const subscription = Observable.of(1, 2, 3).subscribe(new Logger())

//=> 1
//=> 2
//=> 3
//=> 'done'

Observable.prototype.map(functor)

The Observable.prototype.map method creates a new Observable with the results of calling the provided function on each value passing through the calling Observable

// Example
const subscription = Observable.of(1, 2, 3)
  .map(x => x ** 2)
  .map(x => x - 1)
  .subscribe(x => console.log(x))

//=> 0
//=> 3
//=> 8


// Another example
const subscription = Observable.from('hello')
  .map(c => c.toUpperCase())
  .subscribe(c => console.log(c))

//=> 'H'
//=> 'E'
//=> 'L'
//=> 'L'
//=> 'O'

Observable.prototype.filter(functor)

The Observable.prototype.filter method creates a new Observable with all the elements that pass the test of the provided function

// Example
const subscription = Observable.of(1, 2, 3, 4)
  .filter(x => x % 2 === 0)
  .subscribe(x => console.log(x))

//=> 2
//=> 4

Observable.prototype.reduce(functor, [initial])

The Observable.prototype.reduce method return a Promise of the value resulting from applying the provided function against an accumulatorand each element passing throught the observable. The function takes the form of (accumulator, value) => newVal

// Example
const myPromise = Observable.of(1, 2, 3)
  .reduce((acc, val) => acc + val)

//=> Promise {val: 6}

// Another example
Observable.of(1, 2, 3)
  .reduce((acc, val, i) => acc + val + i)  // Here, an optional index param is passed
  .then(x => console.log(x))

//=> 9

Observable.prototype.wait(delayMS)

The Observable.prototype.wait method produces a new Observable that 'holds' values for a provided delay before parring them on.

// Example
Observable.of(1, 2, 3)
  .wait(200)
  .subscribe(x => console.log(x))