jsbus 0.0.2 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/jsbus/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module JsBus
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -1,50 +1,50 @@
1
- (function(window) {
1
+ (function (window) {
2
2
  "use strict";
3
3
 
4
- var eventCounter = 0;
5
-
6
4
  function isFunction(obj) {
7
5
  return !!(obj && obj.constructor && obj.call && obj.apply);
8
6
  }
9
7
 
10
- var EventBus = function() {
8
+ function log(message) {
9
+ if (window && window.eventBus && window.eventBus.debug && window.console && window.console.log) {
10
+ window.console.log(message);
11
+ }
12
+ }
13
+
14
+ var EventBus = function () {
11
15
  var self = this;
12
16
 
13
17
  // If true, logging to console is enabled.
14
18
  self.debug = false;
15
19
 
16
- self.subscribers = [];
17
-
18
- /*
19
- Publish an event to the event bus.
20
+ // Array of subscribers.
21
+ self.subscribers = [];
20
22
 
21
- eventType: string or string array
22
- data: data associated with the event
23
- callback: fired at the completion of the event
24
- */
25
- self.publish = function(eventType, data, callback) {
26
- var eventTypes;
23
+ // Publish an event to the event bus.
24
+ // eventType: string or string array
25
+ // data: data associated with the event
26
+ // callback: fired at the completion of the event
27
+ self.publish = function (eventType, data, callback) {
28
+ var eventTypes = [].concat(eventType);
27
29
  if (isFunction(data)) {
28
30
  log("Given callback but no data. Assigning data to callback.");
29
31
  callback = data;
30
32
  data = {};
31
33
  }
32
-
33
- eventTypes = [].concat(eventType);
34
34
  createAndPublishEvent(eventTypes, data, callback);
35
35
  };
36
36
 
37
37
  // Reset the event bus by removing all subscribers. This is useful
38
38
  // (and necessary) in testing scenarios.
39
39
  self.reset = function() {
40
- eventCounter = 0;
40
+ self.debug = false;
41
41
  self.subscribers = [];
42
42
  };
43
43
 
44
- // Subscribe to one or more events.
44
+ // Subscribe to one or more events. If eventType is an array, the given callback
45
+ // will be bound to multiple events.
45
46
  self.subscribe = function(eventType, callback) {
46
- var eventTypes;
47
- eventTypes = [].concat(eventType);
47
+ var eventTypes = [].concat(eventType);
48
48
  subscribeToEventTypes(eventTypes, callback);
49
49
  };
50
50
 
@@ -58,12 +58,6 @@
58
58
  }
59
59
  }
60
60
 
61
- function log(message) {
62
- if (self.debug && window.console && window.console.log) {
63
- window.console.log(message);
64
- }
65
- }
66
-
67
61
  function pushEventToSubscribers(event, eventType, callback) {
68
62
  var i, subscriber;
69
63
  for (i = self.subscribers.length - 1; i >= 0; i--) {
@@ -82,41 +76,35 @@
82
76
  self.subscribers.push(subscriber);
83
77
  }
84
78
  }
85
- };
79
+ }; // Closes EventBus definition
86
80
 
87
- /*
88
- Subscriber object. A holder for an event type and a callback.
89
- */
81
+ // Subscriber object. A holder for an event type and a callback.
90
82
  var Subscriber = function(eventType, callback) {
91
83
  var self = this;
92
84
  self.eventType = eventType;
93
85
  self.callback = callback;
94
86
  };
95
87
 
96
- /*
97
- Event object.
98
- */
88
+ // Event object.
99
89
  var Event = function(eventType, data) {
100
90
  var self = this;
101
- self.eventCounter = nextEventCounter();
102
91
  self.eventType = eventType;
103
92
  self.data = data || {};
104
93
 
94
+ // Push the event to the given subscriber.
105
95
  self.push = function(subscriber, callback) {
106
- subscriber.callback(self);
96
+ // Invoke the subscriber's callback function. Save the response, if any.
97
+ var response = subscriber.callback(self) || { };
98
+
99
+ // If the publisher has a callback, invoke, passing the subscriber's response.
107
100
  if (callback && isFunction(callback)) {
108
- callback();
101
+ callback(response);
109
102
  }
110
103
  };
111
104
 
112
105
  self.toString = function() {
113
- return "Event " + self.eventCounter + ", Type: " + self.eventType;
106
+ return "Event Type: " + self.eventType;
114
107
  };
115
-
116
- function nextEventCounter() {
117
- eventCounter += 1;
118
- return eventCounter;
119
- }
120
108
  };
121
109
 
122
110
  // Do not define an eventBus is one has already been created.
@@ -124,4 +112,4 @@
124
112
  return;
125
113
  }
126
114
  window.eventBus = new EventBus();
127
- })(window);
115
+ }(window));
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsbus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: