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

92green-rxjs

v0.10.0

Published

Rxjs algorithms

Downloads

309

Readme

92green-rxjs

yarn add 92green-rxjs

Rxjs algorithms.

multiCache

Accepts an array of sources (intended to be things like in-memory caches, database caches and actual data sources) and for each item attempts to retrieve the item from each source sequentially until it is found. It also saves items in caches once found. it does not guarantee item order.

import {multiCache} from '92green-rxjs';

let myMemoryCacheOperator = {
    name: 'memory-cache',
    load, // operator that loads from cache
    save, // operator that saves to cache (not needed on data source)
    clear, // operator that clears item from cache (not needed on data source)
};

// ^ all of the above operators must accept a payload of {id, item} and return {id, item}
//   item may be undefined if not known or not found

let cache = multiCache([
    myMemoryCacheOperator, // first cache
    myDatabaseCacheOperator, // deeper cache
    myDataSource // data source
])

from([
    {id: 'a'},
    {id: 'b'},
    {id: 'c'}
])
    .pipe(cache.load);

// Example output observable:

// {id: 'a', item: {id: 'a', name: 'Hi'}, source: 'data-source'}
// {id: 'b', item: {id: 'b', name: 'Hello'}, source: 'memory-cache'}
// {id: 'c', item: undefined, source: undefined}

memoryCache

An rx cache that works a lot like graphql/dataloader.

Usage with multiCache:

import {from} from 'rxjs'
import {flatMap} from 'rxjs/operators'
import {multiCache} from '92green-rxjs';
import {memoryCache} from '92green-rxjs';

let dataSource = {
    name: 'data-source',
    load: flatMap(async (payload) => ({
        ...payload,
        item: await fetch(payload.id)
    }))
};

let cache = multiCache([
    memoryCache(),
    dataSource
]);

from([
    {id: 'a'},
    {id: 'b'},
    {id: 'a'}
])
    .pipe(cache.load);


// Example output observable:

// {id: 'a', item: {id: 'a', name: 'Hi'}, source: 'data-source'}
// {id: 'b', item: {id: 'b', name: 'Hello'}, source: 'data-source'}
// {id: 'a', item: {id: 'a', name: 'Hi'}, source: 'memory-cache'}
// a is only requested once

zipDiff

Takes two observables and finds which items exist in one or both, according to a key. It emits items with matching items zipped together on an object: {a: itemA, b: itemB}, and emits unmatched items on objects with single keys like {a: itemA}, {b: itemB}.

The keyBy function is called on each item. The data returned is used as an identifier to work out when two items are matching.

A second keyBy function can be added if you need a different keyBy function for the second observable.

Items are emitted as early as they can be, so the internal buffer can try and be as small as possible. Pairs of matching items are emitted as soon as they match, and non-matching items are emitted as soon as it's known that it's impossible that there will be a match. (e.g. if the buffer contains items from A, and input observable B completes, then it's known there will never be matches for any buffered A's, so they are all emitted)

import {zipDiff} from '92green-rxjs';

zipDiff(
    obsA: Observable,
    obsB: Observable,
    keyBy: (item) => any,
    keyByB?: (item) => any
): Observable
zipDiff(obsA, obsB, itemA => itemA.id)

// obsA adds {id: "1", name: "One"}
// obsA adds {id: "2", name: "Two"}
// obsB adds {id: "3", name: "Three"}
// obsB adds {id: "2", name: "Too"}

// output:

{
    a: {id: "2", name: "Two"},
    b: {id: "2", name: "Too"}
}

{
    a: {id: "1", name: "One"}
}

{
    b: {id: "3", name: "Three"}
}

operators/bufferDistinct

Buffers items while the given predicate function returns the same thing, and emits the buffer contents when the given predicate function returns something else or the stream closes. Value comparison is performed using Object.is().

If flushObs is passed, the buffer will be flushed any time flushObs emits.

import {bufferDistinct} from '92green-rxjs/operators';

bufferDistinct(item => comparisonValue, flushObs: ?Observable)
obs.pipe(
    bufferDistinct(item => item.id)
);

// Obs adds {id: 'a', value: 1}
// Obs adds {id: 'a', value: 2}
// Obs adds {id: 'b', value: 3}
// Obs adds {id: 'c', value: 4}
// Obs adds {id: 'c', value: 5}

// output:

[
    {id: 'a', value: 1},
    {id: 'a', value: 2}
]

[
    {id: 'b', value: 3}
]

[
    {id: 'c', value: 4},
    {id: 'c', value: 5}
]

operators/complete

Emits a single item upon completion of the observable.

import {complete} from '92green-rxjs/operators';

complete()
obs.pipe(
    complete(),
    tap(() => {
        console.log("I'm done mate");
    })
);

dynamodb/batchGetWithRetry

Turns AWS DocClient.batchGet() into a pipeable observable which accepts an observable of ids and calls batchGet(), batching items to 100 at a time and retrying dynamo's UnprocessedKeys automatically.

import {batchGetWithRetry} from '92green-rxjs/dynamodb';

batchGetWithRetry({
    docClient: DocClient,
    tableName: string,
    returnItems?: boolean
}): Observable => Observable
let keys = [
    {id: 1},
    {id: 2},
    {id: 3}
];

from(keys)
    .pipe(batchGetWithRetry(...))
    .toPromise();

dynamodb/batchWriteWithRetry

Turns AWS DocClient.batchWrite() into a pipeable observable which accepts an observable of params and calls batchWrite(), batching items to 25 at a time and retrying dynamo's UnprocessedItems automatically.

import {batchWriteWithRetry} from '92green-rxjs/dynamodb';

batchWriteWithRetry({
    docClient: DocClient,
    tableName: string,
    returnItems?: boolean
}): Observable => Observable
from([{
    {
        PutRequest: {
            Item: {
                foo: 200
            }
        }
    }
    ...
}])
    .pipe(batchWriteWithRetry(...))
    .toPromise();

dynamodb/queryAll

Turns AWS DocClient.query() into an observable which will by default keep requesting whenever there is more data to be paginated.

import {queryAll} from '92green-rxjs/dynamodb';

queryAll(
    docClient: DocClient,
    params: Params,
    feedbackObservable: ?Observable
): Observable

dynamodb/scanAll

Turns AWS DocClient.scan() into an observable which will by default keep requesting whenever there is more data to be paginated.

import {scanAll} from '92green-rxjs/dynamodb';

scanAll(
    docClient: DocClient,
    params: Params,
    feedbackObservable: ?Observable
): Observable