Giter Site home page Giter Site logo

callflow's Introduction

#callflow: control your asynchronous code with explicit flows

This library purpose a concept of flow based on explicit continuations.

The idea of the library is to represent asynchronous code (or even synchronous but complex code) as a set pf phases in a workflow. The code using asynchron avoids the infamous pyramid of callbacks by structuring code in a list of functions (phases) that also control the transitions between phases using "next" and "continue" primitives. Transitions between phases can get triggered by directly calls to other phases, by asynchronous calls (next) and by a continuations given in place of a asynchronous callback. The flows have also the concept of "join" that are dormant phases that get called only when the whole list of declared phases got executed.

Syntax & primitives: The syntax of a flow is a JSON with values as functions phases and join phases

The flow can be seen as a set of phases and the computation will pass through each one (synchronously and asynchronously) Each phase has a name. Phases can be function phases or join phases. If a phase is a function phase, the field value in the JSON is a function. If a phase is a join phase, the value of the field is an object with a member "join" containing a list with the names of the phases that are waited until the join phase get called (see examples). Two special functions, called flow primitives, are available in all

Flow variables

When a flow is created, a Java Script object is created. This object beside containing as members all the functions of the flow, the "next" and "continue" functions, it can also contain arbitrary variables.

Basic example:

  var flow = require("callflow");
  var f = flow.createFlow("Flow example", {
        begin:function(a1,a2){
            //.. code
            this.variable = false;
            this.step();
        },
        step:function(a){
            //this.variable is set in begin
            //.. code     
                    
        }
    });
    f();

Example with a join and use of continue:

  var f = flow.createFlow("Flow example", {
        begin:function(a1,a2){
            //..
            this.step(true);
            this.next("step", "Comment explaining why was this function called", true); // quite similar with this.step(true) but step wll be executed at nextTick   
            
            asyncFunction(this.continue("callback","Called later by asyncFunction");
        },
        step:function(a){
            //a will be true in both cases
            //..code
            
        }
        callback:function(err,res){
                        //..
            
        }
        end:{
            join:"step,callback", //waits 2 calls of step and one of callback
            code:function(){
            //..called     
        }            
    });
    var flow = f();
    f.status(); // see the flow status

Integration with the "whys" module (https://github.com/salboaie/whys)

From the beginning, we created flows with the idea to have automated integration with the "whys" module. Each phase transitions is automatically logged with a "why" call explaining the transition.
Beside automated integration, why calls can be performed at will anywhere and the why system will compact the tracking logs for each call. "next" and "continue" functions have the second argument an string that is automatically passed to the why.

  var flow = require("callflow");
       var f = flow.createFlow("Flow example", {
             begin:function(a1,a2){
                 //.. code
                 this.step.why("explanantions...")();                     
             }.why("Additional info"),
             step:function(a){
                 //.. code                
             }.why("Additional info")
         });
         f.why("Additional info")();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.