publisher

Top  Previous  Next

Pubsub is a thread safe publish and subscriber class. This can be very useful to decouple classes from each other. Which is considered good programming practice.

 

publisher = publisher.new()

 

The publisher object has the following methods:

 

Return

Signature

Description

number

subscribe( callback )

subscribes the function to this publisher. Returns the ID of this subscription. This can be used to unsubscribe at a later date.

 

unsubscribe( id )

Unsubscribe from the publisher.

 

publish( arguments )

This function will call all the subscribers with the arguments pass.

 

Example

 

local publisher = require('publisher')

 

local p = publisher.new()

 

p:subscribe( function( aa )

print("Called 1!", aa)

end)

 

p:subscribe( function( aa )

print("Called 2!", aa)

end)

 

p:publish ( "cool" )

 

this will print

 

Thu May 2 15:21:12 2013        Called 1!        cool

Thu May 2 15:21:12 2013        Called 2!        cool