Coder Social home page Coder Social logo

lilactown / bs-most Goto Github PK

View Code? Open in Web Editor NEW
41.0 2.0 9.0 125 KB

Reason/BuckleScript bindings for the Most.js reactive toolkit

OCaml 100.00%
reasonml reactive reactive-programming streams async async-programming bucklescript-bindings bucklescript

bs-most's Introduction

bs-most

BuckleScript bindings for Most.js.

In Reason:

let add = (x, y) => x + y;

let isEven = x => x mod 2 === 0;

Most.(
  fromList([0, 1, 2, 3, 4, 5, 6, 7, 8])
  |> map(add(2))
  |> filter(isEven)
  |> observe(x => Js.log(x))
  |> Js.Promise.then_((_) => {
       Js.log("Stream completed");
       Js.Promise.resolve();
     })
);


/**
 * Logs:
 * 2
 * 4
 * 6
 * 8
 * 10
 * Stream completed
 **/

An example of implementing the Redux architecture in a ReasonReact project can be seen here.

How do I install it?

Inside of a BuckleScript project:

npm install --save bs-most

Then add bs-most to your bs-dependencies in bsconfig.json:

{
  ...
  "bs-dependencies": [
    "bs-most"
  ]
}

How do I use it?

The Most.js API docs are probably the best source of information about each method. This library attempts to provide very low-level bindings, except for what's noted in the What's different section.

The bindings are a single file in src/most.re. I try to provide comments next to each binding and am working on flushing out tests so that users can refer to them.

I am also available by e-mail or DM on the Reason discord (username: @lilactown).

What's missing

  1. generate - Reason doesn't have generator support yet
  2. transduce - Awkward to use in Reason/OCaml without a solid transducer impl (maybe coming soon?? ๐Ÿ˜‰)
  3. combineArray - the callback for this is variadic (depending on how many streams you have in the array), not sure how to type it yet ๐Ÿ˜ž

What's different

  1. Predicates that normally return "truthy" values, now only accept returning booleans
  2. sample is variadic so we offer sample1, sample2, sample3... sample6
  3. fromEvent - DOM events are supported, but generic EventEmitter's are not yet
  4. from only works on arrays right now; generic iterators must be coerced
  5. fromList is a function that creates a stream from a Reason List type, e.g. fromList([1, 2, 3])
  6. unfold is modeled a bit differently - instead of taking in a JS object { done, value, seed }, it takes an option (value, seed). Example:
/* emits 2,4,6 then completes */
Most.unfold(
  (seed) =>
    if (seed < 4) {
      let nextSeed = seed + 1;
      Some((seed * 2, nextSeed));
    } else {
      None;
    },
  1
);

Subjects

This library also includes some bindings for Subjects. Subjects are very useful for e.g. implementing a state store/dispatch system. The API is a little bit different from the raw most-subject library:

  • Subject.make: creates an asynchronous Subject
  • Subject.asStream: casts a subject to a stream, to be used by the rest of the Most module
  • Subject.next: emits a value on a subject
  • Subject.error: emits an error on a subject
  • Subject.complete: completes a subject with no value
  • Subject.completeWith: completes a subject with a value

Build

npm run build

Build + Watch

npm run watch

Testing

npm run test

bs-most's People

Contributors

dboris avatar lilactown avatar mrmurphy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

bs-most's Issues

Issue installing and building in project

After installation I attempted to build my project and received the following exception

bsb -make-world

[14/14] Building src/ReactDOMServerRe.mlast.d
[7/7] Building src/ReactDOMRe.cmj
Warning: refmt version missing. Please set it explicitly, since we may change the default in the future.
[1/2] Building src/most.mlast
FAILED: src/most.mlast
/usr/local/lib/node_modules/bs-platform/lib/bsc.exe -pp "/usr/local/lib/node_modules/bs-platform/lib/refmt.exe --print binary" -w -30-40+6+7+27+32..39+44+45+101 -nostdlib -I '/Users/michael/Projects/ReasonML/my-project/node_modules/bs-platform/lib/ocaml' -bs-super-errors -no-alias-deps -color always -c -o src/most.mlast -bs-syntax-only -bs-binary-ast -impl /Users/michael/Projects/ReasonML/my-project/node_modules/bs-most/src/most.re
File "/Users/michael/Projects/ReasonML/my-project/node_modules/bs-most/src/most.re", line 1, characters 12-13:
Error: 1274: syntax error, consider adding a `;' before

We've found a bug for you!
/Users/michael/Projects/ReasonML/my-project/node_modules/bs-most/src/most.re

There's been an error running Reason's refmt parser on a file.
This was the command:

/usr/local/lib/node_modules/bs-platform/lib/refmt.exe --print binary '/Users/michael/Projects/ReasonML/my-project/node_modules/bs-most/src/most.re' > /var/folders/hy/kvvyvvc10vsd8k1bn_dn1htr0000gn/T/ocamlpp73a5a9

node 8.9.1
npm 5.5.1
bsb 2.2.3
bs-most ^0.1.1
react ^16.2.0
react-dom ^16.2.0
reason-react >=0.3.4

Edit:
When I clone and build manually then link it seems to work

Support for generic event emitters

I have a library that is returning event emitters from various functions. I need to be able to convert those to streams in order to pass them around in my app. I'd be happy to contribute this, but thought I'd start an issue in case anyone has advice on how best to accomplish this.

Update release package

Current release package does not match the contents of this repository.

For example, this line has the Js.boolean type when I download the current release package, while on this repository that line has the bool type.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.