こんにちは、きんくまです。
JSの非同期処理のライブラリを書きました。
これです。
if(!window.kinkuma){
window.kinkuma = {}
}
(function(pkg){
/**
* Command - base class
*/
pkg.Command = function(){
this.delegate;
};
pkg.Command.prototype = {
execute:function(){
},
onComplete:function(){
if(this.delegate){
this.delegate();
}
}
}
/**
* FuncCommand - function command
* @param func Object - function reference you want to run
*/
pkg.FuncCommand = function(func){
this.targetFunction = func;
};
pkg.FuncCommand.prototype = new pkg.Command();
pkg.FuncCommand.prototype.execute = function(){
this.targetFunction();
this.onComplete();
};
/**
* WaitCommand - timer command
* @param waitTime Numer - it's not milliseconds but second
*/
pkg.WaitCommand = function(waitTime){
this.waitTime = waitTime;
this.timerId;
};
pkg.WaitCommand.prototype = new pkg.Command();
pkg.WaitCommand.prototype.execute = function(){
var self = this;
this.timerId = setTimeout(function(){self.onComplete();}, this.waitTime * 1000);
};
/**
* ParallelCommand - run all commands together
*/
pkg.ParallelCommand = function(){
this.commands = [];
this.count;
};
pkg.ParallelCommand.prototype = new pkg.Command();
pkg.ParallelCommand.prototype.execute = function(){
if(this.commands.length == 0){
this.commands = [];
this.onComplete();
}else{
this.count = 0;
var i, len;
len = this.commands.length;
var command;
var self = this;
for(i = 0; i < len; i++){
command = this.commands[i];
command.delegate = function(){self.commandComplete()};
command.execute();
}
}
};
pkg.ParallelCommand.prototype.add = function(command){
this.commands.push(command);
};
pkg.ParallelCommand.prototype.commandComplete = function(){
this.count++;
if(this.count == this.commands.length){
this.onComplete();
}
};
/**
* SerialCommand - serial run command
*/
pkg.SerialCommand = function(){
this.commands = [];
this.currentCommand;
};
pkg.SerialCommand.prototype = new pkg.Command();
pkg.SerialCommand.prototype.execute = function(){
if(this.commands.length == 0){
this.onComplete();
}else{
this.currentCommand = this.commands.shift();
var self = this;
this.currentCommand.delegate = function(){self.commandComplete();};
this.currentCommand.execute();
}
};
pkg.SerialCommand.prototype.add = function(command){
this.commands.push(command);
}
pkg.SerialCommand.prototype.commandComplete = function(){
this.currentCommand = null;
this.execute();
};
})(kinkuma);
使い方
このtraceは使い方で使うための関数で深い意味はないです。
function trace(message){
console.log(message);
}
では、ここから開始。
WaitCommand
タイマークラスです。
var mywait = new kinkuma.WaitCommand(1.0);
mywait.execute();
終了したときのコールバックを設定できます。
var mywait2 = new kinkuma.WaitCommand(2.0);
mywait2.delegate = function(){
trace('mywait2 finished');
};
mywait2.execute();
FuncCommand
関数のコマンドです。コンストラクタの引数に動かしたい関数を参照を代入します。
var myfunc = new kinkuma.FuncCommand(function(){
trace('myfunc done');
});
myfunc.execute();
コールバックを設定できます。
var myfunc2 = new kinkuma.FuncCommand(function(){
trace('myfunc2 done');
});
myfunc2.delegate = function(){
trace('myfunc2 complete');
};
myfunc2.execute();
ParallelCommand
コマンドをたくさんいれて、同時に実行します。
var k = kinkuma;
var para = new k.ParallelCommand();
para.add(new k.WaitCommand(1.0));
para.add(new k.FuncCommand(function(){trace('para func1');}));
para.delegate = function(){ trace('finish para');};
para.execute();
SerialCommand
順番にコマンドを実行していきます。
var k = kinkuma;
var sc = new k.SerialCommand();
sc.add(new k.WaitCommand(0.5));
sc.add(new k.FuncCommand(function(){trace('seri func1');}));
sc.add(new k.WaitCommand(3.0));
sc.delegate = function(){trace('finish serial');};
sc.execute();
Parallel と Serial を同時に使う
ParallelCommand と SerialCommand を混ぜて同時に実行することが可能です。
var k = kinkuma;
var p = new k.ParallelCommand();
p.add(new k.FuncCommand(function(){
trace('para func1');
}));
p.add(new k.FuncCommand(function(){
trace('para func2');
}));
var s = new k.SerialCommand();
s.add(new k.WaitCommand(1.0));
s.add(p);
s.delegate = function(){
trace('mix finish!');
};
s.execute();
このライブラリは、下のActionScript3のライブラリを参考に作りました。ありがとうございますです。
>> Commands (@fladdict)
>> Progression (@nium)
ではでは。