Catalog npm
Back to packageRELEASE DETAILS

0.9.0

npm

92green-rxjs

Rxjs algorithms

Published5 Aug 2021
Direct dependencies6
Transitive dependenciesNot calculated

README

Current package documentation

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

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Declared dependencies 7

Native requirements
6 direct targetsNot calculated transitive count
Package / targetRequirementRelationship
@babel/runtime
Environment
{
  "local_name": "@babel/runtime",
  "alias": false,
  "peer_metadata": null
}
^7.1.5runtime
@babel/cli
Environment
{
  "local_name": "@babel/cli",
  "alias": false,
  "peer_metadata": null
}
^7.1.2development
@babel/core
Environment
{
  "local_name": "@babel/core",
  "alias": false,
  "peer_metadata": null
}
^7.1.2development
babel-preset-blueflag
Environment
{
  "local_name": "babel-preset-blueflag",
  "alias": false,
  "peer_metadata": null
}
^1.0.0development
blueflag-test
Environment
{
  "local_name": "blueflag-test",
  "alias": false,
  "peer_metadata": null
}
^0.22.0development
rxjs
Environment
{
  "local_name": "rxjs",
  "alias": false,
  "peer_metadata": null
}
^6.5.2development
rxjs
Environment
{
  "local_name": "rxjs",
  "alias": false,
  "peer_metadata": null
}
^6.5.2peer

Distribution files

1 collected
Distribution 1

Source distribution

Source file
Checksums from source
{
  "integrity": "sha512-AI00+2Yt5B/ZcVY0DR5BrCwhWD1SjViFwj8z/JYoFlYBpLULCyndXMEZnFYF6PGiTFqHh6lx7e3I1ijebSoR7w==",
  "shasum": "787fdd0b8984255dc57252ce8c5d2b89ad189c34",
  "tarball": "https://registry.npmjs.org/92green-rxjs/-/92green-rxjs-0.9.0.tgz",
  "fileCount": 17,
  "unpackedSize": 28200,
  "npm-signature": "-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJhC5d0CRA9TVsSAnZWagAAkHcP/R/R4c5LxKSst+J6tz4R\nLYQlz6OrVBiqfhEmpxZyvj0roBli1xxK5diE1GV7EXM+hu3BFFFc3Y+ekCB0\nja7Nhv8EttaLgAZ4CbGDVRo065A54CU3svvR92fl5ErcJ9/s14KOa49EDjv5\nyWIhXqVmUyYRUWhrCdTgcH52SFBA1MmxQHyfOz5mJUCcULMPxnVtco1l3TLu\n9fo2EvgMh1jcZ0UqM3c6s/PbQEiiwqu8TgnLoM9cDQdXMNx8JAvsxJYHuCjM\nIbRMp5GCGHfdYXFQL27lgtm6fi4P6nlRDfDJoOW08gkS2pS2m5U/AmrtYCXU\nEIidgPktOszuRjATG2yttRkIEYV6AfYK4QveR77VMVBbsyGZwPmTmaHp6fZJ\nPwmVprrMKYsLT7DkjD7iq70LTOTU5FM7GIRBuBg54vto7O0I41IeAE6/ravy\naPmpKnLhFRzGYPzlU/jeSoHUlrn7RAULpH5u+CFcAFWYmPbxz/bAifbscLVr\n84Bfog6YJjUZC5QOw+JSma3DHI4udvEGH/yIrTadSD7cIdEZuBRSqtSQiunr\nOAXK+9Oq/e7HFcJMs9/EcBlxL/voN+wCEJep1NXCuKrz9Ev+D3SHkkgSRlc4\n3CUk7xHpCWvOvNlqT7QuZy/tEjKsyIOwDeJHmKlbZjaQw6SFUQmTUaXumfBT\nFhXy\r\n=Akfg\r\n-----END PGP SIGNATURE-----\r\n",
  "signatures": [
    {
      "keyid": "SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA",
      "sig": "MEUCIQCXEjku1NOVfjI1Fp/KfUtDHUdtLFX+R2KcefpGFCBj2QIgbPYSyc56DSlGJut5uepiyFgooknovyIan9gAPwOG7BE="
    }
  ]
}

Repository

PENDING
92green/92green-stream

Shared repository data · GitHub

License detected in repository
Not collected

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 21:31 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package_version:d2f514a7b2393431ed21b8e10690475694cc39885e9d49cfc79ed2fdb92f6084

Other collected versions

0.10.012 Oct0.9.116 Sept0.8.13 Jun0.8.07 Aug0.7.022 MayView all collected versions →