[Navigator]: Add a getter to get the current route that is focused from the navigation context.

This commit is contained in:
Hedger Wang
2015-06-19 15:55:08 -07:00
parent 0fec355076
commit 93e98da908
2 changed files with 76 additions and 6 deletions

View File

@@ -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;