apla / dataflo.ws

workflow processing for javascript
107 stars 21 forks source link

Conditions/Branching #22

Closed behrad closed 11 years ago

behrad commented 11 years ago

How can I check condition or do branching?

katspaugh commented 11 years ago

In a task config, you can use whatever property you like to check whether it's a non-value. We like to use if.

{ "$function": "console.print", "$args": "hello", "if": {$truth.is.out.there}" }

As for the else clause, you can use $empty to signal that the task's completed but returned a non-value:

[
    { "$function": "Math.floor", "$args": 0.1, "$set": "resultTruthy", "$empty": "resultFalsy" },
    { "if": "{$resultTruthy}", "$function": "console.print", "$args": "hello" },
    { "if": "{$resultFalsy}", "$function": "console.print", "$args": "bye-bye" }
] // would print `bye-bye' because 0 is a non-value in dataflo.ws
behrad commented 11 years ago

To use $empty, I tested your sample and it didn't work for me saying:

unsatisfied requirements: console.print => if; console.print => if;

I'm using version 0.0.38

behrad commented 11 years ago

@katspaugh I also check with 0.0.39, and $empty is not working

behrad commented 11 years ago

I just noticed that $empty is working for void values: https://github.com/apla/dataflo.ws/blob/master/workflow.js#L350 I considered it to be working for all falsy values, as defined by common.isEmpty

katspaugh commented 11 years ago

@behrad thanks for digging it up, I didn't know myself.

behrad commented 11 years ago

@katspaugh I've also moved the empty call into task completion handler, where to set empty for all tasks (not only $function type of tasks), Can you merge with my pull request?

katspaugh commented 11 years ago

@apla can you merge it in?