Quantcast
Channel: Hacker News 50
Viewing all articles
Browse latest Browse all 9433

fynyky/reactor.js · GitHub

$
0
0

Comments:"fynyky/reactor.js · GitHub"

URL:https://github.com/fynyky/reactor.js


Reactor.js

Reactor is a lightweight library for reactive programming. It provides reactive variables which automatically update themselves when the things they depend on change. This is similar to how a spreadsheet works, where cells can automatically change their own values in reaction to changes in other cells.

Here's a quick example of what Reactor does:

varfoo=Signal(1);varbar=Signal(function(){returnfoo()+1;});foo();// 1bar();// 2foo(6);foo();// 6bar();// 7

You declare how a variable should be calculated once, and it automatically recalculates itself when necessary. This makes it easy to keep a complex data model consistent, and a user interface up to date when a model is changed.

Reactor is designed to be unobtrusive and unopinionated.

  • There is no need to manually declare listeners or bindings. Reactor automatically keeps track of all that for you.
  • It imposes no particular structure on your code. Any variable can be easily replaced with a reactive one.
  • There is no need to learn special special syntax or use special classes, objects, or methods.

Overview

Reactor has just 2 components: signals and observers

  • Signals are values that are expected to change over time. Signals can depend on each other.
  • Observers are functions which are triggered on signal changes.

A signal depends on another signal when it requires the other signal's value to determine its own. Similarly, an observer depends on a signal when it requires that signal's value to determine what to do.

Whenever a signal is updated, it automatically updates all its dependent signals & observers as well. Together, signals and observers form a graph representing the propagation of data throughout the application. Signals sit on the inside of the graph representing the internal data model, while observers are on the edges of the graph affecting external systems.

Here is an example of simple note list application using Reactor. A signal is used to represent user data while an observer is used to update the html.

// The model is just an array of strings wrapped in a SignalnoteList=Signal(["sample note","another sample note"]);// The code to display the notes is wrapped in an Observer// This code is automatically retriggered when the noteList is modifiedObserver(function(){varnoteListElement=document.getElementById("noteList");noteListElement.innerHTML='';// "noteList().length" causes a read of noteList// This automatically sets noteList as a dependency of this Observer// When noteList is updated it automatically retriggers the whole code blockfor(vari=0;i<noteList().length;i++){varnoteElement=document.createElement("div");noteElement.textContent=noteList()[i];noteListElement.appendChild(noteElement);}});// The input only needs to modify the Signal// The UI update is automatically handled by the ObservervarnoteInputElement=document.getElementById("noteInput");noteInputElement.onkeydown=function(event){if(event.keyCode===13){noteList.push(noteInputElement.value);noteInputElement.value='';}};

Reactor adds very little overhead. You use Signal and Observer to wrap the appropriate variables and code blocks, swap reads and assignments for function calls, and you're good to go!

Comparison to other libraries

Reactor is based on the same reactive principles as Bacon.js and Knockout.js. The main difference is that Reactor is trying to be lightweight and keep the additional syntax and complexity to a minimum. Reactor sets dependencies for you automatically so there is no need to manually set subscriptions/listeners.

Compared to Knockout, Reactor does not provide semantic bindings directly to HTML. Instead, users set the appropriate HTML modifying functions as Observers.

Compared to Bacon, Reactor does not help to handle event streams.

Signals

Signal are just values which other signals can depend on.

Reactor provides a global function called Signal. It wraps a given value and returns it as a signal object.

Signal objects are implemented as functions. To read the value of a signal, call it without any arguments.

To change the value of a signal, pass it the new value as an argument.

foo(9);// sets the signal's value to 9

Signals can take on any value: numbers, strings, booleans, and even arrays and objects.

foo(2.39232);foo("cheese cakes");foo(true);foo(["x","y","z"]);foo({"moo":bar});

However, if a signal is given a function, it takes the return value of the function as its value instead of the function itself.

