Skip to content

Commit 47a7e0b

Browse files
author
aksonov
committed
* Pass exception after failure as ‘error’ param to next scene
* Run custom navigator (if ‘navigator’ is passed) even if ’tabs’ is defined - it allows not to use clone scenes within ‘tabbed’ custom navigators * Refresh state after switching tab for JUMP action and non-empty params because React Navigation doesn’t do it (it just switched index) * Throw exception if ‘component’ & ‘children’ to avoid ‘Actions.{key} not found’ error (breaking changes for v3) * Throw exception if there is multiple children for Router (breaking changes for v3)
1 parent fa6034d commit 47a7e0b

File tree

5 files changed

+34
-21
lines changed

5 files changed

+34
-21
lines changed

Example/components/TabView.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const TabView = (props) => {
2929
return (
3030
<View style={[styles.container, props.sceneStyle ]}>
3131
<Text>Tab title:{props.title} name:{props.name}</Text>
32+
<Text>Tab data:{props.data}</Text>
3233
{props.name === 'tab1_1' &&
3334
<Button onPress={()=>Actions.tab1_2()}>next screen for tab1_1</Button>
3435
}
@@ -40,7 +41,7 @@ const TabView = (props) => {
4041
<Button onPress={() => { Actions.tab2(); }}>Switch to tab2</Button>
4142
<Button onPress={() => { Actions.tab3(); }}>Switch to tab3</Button>
4243
<Button onPress={() => { Actions.tab4(); }}>Switch to tab4</Button>
43-
<Button onPress={() => { Actions.tab5(); }}>Switch to tab5</Button>
44+
<Button onPress={() => { Actions.tab5({ data: 'test!' }); }}>Switch to tab5 with data</Button>
4445
<Button onPress={() => { Actions.echo(); }}>push clone scene (EchoView)</Button>
4546
</View>
4647
);

dist/Router.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,12 @@ _navigationStore2.default.states[key][transition]=transition[props];
244244
}
245245
delete props.children;
246246
if(success){
247-
_navigationStore2.default.states[key].success=success instanceof Function?success:function(args){console.log('Transition to state='+success);_navigationStore2.default[success](args);};
247+
_navigationStore2.default.states[key].success=success instanceof Function?
248+
success:function(args){console.log('Transition to state='+success);_navigationStore2.default[success](args);};
248249
}
249250
if(failure){
250-
_navigationStore2.default.states[key].failure=failure instanceof Function?failure:function(args){console.log('Transition to state='+failure);_navigationStore2.default[failure](args);};
251+
_navigationStore2.default.states[key].failure=failure instanceof Function?
252+
failure:function(args){console.log('Transition to state='+failure);_navigationStore2.default[failure](args);};
251253
}
252254

253255
var screen={
@@ -285,6 +287,9 @@ initialRouteParams=_extends({},commonProps,props);
285287
}};for(var _iterator3=children,_isArray3=Array.isArray(_iterator3),_i4=0,_iterator3=_isArray3?_iterator3:_iterator3[typeof Symbol==='function'?Symbol.iterator:'@@iterator']();;){var _ref7;if(_isArray3){if(_i4>=_iterator3.length)break;_ref7=_iterator3[_i4++];}else{_i4=_iterator3.next();if(_i4.done)break;_ref7=_i4.value;}var _child=_ref7;_loop(_child);
286288
}
287289
var mode=modal?'modal':'card';
290+
if(navigator){
291+
return navigator(res,_extends({lazy:lazy,initialRouteName:initialRouteName,initialRouteParams:initialRouteParams,order:order},commonProps,{navigationOptions:createNavigationOptions(commonProps)}));
292+
}
288293
if(lightbox){
289294
return(0,_LightboxNavigator2.default)(res,{mode:mode,initialRouteParams:initialRouteParams,initialRouteName:initialRouteName,navigationOptions:createNavigationOptions(commonProps)});
290295
}else if(tabs){
@@ -293,20 +298,18 @@ tabBarOptions:createTabBarOptions(commonProps),navigationOptions:createNavigatio
293298
}else if(drawer){
294299
return(0,_reactNavigation.DrawerNavigator)(res,_extends({initialRouteName:initialRouteName,contentComponent:contentComponent,order:order},commonProps));
295300
}
296-
if(navigator){
297-
return navigator(res,_extends({lazy:lazy,initialRouteName:initialRouteName,initialRouteParams:initialRouteParams,order:order},commonProps,{navigationOptions:createNavigationOptions(commonProps)}));
298-
}
299301
return(0,_reactNavigation.StackNavigator)(res,_extends({mode:mode,initialRouteParams:initialRouteParams,initialRouteName:initialRouteName},commonProps,{navigationOptions:createNavigationOptions(commonProps)}));
300302
}
301303

