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

"use strict";

function end_module() {
    module.exports = Object.freeze({
        "echo"            : echo,
        "split"           : split,
        "fromEventEmitter": fromEventEmitter,
        "cron"            : crontab,
        "readFile"        : readFile,
        "writeFile"       : writeFile
    });
}

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

var CronJob = cron.CronJob;

var actions = require("./actions.js");

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

function split(array) {
    return new actions.Action(function (emit) {
        array.forEach(emit);
    });
}

function fromEventEmitter(emitter, event) {
    return new actions.Action(function (emit) {
        emitter.on(event, function () {
            emit.apply(undefined, arguments);
        });
    });
}

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

function readFile(file, encoding) {
    return new Action(function (emit) {
        fs.readFile(file, { "encoding": encoding }, function (error, content) {
            if (!error) {
                emit(content);
            }
            else {
                console.log(error);
            }
        });
    });
}

function writeFile(file, content, encoding) {
    return new Action(function (emit) {
        fs.writeFile(file, content, { "encoding": encoding }, function (error) {
            if (!error) {
                emit(undefined);
            }
            else {
                console.log(error);
            }
        });
    });
}

end_module();

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

"use strict";

function end_module() {
    module.exports = Object.freeze({
        "echo"            : echo,
        "split"           : split,
        "fromEventEmitter": fromEventEmitter,
        "cron"            : crontab,
        "readFile"        : readFile,
        "writeFile"       : writeFile
    });
}

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

var CronJob = cron.CronJob;

var actions = require("./actions.js");

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

function split(array) {
    return new actions.Action(function (emit) {
        array.forEach(emit);
    });
}

function fromEventEmitter(emitter, event) {
    return new actions.Action(function (emit) {
        emitter.on(event, function () {
            emit.apply(undefined, arguments);
        });
    });
}

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

function readFile(file, encoding) {
    return new actions.Action(function (emit) {
        fs.readFile(file, { "encoding": encoding }, function (error, content) {
            if (!error) {
                emit(content);
            }
            else {
                console.log(error);
            }
        });
    });
}

function writeFile(file, content, encoding) {
    return new actions.Action(function (emit) {
        fs.writeFile(file, content, { "encoding": encoding }, function (error) {
            if (!error) {
                emit(undefined);
            }
            else {
                console.log(error);
            }
        });
    });
}

end_module();

Fix read/writeFile