varfoo=Signal(function(){return2*3;});foo();// returns 6 instead of the function

Signals can have their value depend on other signals by using functions. If a different signal is read from within the given function, then that signal will automatically be set as a dependency. This means that when the dependency has been updated, the value of the dependent signal will be updated as well.

varfoo=Signal(7);varbar=Signal(function(){returnfoo()*foo();// since foo is read here, // is is registered as a dependency of bar});foo();// returns 7 as expectedbar();// returns 49 since it is defined as foo() * foo()foo(10);// this updates the value of foo// and the value of bar as wellbar();// returns 100 since it was automatically updated together with foo

Notice that there is no need to declare any listeners or bindings. Reactor automatically finds these dependencies in a signal's function definition.

This automatic updating allows signals to be linked together to form more complex dependency graphs.

varfirstName=Signal("Gob");varlastName=Signal("Bluth");// fullName depends on both firstName and lastNamevarfullName=Signal(function(){returnfirstName()+" "+lastName();});// barbarianName depends only on firstNamevarbarbarianName=Signal(function(){returnfirstName()+" the Chicken"});// comicTitle depends on barbrianName and fullName and therefore// indirectly depending on firstName and lastNamevarcomicTitle=Signal(function(){return"He who was once "+fullName()+" is now "+barbarianName();});firstName();// "Gob"lastName();// "Bluth"fullName();// "Gob Bluth"barbarianName();// "Gob the Chicken"comicTitle();// "He who was once Gob Bluth is now Gob the Chicken"firstName("Michael");// updating firstname automatically updates // fullName, barbarianName, and comicTitlefirstName();// "Michael"lastName();// "Bluth"fullName();// "Michael Bluth"barbarianName();// "Michael the Chicken"comicTitle();// "He who was once Michael Bluth is now Michael the Chicken"

As far as possible, signals should only read and avoid having any external effects. This refers to actions such as:

  • Modifying the HTML
  • Writing to disk
  • Logging an interaction
  • Triggering an alert

In a complex graph, a changed valued might cascade and cause some dependent signals' definitions to be invoked multiple times before propagation is complete.

In the example above, updating firstName first causes both fullName and barbarianName to update. This causes comicTitle to be updated twice. Once when fullName is updated, and again when barbarianName is updated. This is fine on its own since there are no external effects.

However, if the definition of comicTitle included writing to disk then it would cause problems. Because comicTitle is updated twice, it would initiate 2 different writes. Furthermore, the first write would be incorrect as the change propagation would not have completed yet.

For external effects, it is recommended that observers are used instead.

Observers

Observers are almost identical to signals except for 3 main differences:

  • They are triggered only after all signals have been updated
  • They are only triggered once per signal update
  • Unlike signals, observers cannot be depended upon

Observers are used for external effects while signals are used for internal state. Signal functions might trigger multiple times before all signals have finished updating. If signals are used for external effects, they could triggered incorrectly and redundantly. Observers are triggered last and only once per update and therefore do not have this problem.

Observers are created in the same way as signals.

varfoo=Signal("random string");varbar=Observer(function(){// Triggers first on creationalert(foo());// Reads from foo and alerts "random string"});// Is automatically registered as a dependent// and triggers again whenever foo is changed

Just like signals, their dependencies are automatically calculated and triggered when the appropriate signal is updated.

foo("a new random string");// triggers bar which// alerts "a new random string"

Just like signals, their functions can be updated.

// change bar update the html instead of alerting// triggers once immediately after updatingbar(function(){fooElement=document.getElementById("foo");fooElement.textContent=foo();});foo("this string will be logged now");// triggers bar which now// logs the string instead

To disable an observer, pass in a null value.

bar(null);// disables the observer 

Working with Arrays and Objects

When updating Arrays and Objects, you should use Reactor's convenience methods instead of updating the objects directly. This means you should use:

  • foo.set(key, value) instead of foo()[key] = value
  • foo.pop() instead of foo().pop()
  • foo.push(value) instead of foo().push(value)
  • foo.reverse() instead of foo().reverse()
  • foo.shift() instead of foo().shift()
  • foo.unshift(value) instead of foo().unshift(value)
  • foo.sort(comparison) instead of foo().sort(comparison)
  • foo.splice(start, length) instead of foo().splice(start, length)

The reason for this is if a signal has an array as its value, directly updating the array will not update the signal's dependants. Because the signal object is still representing the same array, it does not detect the change. Instead, using the provided convenience methods does the same update but allows the change to be detected. This applies to objects as well.

// foo initialized as a signal with an array as its valuevarfoo=Signal(["a","b","c"]);// bar initialized as a signal whos value depends on foovarbar=Signal(function(){returnfoo().join("-");});foo();// ["a","b","c"]bar();// "a-b-c"// Updating foo's array directly does not trigger an update of barfoo().push("d");foo();// ["a","b","c","d"]bar();// "a-b-c"// Instead, updating using the convenience method does trigger the update of barfoo.push("e");foo();// ["a","b","c","d","e"]bar();// "a-b-c-d-e"

Summary

varstringSignal=Signal("a string");// Signals can be set to any valuevarbooleanSignal=Signal(true);varnumberSignal=Signal(1);vardependentSignal=Signal(function(){// If given a function, the signal's value // is the return value of the function // instead of the function itselfreturnnumberSignal()+1;// Reading from another signal automatically sets it// as a dependency});varstringSignal("a new string value");// To update a signal just pass it a new value// this automatically updates all // its dependents as wellvararraySignal=Signal([// Signals can even be arrays or objectsstringSignal,// which contain other signalsbooleanSignal,numberSignal]);varalertObserver=Observer(function(){// Observers are just like signals except:alert(arraySignal().join(","));// They are updated last});// They are only updated once per propagation// They cannot be depended on by signalsarraySignal.set(4,"a new value!")// Convenience method for setting properties // on an Array or Object SignalarraySignal.push("foo");// Convenience methods for updating an array SignalarraySignal.pop();arraySignal.unshift("bar");arraySignal.shift();arraySignal.reverse();arraySignal.sort();arraySignal.splice(1,2,"not a signal");

And if you like Coffeescript, Reactor gets even simpler!

stringSignal = Signal"a string"# Signals can be set to any valuebooleanSignal = SignaltruenumberSignal = Signal1dependentSignal = Signal->numberSignal()+1# If given a function, the signal's value# is the return value of the function # instead of the function itself# Reading from another signal automatically # sets it as a dependencystringSignal"a new string value"# To update a signal just pass it a new value# this automatically updates all # its dependents as wellarraySignal = Signal[# Signals can even be arrays or objectsstringSignal# which contain other signalsbooleanSignalnumberSignal]alertObserver = Observer-># Observers are just like signals except:alertarraySignal().join(",")# They are updated last# They are only updated once per propagation# They cannot be depended on by signalsarraySignal.set4,"a new value!"# Convenience method for setting properties # on an object SignalarraySignal.push"foo"# Convenience methods for updating an array Signal arraySignal.pop()arraySignal.unshift("bar")arraySignal.shift()arraySignal.reverse()arraySignal.sort()arraySignal.splice1,2,"not a signal"

Installation and use

Download reactor.js and include it in your application.

Reactor has just 2 components: Signal and Observer

  • For browsers, they will be bound to window as global objects for use anywhere.
  • For node.js, they will be bound as properties of the exports object to be imported as modules

For Coffeescript users, you can instead use reactor.coffee for your coffeescript workflow.

For node.js users, Reactor.js is also available on npm by running

$ npm install reactorjs

And importing it into your application by adding

Reactor=require("reactorjs");Signal=Reactor.Signal;Observer=Reactor.Observer;

For the lucky people using both coffeescript and node.js: you can just use

{Signal,Observer}=require"reactorjs"

Viewing all articles
Browse latest Browse all 9433

Trending Articles