Posts Tagged ‘asynchronous’

[JavaScript] Asynchronous library in Javascript

Thursday, December 1st, 2011

I wrote asynchronous JavaScript library.

Here is the code.

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);

Usage

This function named 'trace' is used with these usage. It's not so important.

function trace(message){
	console.log(message);
}

Let's start it.

WaitCommand
It's a timer class.

var mywait = new kinkuma.WaitCommand(1.0);
mywait.execute();

You can add callback function.

var mywait2 = new kinkuma.WaitCommand(2.0);
mywait2.delegate = function(){
	trace('mywait2 finished');
};
mywait2.execute();

FuncCommand
It's function command. You have to input function reference to constructor's argument.

var myfunc = new kinkuma.FuncCommand(function(){
	trace('myfunc done');
});
myfunc.execute();

You can add callback function.

var myfunc2 = new kinkuma.FuncCommand(function(){
	trace('myfunc2 done');
});
myfunc2.delegate = function(){
	trace('myfunc2 complete');
};
myfunc2.execute();

ParallelCommand
These commands will start in the same time.

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
Serial execute commands.

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 and Serial Mix in
It's very interesting time. You can mix with ParallelCommand and 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();

I referred to below ActionScript3 codes in Japan, when I created it.
>> Commands (@fladdict)
>> Progression (@nium)

Thank you for reading it.

[iOS] Asynchronous Library in Objective-C

Tuesday, December 28th, 2010

I made a asynchronous library in Objective-C.
Here is Project file.

What is this ?

Asynchronous library is one of the CommandPettern.
Set a command, and execute it.
Then it can call method asynchronously.

Class Explanation

Command > Base class
WaitCommand > timer class
FunctionCommand > function class
SerialCommand > it call registered methods sequentially
ParallelCommand > it call registered methods at the same time

Inspired from
These ActionScript 3 libraries in Japan.
Commands (@fladdict)
Progression(@nium)

How to use

First, you copy below picture files.

WaitCommand test

- (void)commandCompleteHandler:(NSNotification *)aNotification
{
	NSLog(@"command complete !");
}

- (void)waitCommandTest
{
	WaitCommand *wc = [[WaitCommand alloc] initWithTime:1.0];
	[wc addEventListener:kCommandEventCommandComplete target:self listener:@selector(commandCompleteHandler:)];
	[wc execute];
	[wc release];
}

FunctionCommand test

- (void)commandCompleteHandler:(NSNotification *)aNotification
{
	NSLog(@"command complete !");
}

- (void)testFunction
{
	NSLog(@"test function");
}

- (void)functionCommandTest
{
	FunctionCommand *fc = [[FunctionCommand alloc] initWithTarget:self selector:@selector(testFunction)];
	[fc addEventListener:kCommandEventCommandComplete target:self listener:@selector(commandCompleteHandler:)];
	[fc execute];
	[fc release];
}

SerialCommand test

- (void)commandCompleteHandler:(NSNotification *)aNotification
{
	NSLog(@"command complete !");
}

- (void)testFunction
{
	NSLog(@"test function");
}

- (void)serialCommandTest
{
	SerialCommand *sc = [[SerialCommand alloc] init];
	WaitCommand *wc = [[WaitCommand alloc] initWithTime:1.5];
	FunctionCommand *fc = [[FunctionCommand alloc] initWithTarget:self selector:@selector(testFunction)];
	
	[sc addCommand:wc];
	[sc addCommand:fc];
	[sc addEventListener:kCommandEventCommandComplete target:self listener:@selector(commandCompleteHandler:)];
	[sc execute];

	[wc release];
	[fc release];
}

ParallelCommand test

- (void)commandCompleteHandler:(NSNotification *)aNotification
{
	NSLog(@"command complete !");
}

- (void)testFunction
{
	NSLog(@"test function");
}

- (void)parallelCommandTest
{
	ParallelCommand *pc = [[ParallelCommand alloc] init];
	WaitCommand *wc = [[WaitCommand alloc] initWithTime:1.5];
	FunctionCommand *fc = [[FunctionCommand alloc] initWithTarget:self selector:@selector(testFunction)];
	
	[pc addCommand:wc];
	[pc addCommand:fc];
	[pc addEventListener:kCommandEventCommandComplete target:self listener:@selector(commandCompleteHandler:)];
	[pc execute];

	[wc release];
	[fc release];
}

PrallelCommand and SerialCommand Mixed test

- (void)commandCompleteHandler:(NSNotification *)aNotification
{
	NSLog(@"command complete !");
}

- (void)testFunction
{
	NSLog(@"test function");
}

- (void)testFunction2:(id)arg1
{
	NSLog(@"testFunction 2, arg1 = %@", arg1);
}

- (void)testFunction3:(id)arg1 arg2:(id)arg2
{
	NSLog(@"testFunction 2, arg1 = %@, arg2 = %@", arg1, arg2);
}

- (void)parallelSerialMixedTest
{
	ParallelCommand *pc = [[ParallelCommand alloc] init];
	WaitCommand *wc = [[WaitCommand alloc] initWithTime:1.5];
	FunctionCommand *fc = [[FunctionCommand alloc] initWithTarget:self selector:@selector(testFunction)];
	FunctionCommand *fc2 = [[FunctionCommand alloc] initWithTarget:self selector:@selector(testFunction2:) withObject:[UIColor blueColor]];
	UIViewController *vc = [[UIViewController alloc] init];
	FunctionCommand *fc3 = [[FunctionCommand alloc] initWithTarget:self selector:@selector(testFunction3:arg2:) withObject:@"Hello command !" withObject:vc];
	[vc release];
	WaitCommand *wc2 = [[WaitCommand alloc] initWithTime:1.0];
	SerialCommand *sc = [[SerialCommand alloc] init];
	[pc addCommand:fc];
	[pc addCommand:fc2];
	[pc addCommand:fc3];
	
	[sc addCommand:wc];
	[sc addCommand:fc];
	[sc addCommand:wc2];
	[sc addCommand:pc];
	
	[sc addEventListener:kCommandEventCommandComplete target:self listener:@selector(commandCompleteHandler:)];
	[sc execute];
	
	[pc release];
	[wc release];
	[fc release];
	[fc2 release];
	[fc3 release];
	[wc2 release];
}