//.............................................................................. // // This file is part of the Jancy toolkit. // // Jancy is distributed under the MIT license. // For details see accompanying license.txt file, // the public copy of which is also available at: // http://tibbo.com/downloads/archive/jancy/license.txt // //.............................................................................. // Reactive programming is a programming paradigm offering tremendous help // for many programming tasks and is especially effective for UI programming. // The idea behind the reactive programming is to automate the execution of // certain update actions in response to property changes. Imagine a complex // configuration dialog with many interconnected items: a value of one item // may affect the color, visibility, and availability of some other item. // The traditional approach to creating such a dialog is to write multiple // event handlers responsible for updating dependent items. // Jancy makes it possible to write a single 'reactor' block where all the // relations and dependencies are described in a declarative style. After the // reactor start the Jancy runtime will automatically bind to, track property // changes, and execute required updates. //.............................................................................. // update functions that need to be called when bindable properties change updatePurple ( int red, int blue, int alpha ) { printf ("updatePurple (%x, %x, %x)\n", red, blue, alpha); } updateYellow ( int red, int green, int alpha ) { printf ("updateYellow (%x, %x, %x)\n", red, green, alpha); } updateCyan ( int green, int blue, int alpha ) { printf ("updateCyan (%x, %x, %x)\n", green, blue, alpha); } //.............................................................................. // 3 bindable data properties int bindable g_red; int bindable g_green; int bindable g_blue; // this variable will be automatically updated by g_colorReactor int g_rgb; // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . // inside the reactor the developer writes expressions describing relations and // constraints; this is done in the declarative manner // upon starting the reactor the Jancy runtime binds to all bindable non-lvalue // properties referenced in the expressions inside the reactor and // automatically re-evaluates all dependent expressions // if the required logic cannot be expressed by a simple expression, the // 'onevent' statement can be used (see the next sample) reactor g_colorReactor (int alpha) { updatePurple (g_red, g_blue, alpha); updateYellow (g_red, g_green, alpha); updateCyan (g_green, g_blue, alpha); g_rgb = (g_red << 16) + (g_green << 8) + g_blue; } //.............................................................................. // entry point int main () { printf ("main ()\n"); // starting the reactor executes all expressions in this reactor block one // time g_colorReactor.start (5); printf ("changing g_red...\n"); g_red = 0xab; printf ("changing g_green...\n"); g_green = 0xcd; // if the reactor is stopped now then the change of 'g_blue' below will not // be handled // g_colorReactor.stop (); printf ("changing g_blue...\n"); g_blue = 0xef; printf ("g_rgb = #%06x\n", g_rgb); return 0; } //..............................................................................