Why hxbolts?

  1. Why hxbolts?
  2. Trying promhx
  3. Trying thx.promise
  4. Trying task
  5. Trying continuation
  6. Trying async
  7. Trying hext-flow
  8. Finally, hxbolts

Seriously, there are numerous promise / async libraries for haxe, why we need another one? In series of posts I will try to do the same task using different libraries.

Important note: I’m open to discussions on twitter or in comments here.

At first let’s find these libraries

Library must be cross platform – at least swf, js, cpp and neko. I go to the lib.haxe.org and enter “promise” into the search box:

  1. promhx
  2. thx.promise

Not so much. But we can extend search criteria, because “hxbolts is more about transforming async callback hell into nice looking code”. This time I enter “async”:

  1. task
  2. continuation
  3. async
  4. hext-flow

Much better, 6 libraries at all. I have not included some libraries into the list:

  • stx_async – outdated, version from haxelib is too different from the version from github. Almost no documentation, readme file talks about reactive extensions instead of promises, etc.
  • arrowlets – outdated, version from haxelib is too different from the version from github. No documentation. When I tried to use the version from GitHub, it just doesn’t compile (Type not found : stx.ifs.Value).
  • hxdispatch – outdated, github link is dead, seems to be replaced with hext-flow
  • transition9 – only for node.js

Maybe I’ll take other libraries from the list due to the fact that they do not work, or I couldn’t figure out how to work with them.

Secondly, let’s choose a task

I’ll take the real task that I had to do for one of my games (sorry, currently I can’t show the game because my client has not yet decided when he want to launch it).

The game must show loader, then fetch json response from api, parse it, and (if response doesn’t has an “error” field), update state from it. Also, if response contains “playerAvatarUrl”, fetch bitmap data from that url and update player avatar (same for “opponentAvatarUrl”). If response has an “error” field or exception was thrown, show popup with error message. At the end hide the loader and call onTaskSuccessed() if no errors occurred.

Some synchronous pseudo-code (obliviously, in real worls everything must be async):

function fetchText(url : String) : String {
    // fetch text data from url
}

function fetchJson(url : String) : DynamicExt {
    return cast Json.parse(fetchText(url));
}

function fetchBitmapData(url : String) : BitmapData {
    // fetch image from url
}

function sendApiRequest(method : String) : DynamicExt {
    var result : DynamicExt = fetchJson('http://some.api/${method}');

    if (result["error"] != null) {
        throw result["error"];
    }

    return result;
}

function syncState() : Void {
    var result : DynamicExt = sendApiRequest("sync-state");
    updateStateFromTheResponse(result);

    if (result.exists("playerAvatarUrl")) {
        setPlayerAvatar(fetchBitmapData(result["playerAvatarUrl"]));
    }

    if (result.exists("opponentAvatarUrl")) {
        setOpponentAvatar(result["opponentAvatarUrl"]);
    }
}

function doTheTask() : Void {
    showLoader();

    try {
        syncState();
        hideLoader();
        onTaskSuccessed();
    } else {
        hideLoader();
        showErrorPopup();
    }
}

Don’t pay attention to the function names, they are just to illustrate the idea. Moreover, don’t pay attention to methods like asString(), they are from zame-miscutils library, and not related to the topic.

Notice, fetchJson() internally use fetchText(). Surely, it can be done in one function, but this code is more similar to the game code. Same for sendApiRequest() – the game has an Api class, that compute signature for each request, and checks for “error” field in the response. State is synced in several places of the game, but loader is shown only in one place, so I create separate function syncState() to be closer to real game code.

In all examples I will use URLLoader, Loader and friends to load data and images from the network (I can do it thanks to openfl).

Let’s competition begins! More posts are coming.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.