Last active
December 16, 2015 21:21
-
-
Save JoshuaRamirez/5499437 to your computer and use it in GitHub Desktop.
A super simple and easy to use Bus.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//The Bus module: | |
//======================== | |
var makeBus = function () { | |
var subscribers = {}; | |
var subscriptions = []; | |
var makeDeliveryJob = function (client) { | |
return function (event) { | |
client.Receive(event); | |
}; | |
}; | |
var makeSubscription = function (subscriber, eventName) { | |
var subscription = { | |
Subscriber: subscriber, | |
EventName: eventName, | |
Send: makeDeliveryJob(subscriber.Client) | |
}; | |
subscriptions.push(subscription); | |
return subscription; | |
}; | |
var makeSubscriber = function (client) { | |
var subscriber = { | |
Client: client | |
}; | |
subscribers[subscriber] = true; | |
return subscriber; | |
}; | |
var subscribe = function (client) { | |
if (!event.hasOwnProperty("Receive") && typeof client.Receive != 'function') { | |
throw "Interactivity Failure: " + | |
"Tried to subscribe a subscriber object missing a 'Receive(argEvent)' function. " + | |
"Add a 'Receive(argEvent)' function on the subscriber before subscribing."; | |
} | |
var subscribeTo = function (eventName) { | |
var subscriber = makeSubscriber(client); | |
makeSubscription(subscriber, eventName); | |
}; | |
var to = function (eventName) { | |
subscribeTo(eventName); | |
}; | |
return { | |
To: to | |
}; | |
}; | |
var sendSubscriptions = function(event){ | |
for (var i = 0; i < subscriptions.length; i++) { | |
var subscription = subscriptions[i]; | |
if (subscription.EventName = event.Name) { | |
subscription.Send(event); | |
} | |
} | |
}; | |
var publish = function (event) { | |
if (!event.hasOwnProperty("Name")) { | |
throw "Interactivity Failure: " + | |
"Tried to publish an event object missing a 'Name' property. " + | |
"Set a 'Name' property on the event object before publishing." | |
} | |
sendSubscriptions(event); | |
}; | |
return { | |
Subscribe: subscribe, | |
Publish: publish | |
} | |
}; | |
//Usage: | |
//========================= | |
//The bus object | |
var bus = makeBus(); | |
//Build a subscriber | |
var someSubscriber = { | |
Receive:function(event){ | |
//Handle the event here. | |
window.alert(event.Data); | |
} | |
}; | |
//Build a publisher | |
var makeSomePublisher = function(){ | |
//Build an Event | |
var makeSomeEvent = function(eventData){ | |
return { | |
Name:"SomeEvent", | |
Data: eventData //Add this or as many other properties as you like! | |
}; | |
}; | |
var publishSomeEvent = function(){ | |
var someData = "xyz"; //Events can have interesting data! | |
var event = makeSomeEvent(someData); | |
bus.Publish(event); | |
}; | |
var doSomeStuff = function(){ | |
//Do your thing here! | |
//When you're done, publish an event! | |
publishSomeEvent(eventData); | |
}; | |
return { | |
DoSomeStuff:doSomeStuff | |
}; | |
}; | |
var somePublisher = makeSomePublisher(); | |
//Interconnections for the win! | |
bus.Subscribe(someSubscriber).To("SomeEvent"); | |
somePublisher.DoSomeStuff(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment