mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-03-26 07:04:05 +08:00
[Navigator]: Add a getter to get the current route that is focused from the navigation context.
This commit is contained in:
@@ -23,25 +23,34 @@
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* @providesModule NavigationContext
|
||||
* @flow
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
var NavigationEventEmitter = require('NavigationEventEmitter');
|
||||
var emptyFunction = require('emptyFunction');
|
||||
|
||||
type EventSubscription = {
|
||||
remove: Function
|
||||
};
|
||||
var emptyFunction = require('emptyFunction');
|
||||
var invariant = require('invariant');
|
||||
|
||||
import type * as NavigationEvent from 'NavigationEvent';
|
||||
import type * as EventSubscription from 'EventSubscription';
|
||||
|
||||
/**
|
||||
* Class that contains the info and methods for app navigation.
|
||||
*/
|
||||
class NavigationContext {
|
||||
_eventEmitter: ?NavigationEventEmitter;
|
||||
_currentRoute: any;
|
||||
|
||||
constructor() {
|
||||
this._eventEmitter = new NavigationEventEmitter(this);
|
||||
this._currentRoute = null;
|
||||
this.addListener('didfocus', this._onDidFocus, this);
|
||||
}
|
||||
|
||||
// TODO: @flow does not like this getter. Will add @flow check back once
|
||||
// getter/setter is supported.
|
||||
get currentRoute(): any {
|
||||
return this._currentRoute;
|
||||
}
|
||||
|
||||
addListener(
|
||||
@@ -64,13 +73,23 @@ class NavigationContext {
|
||||
}
|
||||
}
|
||||
|
||||
dispose() {
|
||||
dispose(): void {
|
||||
var emitter = this._eventEmitter;
|
||||
if (emitter) {
|
||||
// clean up everything.
|
||||
emitter.removeAllListeners();
|
||||
this._eventEmitter = null;
|
||||
this._currentRoute = null;
|
||||
}
|
||||
}
|
||||
|
||||
_onDidFocus(event: NavigationEvent): void {
|
||||
invariant(
|
||||
event.data && event.data.hasOwnProperty('route'),
|
||||
'didfocus event should provide route'
|
||||
);
|
||||
this._currentRoute = event.data.route;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = NavigationContext;
|
||||
|
||||
Reference in New Issue
Block a user