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.
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.
- Very simple interface (could be done with a single protocol)
- Observables/Observers can be easily coupled-decoupled
- The code isn't really side-effect free. Chains basically end with a dorun block
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.
- 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)
- 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?
A very large library built by Zach Tellman (https://github.com/ztellman/lamina/)
- Can we get Zach to comment here?
This is the well known Racket implementation (http://docs.racket-lang.org/frtime/index.html)
- Very functional
- Will re-interpret lisp functions into event based functions
- Quite a lot of code is required for this
- Doesn't seem to support async functions.
To start with, I suggest we reduce the Reactive Extensions interfaces down to the following
(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.