Last active
August 29, 2015 14:21
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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(){}}); |
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
I think
tink_lang
is unnecessary, just try this