Giter Site home page Giter Site logo

fusetools-haxe's Introduction

Haxe for FuseTools

FuseTools is a new, promising, tool for crossplatform/mobiles apps development.

Application scripting is done in JavaScript, and one of the remarkable aspects of FuseTools is that it is strongly based on observer pattern and reactive programming. The result of this combination is the Observable object which combines those naturally/magically.

Of course you normally have to rely on your brain alone in JavaScript to leverage this crazy reactive programming approach. Unless you use Haxe to have strong typing!

Attention: proof-of-concept ahead

Haxe?

Haxe is a superb strongly typed language which can fit as well heavy-weight single-page applications but also distributed/reactive code as FuseTools applications.

Here's my attempt to convince myself (and maybe you) that the Haxe compiler and type system can bring a lot of value to Fuse projects: it's fun to play with JavaScript, but how do you control the integrity of your projects when they will have grown past the Poc size?

Observable

The Observable API is really rich and challenging to describe in Haxe, especially the fact that an Observable is not instantiated with the new keyword:

var Observable = require('FuseJS/Observable');

var who = Observable('World');

var hello = Observable(function() {
	return "Hello " + who.value;
});

module.exports = {
	hello: hello
}

// UX can bind `hello` and will update automatically whenever `who`'s value change!!!

Haxe doesn't like that so we have to have a slightly different approach, but it looks quite nice in the end:

import Fuse.observable;

class View {
	var who = observable('World'); // Observable<String>
	public var hello:Observable<String>;
	
	public function new() {
		hello = observable(function() {
			return 'Hello ${who.value}';
		});
		who.value = 42; // Compiler error: Int should be String
	}
}

module.exports = new View();

It may look like syntaxic sugar, but it's way more than that: this is fully strongly typed. Everything is validated by the compiler and guarranteed to be sound before running it.

module.exports

Normally Fuse scripting is "distributed": each view has its little inline script or requires an external JS for the view.

Each view script should expose observables and callbacks through a module.exportsobject (see previous point about Observables).

It is possible however to have shared code between views:

<JavaScript File="app.js" ux:Global="app" />
<JavaScript>
	var app = require('app');
	module.exports = new app.MainView();
</JavaScript> 

app.js would be Haxe generated, and to expose these view scripts we need to write:

class App {
	static function main() {
		untyped module.exports = {
			MainView: MainView
		};
	}
}

TODO: there should be a nice way to automate that

Callbacks

A Fuse UX view can bind actions to script callbacks.

The problem is that for some reason those functions are called out of scope, but there's a macro for that ;)

class MainView implements UXExport {
	var currentTab = observable(Tab.All);
	/*...*/	
	public function showAll() {
		currentTab.value = Tab.All; // this.currentTab
	}
	/*...*/	
}

module.exports = new MainView();

If the showAll function was just exposed as a function of the MainView instance, you'd discover that the scope is lost when the function is invoked.

The solution is to implement UXExport which will run a macro pre-binding all the public functions to the instance so that scope will be correct when the method is called by the view.

fusetools-haxe's People

Contributors

elsassph avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.