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

@ambientlight/bs-rx

v0.2.3

Published

bucklescript bindings for RxJs v7

Downloads

58

Readme

STATUS VERSION LICENSE ISSUES

bs-rx

Bucklescript bindings for rxjs v7(alpha)
Most functionality is available, while ajax / websocket apis are not yet done. Refer to documentation for existing coverage.

Installation and Usage

npm install @ambientlight/bs-rx reason-promise bs-fetch

reason-promise and bs-fetch are peer dependencies

Then add @ambientlight/bs-rx (and reason-promise, bs-fetch) into bs-dependencies in your project bsconfig.json.

Rx.range(~start=1, ~count=200, ())
|> Rx.Operators.filter((x, _idx) => x mod 2 == 1)
|> Rx.Operators.map((x, _idx) => x + x)
|> Rx.Observable.subscribe(
  ~next=x=>Js.log(x)
)

You may also utilize -n(no idx) suffixed versions of those operators that errase idx argument:

Rx.range(~start=1, ~count=200, ())
|> Rx.Operators.filtern(x => x mod 2 == 1)
|> Rx.Operators.mapn(x => x + x)
|> Rx.Observable.subscribe(
  ~next=x=>Js.log(x)
);

Examples

Map and flatten each letter to an Observable ticking every 1 second

Rx.of_([|"a", "b", "c"|])
|> Rx.Operators.mergeMap(`Observable((x, _idx) => 
  Rx.interval(~period=1000)
  |> Rx.Operators.map((i, _idx) => string_of_int(i) ++ x)))
|> Rx.Observable.subscribe(~next=x=>Js.log(x));

Custom operator

Create an observable that never completes and repeats when browser is back online.

let repeatWhenOnline = source => 
  source
  |> Rx.Operators.takeUntil(Rx.fromEvent(~target=Webapi.Dom.window, ~eventName="offline"))
  |> Rx.Operators.repeatWhen(_notifier => Rx.fromEvent(~target=Webapi.Dom.window, ~eventName="online"));

let obs = Rx.of1("I'm online")
|> repeatWhenOnline
|> Rx.Observable.subscribe(~next=x=>Js.log(x));

Fetch

Rx.Fetch.fromFetch(`String("https://api.github.com/users?per_page=5"), ())
|> Rx.Operators.mergeMap(`Promise((response, _idx) => response |> Fetch.Response.json))
|> Rx.Observable.subscribe(
  ~next=value => Js.log(value),
  ~error=error=>Js.log(error),
  ~complete=() => Js.log("done")
);

Also, have a look at OperatorTests for more usage examples.

Promises

All bindings accepting promises come in two flavours: as Js.Promise.t (shipped with bucklescript) and Promise.t from reason-promise. For the later, use `Repromise variant constructor.

[@bs.module "rxjs/operators"]
external mergeMap: (
  [@bs.unwrap] [
    | `Observable(('a, int) => t('b))
    | `Promise(('a, int) => Js.Promise.t('b))
    | `Repromise(('a, int) => Promise.t('b))
    | `Array(('a, int) => array('b))
  ],
  ~concurrent: int=?
) => operator('a, 'b) = "mergeMap";

Testing

You may find marble testing handy to test your rxjs logic. Marble string syntax allows you to specify rxjs events(such as emissions, subscription points) over virtual time that progresses by frames(denoted by -). You can use it to express the expected behavior of your observable sequences as strings and compare them with Rx.Observable.t('a) instances you are testing. You need to initialize TestScheduler.t with a function that can perform deep comparison (such as BsMocha.Assert.deep_equal), then put your marble tests inside ts |> TestScheduler.run(_r => ...). Asynchronous operators usually take ~scheduler parameter, pass TestScheduler.t instance to them. The next example illustrates it, also you may want to refer to rxjs marble diagrams documentation.

open Jest;
open Rx.Testing;
open TestScheduler;

//...

test("timeInterval: should record the time interval between source elements", () => {
  let ts = TestScheduler.create(~assertDeepEqual=BsMocha.Assert.deep_equal);
  ts |> run(_r => {
    // subscribe in 6th frame, 4 emissions: b, c, d, e
    let e1 = ts |> hot("--a--^b-c-----d--e--|");
    let e1subs =    [|"^--------------!"|];
    let expected =    "-w-x-----y--z--|";
    // expected values in w, x, y, z emissions
    let values = { "w": 1, "x": 2, "y": 6, "z": 3 };

    let result = e1
    |> HotObservable.asObservable
    |> Rx.Operators.timeInterval(~scheduler=ts|.TestScheduler.asScheduler)
    |> Rx.Operators.map((x, _idx) => x |. Rx.TimeInterval.intervalGet);

    ts |> expectObservable(result) |> toBeObservable(expected, ~values);
    ts |> expectSubscriptions(e1 |> HotObservable.subscriptions) |> toBeSubscriptions(e1subs);
    Expect.expect(true) 
  })
  |> Expect.toBe(true)
});

Contributing

Any contribution is greatly appreciated. Feel free to reach out in issues for any questions or problem you ran into. Implementational inheritance is used to model inheritance used in rxjs, you may want to refer to Implementation Inheritance.

git clone https://github.com/ambientlight/bs-rx.git
cd bs-rx
npm install
npm run build
npm run test

You can also generate docs via bsdoc. If you have forked this repo, the pushes to master should spin the github actions workflow that rebuild the github pages docs with workflow available at deploy_docs.yml. (You will need to set GH_PAGES_TOKEN for github pages deployment to work).

If you want to generate docs in local make sure you have bsdoc built against ocaml version matching the ocaml version used in your bs-platform (4.02.3+buckle-master for [email protected]).

opam switch 4.02.3+buckle-master

For osx, you can use the npm installation of bsdoc(corresponds to bs-platform 6), but for linux-based distros, you would need to build bsdoc from source for now.

See Also

reductive-observable: Centalized rx side-effects for reductive.