-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathreact-bacon.js
More file actions
106 lines (100 loc) · 4 KB
/
react-bacon.js
File metadata and controls
106 lines (100 loc) · 4 KB
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
102
103
104
105
106
!function(e){if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.ReactBacon=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
var Bacon = (window.Bacon);
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()});
}
}
}
});
})());
},{}]},{},[1])
(1)
});