ACTION QUERRY
by João Vitor Scheuermann
JavaScript
function ActionsQuerry() {
let ACTIONS = new Array();
let QUERRY = new Array();
ACTIONS.push({
NAME: "teste",
ARGS: null,
CALLBACK: null,
DO: function(args, callback, next) {
console.log('teste worked')
setTimeout(function() {
next();
}, 5000);
}
})
ACTIONS.push({
NAME: "teste2",
ARGS: null,
CALLBACK: null,
DO: function(args, callback, next) {
console.log('teste2 worked')
setTimeout(function() {
next();
}, 2000);
}
})
this.do = function() {
if (typeof arguments[0] !== "string") throw new Error("action must be a string!!");
let action = arguments[0];
let callback = arguments[arguments.length - 1];
let args = arguments.length > 2 ? Array.prototype.slice.call(arguments, 1, arguments.length - 1) : undefined;
for (ACTION of ACTIONS) {
if (ACTION.NAME === action) {
ACTION.ARGS = args;
ACTION.CALLBACK = callback;
QUERRY.push(ACTION);
}
}
}
let index = 0;
this.start = (callback) => {
function next() {
index++
if (index <= QUERRY.length - 1) {
QUERRY[index].DO(QUERRY[index].ARGS, QUERRY[index].CALLBACK, next)
} else {
if (callback !== undefined) callback();
}
};
if (index == 0) {
QUERRY[index].DO(QUERRY[index].ARGS, QUERRY[index].CALLBACK, next)
};
}
}
let teste = new ActionsQuerry();
teste.do("teste", "end");
teste.do("teste2", "end");
teste.do("teste", "end");
teste.start(() => {
console.log('done')
})