Coder Social home page Coder Social logo

as3-message-buffer's Introduction

#AS3-Message-Buffer

A fast Message Buffer so that developers can listen for and process a message of a known type asynchronously.

This is an easy to use yet powerful tool that will allow you to dispatch messages (that you describe) to message-processors automatically.

##Features

  • Create custom messages that have a very low overhead
  • Create custom processors that can recieve the message they want and process them automatically
  • A message buffer that can asynchronously deliver messages to processors via an update call

##Example

In this scenario an enemy can take damage and message any observer how much damage it has taken.

First of all we need to set up the message buffer so we can register processors and dispatch messages:

			var buffer:MessageBuffer = new MessageBuffer();
			
			stage.addEventListener(Event.ENTER_FRAME, function(e:Event):void
				{
					buffer.update();
				});

It is up to you when the buffer gets updated, for this example a simple enter frame hook will work :)

Now we need a message type:

	public class EnemyHurtMessage extends BaseMessage 
	{
		public var damage:int = 0;
	}

Ensure all messages extend BaseMessage.

Now we have our message type, we can create a message processor to handle them:

	public class DamageMessageProcessor implements IMessageProcessor
	{
		public function get messageType():Class
		{
			return EnemyHurtMessage;
		}
		
		public function onMessage(inputMessage:*):void
		{
			var msg:EnemyHurtMessage = inputMessage;
			
			trace("Ouch I have been hurt " + msg.damage);
		}
	}

Finally we can register the processor to the message buffer and test it out:

			buffer.registerProcessor(new DamageMessageProcessor());
		
			var testMsg:EnemyHurtMessage = new EnemyHurtMessage();
			testMsg.damage = 10;
			
			buffer.enqueue(testMsg);

You will see the following output from tracing:

Ouch I have been hurt 10

Remember, unlike standard observable tools such as Signals the message-buffer is dispatched within the update making it asynchronous!

##Advanced example

We can take the previous example a step further by adding object pooling (the object pool can be found https://github.com/guerrillacontra/AS3-Automatic-Object-Pool):

			var pool:ObjectPool = new ObjectPool(1);
			
			var buffer:MessageBuffer = new MessageBuffer();
			
			buffer.onMessageRemoved = function(m:BaseMessage):void
			{
				if (pool.isFromPool(m))
				{
					pool.recycle(m);
				}
				
			};
			
			stage.addEventListener(Event.ENTER_FRAME, function(e:Event):void
				{
					buffer.update();
				});
			
			buffer.registerProcessor(new DamageMessageProcessor());
			
			var testMsg:EnemyHurtMessage = pool.fetch(EnemyHurtMessage);
			testMsg.damage = 10;
			
			buffer.enqueue(testMsg);

By overriding the onMessageRemoved callback we can provide custom behaviour and in this situation, recycle a pooled message automatically.

##Advanced example with a functional processor

The above example can also be written with a functional processor, very useful for simple processors.

			var pool:ObjectPool = new ObjectPool(1);
			
			var buffer:MessageBuffer = new MessageBuffer();
			
			buffer.onMessageRemoved = function(m:BaseMessage):void
			{
				if (pool.isFromPool(m))
				{
					pool.recycle(m);
				}
				
			};
			
			stage.addEventListener(Event.ENTER_FRAME, function(e:Event):void
				{
					buffer.update();
				});
			
			
			var onAttacked:Function = function(m:EnemyHurtMessage):void
			{
			   trace("Ouch I have been hurt " + m.damage);
			};
			
			buffer.registerProcessor(new FunctionalMessageProcessor(EnemyHurtMessage, onAttacked));
			
			var testMsg:EnemyHurtMessage = pool.fetch(EnemyHurtMessage);
			testMsg.damage = 10;
			
			buffer.enqueue(testMsg);

as3-message-buffer's People

Contributors

guerrillacontra avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.