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

jazzi-observable

v2.0.2

Published

Observable implementation using jazzi

Downloads

6

Readme

Jazzi Observable

Observable implementation using jazzi

Installation

npm install jazzi-observable
//or
yarn add jazzi-observable

Basic Usage

import Observable from "jazzi-observable"

// Create the observable
const obs = Observable.of(1,2,3)

// Subscribe to the observable
const subscription = obs.subscribe({
    next: x => console.log(x)
})

// Unsubscribe
subscription.unsubscribe()

API

Observable Type

This is the default export of the library. It is a huge object with all the constructors, operators and schedulers.

import Observable from 'jazzi-observable'
const { map, filter } = Observable.operators;
const { asyncScheduler } = Observable.schedulers;

const obs = Observable
.fromArray([1,2,3], asyncScheduler)
.pipe(
    map(x => x + 1),
    filter(x => x % 2 === 0)
)

obs.subscribe(console.log) 
// Asyncrously logs
// 2
// 4
// and then completes

Subscription Object

When calling subscribe on an observable, a subscription object is returned. It is actually a function with an unsubcribe attribute. Either calling the function or calling the unsubscribe attribute will unsubscribe from the observable

import { of } from "jazzi-observable/constructors";

const asyncLogger = of(1,2,3)
    .async()
    .tap(console.log);

// Starts the observable
const sub = asyncLogger.subscribe()

// Both have the same effect
sub()
sub.unsubscribe()

Constructors

These are the functions that return an observable. There are 10:

import Observable from 'jazzi-observable';

Observable.Observable(fn)                // Receives a function to call on subscribe. The function receives an observer
Observable.of(...args)                   // Returns an observable that emits the received arguments
Observable.from(fn)                      // Alias of Observable
Observable.pure(someFunction)            // Alias of Observable
Observable.fromPromise(promise)          // Returns an observable from a promise
Observable.fromArray(array)              // Returns an observable that emits the values from an array
Observable.fromEvent(target,eventName)   // Returns an observable that emits when the event triggers
Observable.interval(milliseconds)        // Returns an observable that emits an increasing sequence of integers in an interval
Observable.throwError(anything)          // Returns an observable that immediately throws whatever you pass it
Observable.complete()                    // Returns an observable that immediately completes

All constructors can be imported as standalone functions (with the exception of Observable.Observable) from jazzi-observable/constructors

Operators

There are two ways to use operators. Either as observable methods on an observable instance or as functions that can be passed to the pipe method. Actually, any function that receives and returns an observable can be passed to the pipe method.

import { of } from 'jazzi-observable/constructors';
import { map, mergeAll } from 'jazzi-observable/operators';

of(1,2,3)
.map(x => of(x,x+1,x+2))
.mergeAll()

// or in pipe style

of(1,2,3)
.pipe(
    map(x => of(x,x+1,x+2)),
    mergeAll()
)

All operators are functions that return a function that can be passed to the pipe method so even if they receive no arguments, it must be called. This was decided to maintain consistency. The list of available operators are the following:

  • sequence, collect, audit, auditTime, tap, take, takeWhile, takeLast, takeUntil, skip, mergeAll, mergeMap, concatAll, concatMap, switchAll, switchMap, exhaust, exhaustMap, combineAll, withLatestFrom, async, asap, sync, mapTo, map, fmap, filter, bind, chain, flatMap, effect, peak, flat, join

for more info check API.md

Additional Notes

  • The Observable type was built using jazzi typeclasses. It implements Functor, Filterable, Effect, Monad and Thenable. For more info on that check out jazzi.

  • As Thenable objects, observables can be await'ed. On await, they are subscribed. Optionally you can call toPromise to create a promise from the observable.

import { of, complete, throwError } from 'jazzi-observable/constructors';

async function someAsyncStuff(){
    const firstEmition = await of(1,2,3)
    console.log(firstEmition) // Logs 1 ignores 2 and 3

    const undefinedIfComplete = await complete()
    console.log(undefinedIfComplete) // Logs undefined

    const sameEffectAsJustAwait =  await of(1,2,3).toPromise()
    console.log(sameEffectAsJustAwait) // Logs 1 ignores 2 and 3
}

async function someMoreAsynStuff(){
    try {
        const thisThrows = await throwError(42)
    } catch (e) {
        console.log(`this is ${e}`) // Logs "this is 42"
    }
}