302304
var Router=function Router(_ref9){var createReducer=_ref9.createReducer,_ref9$wrapBy=_ref9.wrapBy,wrapBy=_ref9$wrapBy===undefined?function(props){return props;}:_ref9$wrapBy,props=_objectWithoutProperties(_ref9,['createReducer','wrapBy']);
305+
(0,_Util.assert)(!Array.isArray(props.children),'Router should contain only one scene, please wrap your scenes with Scene ');
303306
var scene=props.children;
304307
var AppNavigator=processScene(scene,props,[],wrapBy);
305308
_navigationStore2.default.router=AppNavigator.router;
306309
_navigationStore2.default.reducer=createReducer&&createReducer(props);
307310
RightNavBarButton=wrapBy(_NavBar.RightButton);
308311
LeftNavBarButton=wrapBy(_NavBar.LeftButton);
309-
return _react2.default.createElement(App,{navigator:AppNavigator,__source:{fileName:_jsxFileName,lineNumber:309}});
312+
return _react2.default.createElement(App,{navigator:AppNavigator,__source:{fileName:_jsxFileName,lineNumber:312}});
310313
};
311314
Router.propTypes={
312315
createReducer:_propTypes2.default.func,

dist/navigationStore.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ function NavigationStore(){var _this=this;_classCallCheck(this,NavigationStore);
130130

131131
nextState=function(state,cmd){return _this.reducer?_this.reducer(state,cmd):_this._router.getStateForAction(cmd,state);};this.
132132

133-
dispatch=function(cmd,type){
134-
_this.setState(_this.nextState(_this.state,cmd),type);
133+
dispatch=function(cmd,type,params){
134+
_this.setState(_this.nextState(_this.state,cmd),type,params);
135135
};_initDefineProp(this,'setState',_descriptor4,this);this.
136136

137137

@@ -149,6 +149,9 @@ _this.setState(_this.nextState(_this.state,cmd),type);
149149

150150

151151

152+
153+
154+
152155
run=function(){for(var _len=arguments.length,params=Array(_len>3?_len-3:0),_key=3;_key<_len;_key++){params[_key-3]=arguments[_key];}var type=arguments.length>0&&arguments[0]!==undefined?arguments[0]:ActionConst.PUSH;var routeName=arguments[1];var actions=arguments[2];
153156
var res={};
154157
for(var _iterator=params,_isArray=Array.isArray(_iterator),_i=0,_iterator=_isArray?_iterator:_iterator[typeof Symbol==='function'?typeof Symbol==='function'?Symbol.iterator:'@@iterator':'@@iterator']();;){var _ref;if(_isArray){if(_i>=_iterator.length)break;_ref=_iterator[_i++];}else{_i=_iterator.next();if(_i.done)break;_ref=_i.value;}var param=_ref;
@@ -158,7 +161,7 @@ res=_extends({},res,filterParam(param));
158161
}
159162
res.routeName=routeName;
160163
if(supportedActions[type]){
161-
_this.dispatch(createAction(supportedActions[type])({routeName:routeName,index:0,actions:actions,params:res}),type);
164+
_this.dispatch(createAction(supportedActions[type])({routeName:routeName,index:0,actions:actions,params:res}),type,res);
162165
}else if(type===ActionConst.POP_TO){
163166
var nextScene='';
164167
var newState=_this._state;
@@ -235,7 +238,7 @@ _this.run(ActionConst.REPLACE,routeName,[_reactNavigation.NavigationActions.navi
235238
routeName:routeName,
236239
params:res})]);
237240

238-
};var defaultSuccess=function defaultSuccess(){};var defaultFailure=function defaultFailure(){};(0,_mobx.autorunAsync)(function _callee(){var handler,res,_handler,success,failure,params,_res;return regeneratorRuntime.async(function _callee$(_context){while(1){switch(_context.prev=_context.next){case 0:_context.prev=0;if(_this.prevScene&&_this.currentScene!==_this.prevScene){handler=_this[_this.prevScene+_Util.OnExit];if(handler){try{res=handler();if(res instanceof Promise){res.then(defaultSuccess,defaultFailure);}}catch(e){console.error('Error during onExit handler:',e);}}}if(!(_this.currentScene&&_this.currentScene!==_this.prevScene&&_this.states[_this.currentScene])){_context.next=20;break;}_handler=_this[_this.currentScene+_Util.OnEnter];success=_this.states[_this.currentScene].success||defaultSuccess;failure=_this.states[_this.currentScene].failure||defaultFailure;if(!_handler){_context.next=20;break;}_context.prev=7;params=_this.currentState().params;console.log('RUN onEnter handler for state=',_this.currentScene,' params='+JSON.stringify(params));_context.next=12;return regeneratorRuntime.awrap(_handler(params));case 12:_res=_context.sent;if(_res){console.log('SUCCESS',_res);success(_res);}else{console.log('FAILURE NULL RES');failure();}_context.next=20;break;case 16:_context.prev=16;_context.t0=_context['catch'](7);console.log('FAILURE EXCEPTION',_context.t0);failure(_context.t0);case 20:_context.next=25;break;case 22:_context.prev=22;_context.t1=_context['catch'](0);console.error('Error handling:'+_context.t1);case 25:case'end':return _context.stop();}}},null,_this,[[0,22],[7,16]]);});}return NavigationStore;}(),(_descriptor=_applyDecoratedDescriptor(_class.prototype,'currentScene',[_mobx.observable],{enumerable:true,initializer:function initializer(){return'';}}),_descriptor2=_applyDecoratedDescriptor(_class.prototype,'prevScene',[_mobx.observable],{enumerable:true,initializer:function initializer(){return'';}}),_descriptor3=_applyDecoratedDescriptor(_class.prototype,'currentParams',[_mobx.observable],{enumerable:true,initializer:null}),_descriptor4=_applyDecoratedDescriptor(_class.prototype,'setState',[_mobx.action],{enumerable:true,initializer:function initializer(){var _this2=this;return function(newState,type){if(!newState){return;}var state=_this2.currentState(newState);if(type===ActionConst.JUMP&&state.routeName===_this2.currentScene){return;}_this2._state=newState;_this2.prevScene=_this2.currentScene;_this2.currentScene=state.routeName;_this2.currentParams=state.params;};}})),_class);exports.default=
241+
};var defaultSuccess=function defaultSuccess(){};var defaultFailure=function defaultFailure(){};(0,_mobx.autorunAsync)(function _callee(){var handler,res,_handler,success,failure,params,_res;return regeneratorRuntime.async(function _callee$(_context){while(1){switch(_context.prev=_context.next){case 0:_context.prev=0;if(_this.prevScene&&_this.currentScene!==_this.prevScene){handler=_this[_this.prevScene+_Util.OnExit];if(handler){try{res=handler();if(res instanceof Promise){res.then(defaultSuccess,defaultFailure);}}catch(e){console.error('Error during onExit handler:',e);}}}if(!(_this.currentScene&&_this.currentScene!==_this.prevScene&&_this.states[_this.currentScene])){_context.next=20;break;}_handler=_this[_this.currentScene+_Util.OnEnter];success=_this.states[_this.currentScene].success||defaultSuccess;failure=_this.states[_this.currentScene].failure||defaultFailure;if(!_handler){_context.next=20;break;}_context.prev=7;params=_this.currentState().params;console.log('RUN onEnter handler for state=',_this.currentScene,' params='+JSON.stringify(params));_context.next=12;return regeneratorRuntime.awrap(_handler(params));case 12:_res=_context.sent;if(_res){console.log('SUCCESS',_res);success(_res);}else{console.log('FAILURE NULL RES');failure();}_context.next=20;break;case 16:_context.prev=16;_context.t0=_context['catch'](7);console.log('FAILURE EXCEPTION',_context.t0);failure({error:_context.t0});case 20:_context.next=25;break;case 22:_context.prev=22;_context.t1=_context['catch'](0);console.error('Error handling:'+_context.t1);case 25:case'end':return _context.stop();}}},null,_this,[[0,22],[7,16]]);});}return NavigationStore;}(),(_descriptor=_applyDecoratedDescriptor(_class.prototype,'currentScene',[_mobx.observable],{enumerable:true,initializer:function initializer(){return'';}}),_descriptor2=_applyDecoratedDescriptor(_class.prototype,'prevScene',[_mobx.observable],{enumerable:true,initializer:function initializer(){return'';}}),_descriptor3=_applyDecoratedDescriptor(_class.prototype,'currentParams',[_mobx.observable],{enumerable:true,initializer:null}),_descriptor4=_applyDecoratedDescriptor(_class.prototype,'setState',[_mobx.action],{enumerable:true,initializer:function initializer(){var _this2=this;return function(newState,type,params){if(!newState){return;}var state=_this2.currentState(newState);if(type===ActionConst.JUMP&&state.routeName===_this2.currentScene){return;}_this2._state=newState;_this2.prevScene=_this2.currentScene;_this2.currentScene=state.routeName;_this2.currentParams=state.params;if(type===ActionConst.JUMP&&params){_this2.refresh(params);}};}})),_class);exports.default=
239242

