Error formatting macro: pagetree: java.lang.NullPointerException
Skip to end of metadata
Go to start of metadata
You are viewing an old version of this page. View the current version. Compare with Current  |   View Page History

This document is a discussion on the best path for adding reactive programming functionality to ClojureScript. Although this is primarily focused around UI updates/bindings, it should be generic enough to support something like the following:

 

Textbox -> On Key Press -> Throttle to 1 per sec -> Make Server request for data -> Format response into DOM elements -> Update UI

 

Async binding chains are required to allow for throttling, server calls, and for maintaining general UI responsiveness.

 

 

Related Work:

Reactive Extensions for .NET (http://msdn.microsoft.com/en-us/data/gg577609.aspx):

This is a well known and very powerful technology developed by MS.

Pros:

  • Very simple interface (could be done with a single protocol)
  • Observables/Observers can be easily coupled-decoupled

Cons:

  • The code isn't really side-effect free. Chains basically end with a dorun block

 

TPL Dataflow:

A slightly different library also developed by MS: (http://msdn.microsoft.com/en-us/devlabs/gg585582.aspx)

Unlike Rx, TPL Dataflow is expressed as blocks with input queues and output queues. If messages are not removed from these queues the messages will build up. This is by design as TPL Dataflow is more about processing data via Dataflow graphs and less about event based UI programming.

Pros:

  • Also a fairly simple to implement (just a few interfaces needed)
  • The "code blocks" could be very functional. Each block could accept a list of queues and return a map of data to queue into the outputs
  • Fairly may uses cases
  • Could possibly work well with web workers? (https://developer.mozilla.org/en/Using_web_workers)

 

Cons:

  • If we were building a dataflow off of mouse events, the queue could get full very quickly.
    • Perhaps a throttle block would be all that's needed here?

 

Lamina:

A very large library built by Zach Tellman  (https://github.com/ztellman/lamina/)

Pros:

  • Can we get Zach to comment here?

 

FrTime:

This is the well known Racket implementation (http://docs.racket-lang.org/frtime/index.html)

Pros:

  • Very functional
  • Will re-interpret lisp functions into event based functions

Cons:

  • Quite a lot of code is required for this
  • Doesn't seem to support async functions.

 

 

Suggested Implementation:

 

To start with, I suggest we reduce the Reactive Extensions interfaces down to the following

 

(defprotocol IObservable

    (subscribe [this onnext onerror oncomplete]))

 

To subscribe, a user would need to supply functions that will be called when a new event arrives, when an error has occurred, and when the stream has completed. In return the subscribe method will return a single function that the user can call to detach from the IObservable.

This interface could be further simplified by combining the input functions into a single function that would accept either a event or one of ::error or ::complete messages. However, in my attempts with this, I've discovered that this simply makes the subscriber code more complex as a single function must now handle three use cases.

 

Several functions could then be created that operate on this interface. For instance, rmap (reactive map) would apply a function to each event sent through onnext.

 

 

Labels: