Skip to content

Instantly share code, notes, and snippets.

@francescoagati
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save francescoagati/5d9b51f3668e08029731 to your computer and use it in GitHub Desktop.
Save francescoagati/5d9b51f3668e08029731 to your computer and use it in GitHub Desktop.
async flow programming with setTimeout and requestAnimationFrame in haxe with continuation and tink_lang short lambda and inline
import js.Browser;
@:build(com.dongxiguo.continuation.Continuation.cpsByMeta(":async"))
@:tink class AsyncFlow {
static inline function sleep(n:Int,next:Void->Void) untyped setTimeout(next,n);
static inline function rq(fn:Void->Void,next:Void->Void) {
Browser.window.requestAnimationFrame(function(b) {
fn();
next();
});
}
@:async public static function flow(){
@await rq([] => {
trace("trace 1");
});
@await sleep(100);
@await rq([] => {
trace("trace 2");
});
@await sleep(100);
@await rq([] => {
trace("trace 3");
});
}
}
class Main {
public static function main() {
AsyncFlow.flow(function() {});
}
}
(function (console) { "use strict";
var AsyncFlow = function() { };
AsyncFlow.flow = function(__return) {
var fn = function() {
console.log("trace 1");
return;
};
var next2 = function() {
setTimeout(function() {
var fn1 = function() {
console.log("trace 2");
return;
};
var next1 = function() {
setTimeout(function() {
var fn2 = function() {
console.log("trace 3");
return;
};
var next = function() {
__return();
};
window.requestAnimationFrame(function(b) {
fn2();
next();
});
},100);
};
window.requestAnimationFrame(function(b1) {
fn1();
next1();
});
},100);
};
window.requestAnimationFrame(function(b2) {
fn();
next2();
});
};
var Main = function() { };
Main.main = function() {
AsyncFlow.flow(function() {
});
};
Main.main();
})(typeof console != "undefined" ? console : {log:function(){}});
@Atry
Copy link

Atry commented May 30, 2015

I think tink_lang is unnecessary, just try this

@await Browser.window.requestAnimationFrame();
trace("trace 1");
@await sleep(100);
@await Browser.window.requestAnimationFrame();
trace("trace 2");
@await sleep(100);
@await Browser.window.requestAnimationFrame();
trace("trace 3");

@francescoagati
Copy link
Author

requestAnimationFrame don't block. i have wrapped it for make it blocking and using await

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment