Consequence.js or: How I learned to stop delegating and set the var.
April 1, 2011 12:04 PM   Subscribe

Consequence.js or: How I learned to stop delegating and set the var.
Consequence.js is a small Javascript library for reactive programming:

With Consequence.js, one can set a state clause that, when satisfied at any time, results in the execution of bound imperative handlers, for either the positive (true) case, or the negative (false) case.

This library can be used when a programmer would rather say "whenever some complex state change X happens, do Y, and let me not worry about when. Example domains where this could be useful are games, user interfaces, and, to give a familiar example where this exists presently, in spreadsheets cell formulas.

Currently in alpha, looking for comments from JS programmers who like this programming paradigm.
Role: programmer
posted by hanoixan (2 comments total) 3 users marked this as a favorite

It's an interesting concept... and it's certainly a paradigm I like, although I hadn't heard of it.

... I'm wondering if a lot of people handle this kinda thing through approaches like custom event binding or more traditional patterns.
posted by ph00dz at 6:13 AM on April 2, 2011

Yeah, other ways of doing this are delegates, events, and just plain old monolithic handling in a single loop, checking state as you go. The idea is that instead of having to test the state (in multiple areas possibly), and then delegating/sending an event, the binding and handlers are set once, and in effect until it's unbound. Not so useful if your code trickles events top down through a tree of handlers, but more useful if you have multiple objects that have no natural flow hierarchy (e.g., trigger objects in a game).

It took me a while to find the name of this paradigm, finally via a posting I made on Stackoverflow. Although the Javascript implementation runs the risk of taking a lot of memory, I think a C++ implementation could have a much smaller footprint (at the risk of having to use preprocessor trickery to define handler functions inline to the binding).

Using this, one should be able to program something like Pong in very few lines of code, but I haven't proven that yet. :)
posted by hanoixan at 10:37 AM on April 2, 2011 [1 favorite]

« Older For A New Green Society...   |   Inside the John Lennon Educati... Newer »

You are not currently logged in. Log in or create a new account to post comments.