Most.js is a toolkit for reactive programming. It helps you compose asynchronous operations on streams of values and events, e.g. WebSocket messages, DOM events, etc, and on time-varying values, e.g. the "current value" of an <input>, without many of the hazards of side effects and mutable shared state.
It features an ultra-high performance, low overhead architecture, APIs for easily creating event streams from existing sources, like DOM events, and a small but powerful set of operations for merging, filtering, transforming, and reducing event streams and time-varying values.
Here's a simple program that displays the result of adding two inputs. The result is reactive and updates whenever either input changes.
First, the HTML fragment for the inputs and a place to display the live result:
Using most.js to make it reactive:
var most = require'most';var xInput = documentquerySelector'input.x';var yInput = documentquerySelector'input.y';var resultNode = documentquerySelector'.result';// x represents the current value of xInputvar x = mostfromEvent'input' xInputmaptoNumber;// y represents the current value of yInputvar y = mostfromEvent'input' yInputmaptoNumber;// result is the live current value of adding x and yvar result = mostcombineadd x y;// Observe the result value by rendering it to the resultNoderesultobserverenderResult;;return x + y;return Numberetargetvalue;resultNodetextContent = result;
As a module:
npm install --save most
var most = require'most';
bower install --save most`
Reactive programming is an important concept that provides a lot of advantages: it naturally handles asynchrony and provides a model for dealing with complex data and time flow while also lessening the need to resort to shared mutable state. It has many applications: interactive UIs and animation, client-server communication, robotics, IoT, sensor networks, etc.
A primary focus of most.js is performance. The perf test results indicate that it is achieving its goals in this area. Our hope is that by publishing those numbers, and showing what is possible, other libs will improve as well.
Most.js is highly modularized. It's internal Stream/Source/Sink architecture and APIs are simple, concise, and well defined. Combinators are implemented entirely in terms of that API, and don't need to use any private details. This makes it easy to implement new combinators externally (ie in contrib repos, for example) while also guaranteeing they can still be high performance.
Aside from making combinators less "obviously correct", complexity can also lead to performace and maintainability issues. We felt a simple implementation would lead to a more stable and performant lib overall.
Most.js integrates with language features, such as promises, iterators, generators, and asynchronous generators.
Promises are a natural compliment to asynchronous reactive streams. The relationship between synchronous "sequence" and "value" is clear, and the asynchronous analogue needs to be clear, too. By taking the notion of a sequence and a value and lifting them into the asynchronous world, it seems clear that reducing an asynchronous sequence should produce a promise. Hence, most.js uses promises when a single value is the natural synchronous analogue.
Most.js interoperates seamlessly with ES6 and Promises/A+ promises. For example, reducing a stream returns a promise for the final result:
// After 4 seconds, logs 10mostfrom1 2 3 4delay1000reducereturn result + y;0thenconsole.logresult;;
You can also create a stream from a promise:
// Logs "hello"mostfromPromisePromiseresolve'hello'observeconsole.logmessage;;
Conceptually, generators allow you to write a function that acts like an iterable sequence. Generators support the standard ES6 Iterator interface, so they can be iterated over using ES6 standard
for of or the iterator's
Most.js interoperates with ES6 generators and iterators. For example, you can create an event stream from any ES6 iterable:
let i=0;whiletrueyield i++;// Log the first 100 integersmostfromallTheIntegerstake100observex => console.logx;
You can also create an event stream from an asynchronous generator, a generator that yields promises:
let i=0;whiletrueyield delayPromiseinterval i++;return resolve => setTimeout => resolvevalue ms;// Log the first 100 integers, at 1 second intervalsmostgenerateallTheIntegers 1000take100observex => console.logx;