t-moe / FluentFlow

A tool to filter json objects by describing their (timely) behaviour
GNU General Public License v3.0
4 stars 1 forks source link

Make fluentflow rule asynchronous #6

Closed Enteee closed 8 years ago

Enteee commented 8 years ago

For asynchronous operations inside a rule I'd like to have a callback (instead of a return) which tells the matcher if a rule matched or not.

t-moe commented 8 years ago

Ok. Basic async support is implemented. If a matcher function returns nothing (=undefined) the matcher core will wait till the matcher function calls this.next(...) with a boolean argument.

t-moe commented 8 years ago

Example which shows the technique (but makes totally no sense ):

$.match(function(currentObject) {
    var cb = this.next;
    setTimeout(function() {
        cb(currentObject.type == "ForkEvent");
    },100);
}).then(function(){
    console.log("Hey, I had to wait 100ms!");  
});
Enteee commented 8 years ago

Why are you using this.next instead of an optional function argument?

t-moe commented 8 years ago

The number of arguments to the matcher function depends on the position in the chain. E.g. A matcher function, after 2 "followedBy's" can take up to 3 args. Making the callback an optional last function parameter, would force me to drop the "detect if the function needs the previous objects or not"-support. Making the callback the first function parameter is also bad, for functions that don't use it.