Skip to content

Commit 464bb26

Browse files
committed
Merge pull request #306 from ericwooley/componentDidConnectHook
Component did connect hook
2 parents 4998c6a + 7c83e17 commit 464bb26

File tree

4 files changed

+92
-33
lines changed

4 files changed

+92
-33
lines changed

dist/alt-with-addons.js

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
module.exports = require('./components/AltContainer.js');
55

66
},{"./components/AltContainer.js":2}],2:[function(require,module,exports){
7-
(function (global){
87
/**
98
* AltContainer.
109
*
@@ -63,7 +62,7 @@ module.exports = require('./components/AltContainer.js');
6362
*/
6463
'use strict';
6564

66-
var React = (typeof window !== "undefined" ? window.React : typeof global !== "undefined" ? global.React : null);
65+
var React = (window.React);
6766
var mixinContainer = require('./mixinContainer');
6867
var assign = require('../utils/functions').assign;
6968

@@ -77,7 +76,6 @@ var AltContainer = React.createClass(assign({
7776

7877
module.exports = AltContainer;
7978

80-
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
8179
},{"../utils/functions":27,"./mixinContainer":3}],3:[function(require,module,exports){
8280
'use strict';
8381

@@ -1593,11 +1591,12 @@ var StoreMixin = {
15931591
return x;
15941592
};
15951593

1596-
var makeActionHandler = function makeActionHandler(action) {
1594+
var makeActionHandler = function makeActionHandler(action, isError) {
15971595
return function (x) {
15981596
var fire = function fire() {
15991597
loadCounter -= 1;
16001598
action(intercept(x, action, args));
1599+
if (isError) throw x;
16011600
};
16021601
return typeof window === 'undefined' ? function () {
16031602
return fire();
@@ -1610,7 +1609,7 @@ var StoreMixin = {
16101609
loadCounter += 1;
16111610
/* istanbul ignore else */
16121611
if (spec.loading) spec.loading(intercept(null, spec.loading, args));
1613-
return spec.remote.apply(spec, [state].concat(args)).then(makeActionHandler(spec.success))['catch'](makeActionHandler(spec.error));
1612+
return spec.remote.apply(spec, [state].concat(args))['catch'](makeActionHandler(spec.error, 1)).then(makeActionHandler(spec.success));
16141613
} else {
16151614
// otherwise emit the change now
16161615
_this.emitChange();
@@ -1959,6 +1958,7 @@ var STATE_CONTAINER = (0, _esSymbol2['default'])();
19591958
exports.STATE_CONTAINER = STATE_CONTAINER;
19601959

19611960
},{"es-symbol":5}],16:[function(require,module,exports){
1961+
/* istanbul ignore next */
19621962
'use strict';
19631963

19641964
Object.defineProperty(exports, '__esModule', {
@@ -1969,7 +1969,6 @@ exports.warn = warn;
19691969
exports.uid = uid;
19701970
exports.formatAsConstant = formatAsConstant;
19711971
exports.dispatchIdentity = dispatchIdentity;
1972-
/* istanbul ignore next */
19731972
function NoopClass() {}
19741973

19751974
var builtIns = Object.getOwnPropertyNames(NoopClass);
@@ -2163,16 +2162,6 @@ exports['default'] = ActionListeners;
21632162
module.exports = exports['default'];
21642163

21652164
},{"es-symbol":5}],19:[function(require,module,exports){
2166-
'use strict';
2167-
2168-
Object.defineProperty(exports, '__esModule', {
2169-
value: true
2170-
});
2171-
2172-
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
2173-
2174-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
2175-
21762165
/**
21772166
* AltManager(Alt: AltClass): undefined
21782167
*
@@ -2199,6 +2188,16 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
21992188
* ```
22002189
*/
22012190

2191+
'use strict';
2192+
2193+
Object.defineProperty(exports, '__esModule', {
2194+
value: true
2195+
});
2196+
2197+
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
2198+
2199+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
2200+
22022201
var AltManager = (function () {
22032202
function AltManager(Alt) {
22042203
_classCallCheck(this, AltManager);
@@ -2494,12 +2493,12 @@ function atomic(alt) {
24942493
module.exports = exports['default'];
24952494

24962495
},{"./functions":24,"./makeFinalStore":25}],22:[function(require,module,exports){
2496+
/*global window*/
24972497
'use strict';
24982498

24992499
Object.defineProperty(exports, '__esModule', {
25002500
value: true
25012501
});
2502-
/*global window*/
25032502
exports['default'] = chromeDebug;
25042503

25052504
function chromeDebug(alt) {
@@ -2510,7 +2509,6 @@ function chromeDebug(alt) {
25102509
module.exports = exports['default'];
25112510

25122511
},{}],23:[function(require,module,exports){
2513-
(function (global){
25142512
/**
25152513
* 'Higher Order Component' that controls the props of a wrapped
25162514
* component via stores.
@@ -2564,7 +2562,7 @@ Object.defineProperty(exports, '__esModule', {
25642562

25652563
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
25662564

2567-
var _react = (typeof window !== "undefined" ? window.React : typeof global !== "undefined" ? global.React : null);
2565+
var _react = (window.React);
25682566

25692567
var _react2 = _interopRequireDefault(_react);
25702568

@@ -2594,6 +2592,10 @@ function connectToStores(Component) {
25942592
stores.forEach(function (store) {
25952593
store.listen(_this.onChange);
25962594
});
2595+
var component = this.refs['connectToStores-component'];
2596+
if (typeof component.componentDidConnect === 'function') {
2597+
component.componentDidConnect();
2598+
}
25972599
},
25982600

25992601
componentWillUnmount: function componentWillUnmount() {
@@ -2610,7 +2612,9 @@ function connectToStores(Component) {
26102612
},
26112613

26122614
render: function render() {
2613-
return _react2['default'].createElement(Component, (0, _functions.assign)({}, this.props, this.state));
2615+
return _react2['default'].createElement(Component, (0, _functions.assign)({
2616+
ref: 'connectToStores-component'
2617+
}, this.props, this.state));
26142618
}
26152619
});
26162620

@@ -2620,7 +2624,6 @@ function connectToStores(Component) {
26202624
exports['default'] = connectToStores;
26212625
module.exports = exports['default'];
26222626

2623-
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
26242627
},{"./functions":24}],24:[function(require,module,exports){
26252628
'use strict';
26262629

@@ -2655,12 +2658,6 @@ function assign(target) {
26552658
}
26562659

26572660
},{}],25:[function(require,module,exports){
2658-
"use strict";
2659-
2660-
Object.defineProperty(exports, "__esModule", {
2661-
value: true
2662-
});
2663-
exports["default"] = makeFinalStore;
26642661
/**
26652662
* makeFinalStore(alt: AltInstance): AltStore
26662663
*
@@ -2685,6 +2682,12 @@ exports["default"] = makeFinalStore;
26852682
* ```
26862683
*/
26872684

2685+
"use strict";
2686+
2687+
Object.defineProperty(exports, "__esModule", {
2688+
value: true
2689+
});
2690+
exports["default"] = makeFinalStore;
26882691
function FinalStore() {
26892692
var _this = this;
26902693

@@ -2707,7 +2710,6 @@ function makeFinalStore(alt) {
27072710
module.exports = exports["default"];
27082711

27092712
},{}],26:[function(require,module,exports){
2710-
(function (global){
27112713
'use strict';
27122714

27132715
Object.defineProperty(exports, '__esModule', {
@@ -2717,7 +2719,7 @@ exports['default'] = withAltContext;
27172719

27182720
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
27192721

2720-
var _react = (typeof window !== "undefined" ? window.React : typeof global !== "undefined" ? global.React : null);
2722+
var _react = (window.React);
27212723

27222724
var _react2 = _interopRequireDefault(_react);
27232725

@@ -2741,7 +2743,6 @@ function withAltContext(flux) {
27412743

27422744
module.exports = exports['default'];
27432745

2744-
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
27452746
},{}],27:[function(require,module,exports){
27462747
'use strict';
27472748

dist/alt.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,11 +1004,12 @@ var StoreMixin = {
10041004
return x;
10051005
};
10061006

1007-
var makeActionHandler = function makeActionHandler(action) {
1007+
var makeActionHandler = function makeActionHandler(action, isError) {
10081008
return function (x) {
10091009
var fire = function fire() {
10101010
loadCounter -= 1;
10111011
action(intercept(x, action, args));
1012+
if (isError) throw x;
10121013
};
10131014
return typeof window === 'undefined' ? function () {
10141015
return fire();
@@ -1021,7 +1022,7 @@ var StoreMixin = {
10211022
loadCounter += 1;
10221023
/* istanbul ignore else */
10231024
if (spec.loading) spec.loading(intercept(null, spec.loading, args));
1024-
return spec.remote.apply(spec, [state].concat(args)).then(makeActionHandler(spec.success))['catch'](makeActionHandler(spec.error));
1025+
return spec.remote.apply(spec, [state].concat(args))['catch'](makeActionHandler(spec.error, 1)).then(makeActionHandler(spec.success));
10251026
} else {
10261027
// otherwise emit the change now
10271028
_this.emitChange();
@@ -1370,6 +1371,7 @@ var STATE_CONTAINER = (0, _esSymbol2['default'])();
13701371
exports.STATE_CONTAINER = STATE_CONTAINER;
13711372

13721373
},{"es-symbol":1}],11:[function(require,module,exports){
1374+
/* istanbul ignore next */
13731375
'use strict';
13741376

13751377
Object.defineProperty(exports, '__esModule', {
@@ -1380,7 +1382,6 @@ exports.warn = warn;
13801382
exports.uid = uid;
13811383
exports.formatAsConstant = formatAsConstant;
13821384
exports.dispatchIdentity = dispatchIdentity;
1383-
/* istanbul ignore next */
13841385
function NoopClass() {}
13851386

13861387
var builtIns = Object.getOwnPropertyNames(NoopClass);

src/utils/connectToStores.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ function connectToStores(Component) {
6666
stores.forEach((store) => {
6767
store.listen(this.onChange)
6868
})
69+
if (Component.componentDidConnect !== undefined) {
70+
Component.componentDidConnect(this.props)
71+
}
6972
},
7073

7174
componentWillUnmount() {

test/connect-to-stores-test.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,60 @@ export default {
154154
const span = TestUtils.findRenderedDOMComponentWithTag(node, 'span')
155155

156156
assert(span.props.foo === 'Baz')
157+
},
158+
159+
'componentDidConnect hook is called '() {
160+
let componentDidConnect = false
161+
class ClassComponent extends React.Component {
162+
static getStores() {
163+
return [testStore]
164+
}
165+
static getPropsFromStores(props) {
166+
return testStore.getState()
167+
}
168+
static componentDidConnect() {
169+
componentDidConnect = true
170+
}
171+
render() {
172+
return <span foo={this.props.foo} />
173+
}
174+
}
175+
const WrappedComponent = connectToStores(ClassComponent)
176+
const node = TestUtils.renderIntoDocument(
177+
<WrappedComponent />
178+
)
179+
assert(componentDidConnect === true)
180+
},
181+
'Component receives all updates'(done) {
182+
let componentDidConnect = false
183+
class ClassComponent extends React.Component {
184+
static getStores() {
185+
return [testStore]
186+
}
187+
static getPropsFromStores(props) {
188+
return testStore.getState()
189+
}
190+
static componentDidConnect() {
191+
testActions.updateFoo('Baz')
192+
componentDidConnect = true
193+
}
194+
componentDidUpdate() {
195+
assert(this.props.foo === 'Baz')
196+
done()
197+
}
198+
render() {
199+
return <span foo={this.props.foo} />
200+
}
201+
}
202+
203+
const WrappedComponent = connectToStores(ClassComponent)
204+
205+
let node = TestUtils.renderIntoDocument(
206+
<WrappedComponent />
207+
)
208+
209+
const span = TestUtils.findRenderedDOMComponentWithTag(node, 'span')
210+
assert(componentDidConnect === true)
157211
}
158212
}
159213
}

0 commit comments

Comments
 (0)