240243

241244

src/Router.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,12 @@ function processScene(scene: Scene, inheritProps = {}, clones = [], wrapBy) {
244244
}
245245
delete props.children;
246246
if (success) {
247-
navigationStore.states[key].success = success instanceof Function ? success : args => { console.log(`Transition to state=${success}`); navigationStore[success](args); };
247+
navigationStore.states[key].success = success instanceof Function ?
248+
success : args => { console.log(`Transition to state=${success}`); navigationStore[success](args); };
248249
}
249250
if (failure) {
250-
navigationStore.states[key].failure = failure instanceof Function ? failure : args => { console.log(`Transition to state=${failure}`); navigationStore[failure](args); };
251+
navigationStore.states[key].failure = failure instanceof Function ?
252+
failure : args => { console.log(`Transition to state=${failure}`); navigationStore[failure](args); };
251253
}
252254
// console.log(`KEY ${key} DRAWER ${drawer} TABS ${tabs} WRAP ${wrap}`, JSON.stringify(commonProps));
253255
const screen = {
@@ -285,6 +287,9 @@ function processScene(scene: Scene, inheritProps = {}, clones = [], wrapBy) {
285287
}
286288
}
287289
const mode = modal ? 'modal' : 'card';
290+
if (navigator) {
291+
return navigator(res, { lazy, initialRouteName, initialRouteParams, order, ...commonProps, navigationOptions: createNavigationOptions(commonProps) });
292+
}
288293
if (lightbox) {
289294
return LightboxNavigator(res, { mode, initialRouteParams, initialRouteName, navigationOptions: createNavigationOptions(commonProps) });
290295
} else if (tabs) {
@@ -293,13 +298,11 @@ function processScene(scene: Scene, inheritProps = {}, clones = [], wrapBy) {
293298
} else if (drawer) {
294299
return DrawerNavigator(res, { initialRouteName, contentComponent, order, ...commonProps });
295300
}
296-
if (navigator) {
297-
return navigator(res, { lazy, initialRouteName, initialRouteParams, order, ...commonProps, navigationOptions: createNavigationOptions(commonProps) });
298-
}
299301
return StackNavigator(res, { mode, initialRouteParams, initialRouteName, ...commonProps, navigationOptions: createNavigationOptions(commonProps) });
300302
}
301303

