-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathreact-bacon.js
101 lines (96 loc) · 3.25 KB
/
react-bacon.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
var Bacon = require('baconjs');
module.exports.BaconMixin = ((function(){
'use strict';
function propsOrStateProperty(component, allPropsOrStateKey, groupKey, filterKey) {
var bacon = component._bacon = component._bacon || {};
var allPropertyKey = 'properties.'+allPropsOrStateKey;
var groupedPropertiesKey = 'properties.'+groupKey;
var property = bacon[allPropertyKey];
if (!property) {
var bus = bacon['buses.'+allPropsOrStateKey] = new Bacon.Bus();
property = bacon[allPropertyKey] = bus.toProperty(component[groupKey]).skipDuplicates();
}
if (filterKey != null) {
var wholePropsOrStateProperty = property;
var filteredPropertyKey = groupedPropertiesKey+'.'+filterKey;
property = bacon[filteredPropertyKey];
if (!property) {
property = bacon[filteredPropertyKey] = wholePropsOrStateProperty.
filter(function(x){return x;}).
map(function(propsOrState){
return propsOrState[filterKey];
}).
skipDuplicates().
toProperty();
}
}
return property;
}
return ({
propsProperty: function(propName) {
return propsOrStateProperty(this, 'allProps', 'props', propName);
},
stateProperty: function(stateName) {
return propsOrStateProperty(this, 'allState', 'state', stateName);
},
eventStream: function(eventName) {
var bacon = this._bacon = this._bacon || {};
var buses = bacon['buses.events'] = bacon['buses.events'] || {};
var bus = buses[eventName];
if (!bus) {
bus = buses[eventName] = new Bacon.Bus();
this[eventName] = function sendEventToStream(event) {
bus.push(event);
};
}
return bus;
},
plug: function(stream, stateKey) {
var unsubscribe;
var component = this;
var bacon = this._bacon = this._bacon || {};
var unsubscribers = bacon.unsubscribers = bacon.unsubscribers || [];
if (stateKey == null) {
unsubscribe = stream.onValue(function(partialState) {
component.setState(partialState);
});
} else {
unsubscribe = stream.onValue(function(value) {
var partialState = {};
partialState[stateKey] = value;
component.setState(partialState);
});
}
unsubscribers.push(unsubscribe);
return unsubscribe;
},
componentDidUpdate: function() {
var bacon = this._bacon;
if (bacon) {
var allPropsBus = bacon['buses.allProps'];
allPropsBus && allPropsBus.push(this.props);
var allStateBus = bacon['buses.allState'];
allStateBus && allStateBus.push(this.state);
}
},
componentWillUnmount: function() {
var bacon = this._bacon;
if (bacon) {
var allPropsBus = bacon['buses.allProps'];
allPropsBus && allPropsBus.end();
var allStateBus = bacon['buses.allState'];
allStateBus && allStateBus.end();
var eventBuses = bacon['buses.events'];
if (eventBuses) {
for (var eventName in eventBuses) {
eventBuses[eventName].end();
}
}
var unsubscribers = bacon.unsubscribers;
if (unsubscribers) {
unsubscribers.forEach(function(f) {f()});
}
}
}
});
})());