/*
 * Avocore : actions.js
 * copyright (c) 2015 Susisu
 */

"use strict";

function end_module() {
    module.exports = Object.freeze({
        "Action": Action,
        "pure"  : pure,
        "bind"  : bind,
        "then"  : then,
        "echo"  : echo,
        "cron"  : crontab
    });
}

var events  = require("events"),
    cron    = require("cron");

var EventEmitter = events.EventEmitter;
var CronJob      = cron.CronJob;

function Action(func) {
    Object.defineProperties(this, {
        "__Action__func__": {
            "value": func
        }
    });
}

Action.prototype = Object.create(Object.prototype, {
    "constructor": {
        "writable"    : true,
        "configurable": true,
        "value": Action
    },
    "run": {
        "writable"    : true,
        "configurable": true,
        "value": function (callback) {
            var emitter = new EventEmitter();
            if (typeof callback === "function") {
                emitter.on("signal", callback);
            }
            this["__Action__func__"].call(
                undefined,
                function (value) {
                    emitter.emit("signal", value);
                }
            );
            return emitter;
        }
    },
    "bind": {
        "writable"    : true,
        "configurable": true,
        "value": function (func) {
            var actionA = this;
            return new Action(function (emit) {
                var emitterA = actionA.run(function (valueA) {
                    var actionB  = func.call(undefined, valueA);
                    var emitterB = actionB.run(function (valueB) {
                        emit(valueB);
                    })
                });
            });
        }
    },
    "then": {
        "writable"    : true,
        "configurable": true,
        "value": function (actionB) {
            var actionA = this;
            return new Action(function (emit) {
                var emitterA = actionA.run(function (valueA) {
                    var emitterB = actionB.run(function (valueB) {
                        emit(valueB);
                    })
                });
            });
        }
    }
});

function pure(value) {
    return new Action(function (emit) {
        emit(value);
    });
}

function bind(action, func) {
    return action.bind(func);
}

function then(actionA, actionB) {
    return actionA.then(actionB);
}

function echo(message) {
    return new Action(function (emit) {
        console.log(message);
        emit(undefined);
    });
}

function crontab(cronTime, timeZone) {
    return new Action(function (emit) {
        new CronJob({
            "cronTime": cronTime,
            "start"   : true,
            "timeZone": timeZone,
            "onTick"  : function () {
                emit(undefined);
            }
        });
    });
}

end_module();

@@ -297,49 +297,13 @@
 var 
-events  = require(%22events%22),%0A    
 cron 
-   
 = re
@@ -322,48 +322,8 @@
 );%0A%0A
-var EventEmitter = events.EventEmitter;%0A
 var 
@@ -782,139 +782,100 @@
     
-var emitter = new EventEmitter();%0A            if (typeof callback === %22function%22) %7B%0A                emitter.on(%22signal%22, callback);
+this%5B%22__Action__func__%22%5D.call(%0A                undefined,%0A                function (value) %7B
 %0A   
@@ -879,34 +879,32 @@
             
-%7D%0A
         
     this%5B%22__
@@ -895,104 +895,102 @@
     
-    this%5B%22__Action__func__%22%5D.call(%0A                undefined,%0A                function (
+if (typeof callback === %22function%22) %7B%0A                        callback.call(undefined, 
 value)
- %7B
+;
 %0A   
@@ -1010,38 +1010,9 @@
     
-emitter.emit(%22signal%22, value);
+%7D
 %0A   
@@ -1045,36 +1045,8 @@
  );%0A
-            return emitter;%0A
     
@@ -1258,39 +1258,24 @@
             
- var emitterA =
  actionA.run
@@ -1375,39 +1375,24 @@
             
- var emitterB =
  actionB.run
@@ -1464,32 +1464,33 @@
               %7D)
+;
 %0A               
@@ -1738,23 +1738,8 @@
     
- var emitterA =
  act
@@ -1790,23 +1790,8 @@
     
- var emitterB =
  act
@@ -1879,16 +1879,17 @@
       %7D)
+;
 %0A       

Remove dependency on events