302304
const Router = ({ createReducer, wrapBy = props => props, ...props }) => {
305+
assert(!Array.isArray(props.children), 'Router should contain only one scene, please wrap your scenes with Scene ');
303306
const scene: Scene = props.children;
304307
const AppNavigator = processScene(scene, props, [], wrapBy);
305308
navigationStore.router = AppNavigator.router;

src/navigationStore.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class NavigationStore {
118118
}
119119
} catch (e) {
120120
console.log('FAILURE EXCEPTION', e);
121-
failure(e);
121+
failure({ error: e });
122122
}
123123
}
124124
}
@@ -130,11 +130,11 @@ class NavigationStore {
130130

131131
nextState = (state, cmd) => (this.reducer ? this.reducer(state, cmd) : this._router.getStateForAction(cmd, state));
132132

133-
dispatch = (cmd, type) => {
134-
this.setState(this.nextState(this.state, cmd), type);
133+
dispatch = (cmd, type, params) => {
134+
this.setState(this.nextState(this.state, cmd), type, params);
135135
};
136136

137-
@action setState = (newState, type) => {
137+
@action setState = (newState, type, params) => {
138138
// don't allow null state
139139
if (!newState) {
140140
return;
@@ -147,6 +147,9 @@ class NavigationStore {
147147
this.prevScene = this.currentScene;
148148
this.currentScene = state.routeName;
149149
this.currentParams = state.params;
150+
if (type === ActionConst.JUMP && params) {
151+
this.refresh(params);
152+
}
150153
};
151154

152155
run = (type = ActionConst.PUSH, routeName, actions, ...params) => {
@@ -158,7 +161,7 @@ class NavigationStore {
158161
}
159162
res.routeName = routeName;
160163
if (supportedActions[type]) {
161-
this.dispatch(createAction(supportedActions[type])({ routeName, index: 0, actions, params: res }), type);
164+
this.dispatch(createAction(supportedActions[type])({ routeName, index: 0, actions, params: res }), type, res);
162165
} else if (type === ActionConst.POP_TO) {
163166
let nextScene = '';
164167
let newState = this._state;

0 commit comments

Comments
 (0)