Introducing Compute

Compute, a reactive programming library for the browser, and the server

tl;dr:

  • knockout compatible reactive programming library
  • lets you define relationships between observables, and react to observable value changes
  • use this when you don't just want to update your DOM, but leverage observables for your app logic
  • Fully open source: https://github.com/akshat1/compute. MIT licensed.
  • Use with bower: bower install compute --save
  • Use on the server with npm: npm install simian-compute
  • *npm module temporarily called simian-compute because the name compute is in use by an empty package. I've mailed the author if they would like to vacate the package name.

The (slightly) Longer Version

Compute provides an implementation of observable, and observable-array. Compute also lets you define relationships among these observables. Compute is compatible with knockoutjs, to the extent that it will use knockout's observables. Here's some sample code:

var base = Compute.o();
var altitude = Compute.o();
var hypotenuse = Compute.from(base, altitude, function(b, a){
  return Math.sqrt(b*b + a*a);
});
base(3);
altitude(4);
console.log(hypotenuse()); //5
base(5);
altitude(12);
console.log(hypotenuse()); //13

Compute provides two kinds of observables. Observables (Compute.o) and Observable Array (Compute.oa), like knockoutjs. Compute lets you string arbitrary number of such observables together.

var principle = Compute.o(100);
var rate = Compute.o(0.05);
var time = Compute.o(3);
var interest = Compute.from(principle, rate, time, function(p, r, t){
  return p * r * t;
});
interest.$fire(); //since observable subscriptions are only called on mutation, force value function
console.debug(interest);

Compute also allows you to call an arbitrary function without setting the value of an observable.

var aye = Compute.o();
var bee = Compute.o();
var see = Compute.o();
var dee = Compute.o();
Compute.on(aye, bee, see, dee, function(a, b, c, d){
  console.log("One of our observables changed. I can react to it!");
});

Works with Knockout

Finally, if you are using knockout, Compute uses ko.observable (and ko.observableArray) for C.o and C.oa. This lets you use C.o and C.oa in your viewmodel and have them directly bound to your knockout bindings.

Origins:

Compute is inspired by Knockout, Haskell and a proprietary library I use at work to make awesome end-to-end analytics for big-data.