Skip to content

Library that implements a simple publish/subscribe model in LiveCode

License

Notifications You must be signed in to change notification settings

stam66/skPubSub

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

skPubSub

Library that implements a simple publish/subscribe model in LiveCode
Example usage/test stack can be found here

About publish/subscribe

In the Pub/Sub model, any handler can ‘Publish’ a message - ie. emit a message blindly, without knowing who will receive this and what will happen. One or more objects can ‘subscribe’ to this message and perform 1 or more actions when received, without the publisher knowing who the subscriber is, or the subscriber knowing who the publisher is (aka ‘loose coupling’).

Publishers post messages to an intermediary message broker (in this case a library loaded into the backScripts to be available in any context), and subscribers register subscriptions with that broker which logs the long id of the subscriber and which handler the subscriber should call (the ‘callback’ handler). A subscriber can register multiple callbacks for a message and can subscribe to multiple messages.

The library

The attached scriptOnlyStack is loaded by using ‘start using stack…”. The stack’s script loads it into the backScripts making it available in any context.

Andre Garzia published a book called Advanced Application Architecture for LiveCode, which bundled a simple stack that does this. While I had long ago purchased his eBook I couldn’t for the life of me locate the stack, so I have tried to re-created his script (more or less successfully) - any kudos should go to him, any blame with this should be directed at me! (The book is well worth reading and highly recommended).

API

subscribe pEvent, pCallback, pTarget
• pEvent is the message to subscribe to
• pCallback is the hander the subscriber will run when the message is received
• pTarget is the long id of the subscriber

broadcast pEvent, pData
• pEvent is the message to broadcast
• pData are the parameters for the callback handler

unsubscribe pEvent, pCallback, pTarget
• pEvent is the message to unsubscribe from
• pCallback is the callback to remove
• pTarget is the long id of the subscriber to unsubscribe
This allows specific callbacks to be removed without wholly unsuscribing; if not callback specified all are removed.

removeAllSubscriptions
All subscriptions for all controls are removed.

About

Library that implements a simple publish/subscribe model in LiveCode

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published