Skip to content

Commit 68aa691

Browse files
committed
Add valueStream to read the targets value directly and redefine
eventStream, domEventStream and valueStream in terms of a common eventBus function.
1 parent 0e70e52 commit 68aa691

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

src/react-bacon.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@ var Bacon = require('baconjs');
33
module.exports.BaconMixin = ((function(){
44
'use strict';
55

6+
function eventBus(component, eventName, generator) {
7+
var bacon = component._bacon = component._bacon || {};
8+
var buses = bacon['buses.events'] = bacon['buses.events'] || {};
9+
var bus = buses[eventName];
10+
if (!bus) {
11+
bus = buses[eventName] = new Bacon.Bus();
12+
component[eventName] = generator(bus);
13+
}
14+
return bus;
15+
}
16+
617
function propsOrStateProperty(component, allPropsOrStateKey, groupKey, filterKey) {
718
var bacon = component._bacon = component._bacon || {};
819
var allPropertyKey = 'properties.'+allPropsOrStateKey;
@@ -36,32 +47,25 @@ module.exports.BaconMixin = ((function(){
3647
return propsOrStateProperty(this, 'allState', 'state', stateName);
3748
},
3849
eventStream: function(eventName) {
39-
var bacon = this._bacon = this._bacon || {};
40-
var buses = bacon['buses.events'] = bacon['buses.events'] || {};
41-
var bus = buses[eventName];
42-
if (!bus) {
43-
bus = buses[eventName] = new Bacon.Bus();
44-
this[eventName] = function sendEventToStream(event) {
45-
bus.push(event);
46-
};
47-
}
48-
return bus;
50+
return eventBus(this, eventName, function (bus) {
51+
return function sendEventToStream(event) { bus.push(event); };
52+
});
53+
},
54+
valueStream: function(eventName) {
55+
return eventBus(this, eventName, function (bus) {
56+
return function sendEventToStream(event) { bus.push(event.target.value); };
57+
});
4958
},
5059
domEventStream: function(eventName, prevDefault, stopPropagation) {
51-
var bacon = this._bacon = this._bacon || {};
52-
var buses = bacon['buses.events'] = bacon['buses.events'] || {};
53-
var bus = buses[eventName];
5460
if (typeof prevDefault == "undefined") prevDefault = true;
5561
if (typeof stopPropagation == "undefined") stopPropagation = true;
56-
if (!bus) {
57-
bus = buses[eventName] = new Bacon.Bus();
58-
this[eventName] = function sendEventToStream(event) {
62+
return eventBus(this, eventName, function (bus) {
63+
return function sendEventToStream(event) {
5964
prevDefault && event.preventDefault();
6065
stopPropagation && event.stopPropagation();
6166
bus.push(event);
6267
};
63-
}
64-
return bus;
68+
});
6569
},
6670
subscribeTo: function(unsub) {
6771
var bacon = this._bacon = this._bacon || {};

0 commit comments

Comments
 (0)