Thanks to visit codestin.com
Credit goes to lib.haxe.org

ludi-compose

Simple micro-application composition
https://github.com/c-g-dev/ludi-compose

To install, run:

haxelib install ludi-compose 0.0.1 

See using Haxelib in Haxelib documentation for more information.

README.md

ludi-compose

Simple micro-application composition for Haxe.

ludi-compose provides a lightweight way to compose applications by wiring together independent components.

This is mostly a showcase of a design pattern concept. The underlying logic is very simple.


    class SomeWidget {

        public function new() {

            /*

            SomeWidget is the identity/facade class for an entire UI widget.

            Even if we offload complexity to individual components (ala MVC or something), this class is still orchestrating those components.

            Ideally this class is completely focused on its external API  contract and shouldn't get gummed up by components.

            While we could make a seperate class to handle the component management, that approach teeters on overengineering, especially for small-mid projects.

            Below is a pattern for concisely and extendibly dropping self-managed, connected components into the facade without making it take ownership of them.
            */

             var app = Composition.create({
                data: new DataState(),
                button: new Button(),
                controller: new Controller()
            });

            app.load();

        }
       

    }

Unfortunately there is no completion for field access like app.button (they are typed as Dynamic, see Composition.fieldAccess). Originally the idea was to tag a Composition with a type, like new Composition<MyClass>({...}) and then you could get the completion via @:genericBuild access to Composition<MyClass> in other files. But the macro "build order" limitation makes this infeasible. Hopefully I can find a clever way to circumvent this later.

Installation

haxelib install ludi-compose

Example

import ludi.compose.Composition;
import ludi.compose.IComposible;
import ludi.compose.CompositionEvent;

// 1. A simple data component
class DataState {
    public var count:Int = 0;
    public function new() {}
}

// 2. A view component
class Button {
    public var onClick:Void->Void;
    public function new() {}
    public function click() {
        if(onClick != null) onClick();
    }
}

// 3. A controller that wires them together
class Controller implements IComposible {
    public function new() {}

    public function onCompositionEvent(e:CompositionEvent, comp:Composition):Void {
        switch(e) {
            case Setup:
                var data:DataState = comp.data;
                var btn:Button = comp.button;

                btn.onClick = () -> {
                    data.count++;
                    trace("Count: " + data.count);
                };
            default:
        }
    }
}

class Main {
    static function main() {
        // Define the composition. Order matters, as it defines order in which the components consume events.
        var app = Composition.create({
            data: new DataState(), //components don't need to implement IComposible. If they don't, they're just normal fields.
            button: new Button(),
            controller: new Controller() //IComposible components get lifecycle/event percolation
        });

        // Initialize
        app.load();

        // Use the application
        app.button.click(); // Traces: Count: 1
    }
}
Contributors
cgdev
Version
0.0.1
Published
1 week ago
License
MIT

All libraries are free

Every month, more than a thousand developers use Haxelib to find, share, and reuse code — and assemble it in powerful new ways. Enjoy Haxe; It is great!

Explore Haxe

Haxe Manual

Haxe Code Cookbook

Haxe API documentation

You can try Haxe in the browser! try.haxe.org

Join us on GitHub!

Haxe is being developed on GitHub. Feel free to contribute or report issues to our projects.

Haxe on GitHub