Skip to content

Commit

Permalink
Emitter added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Khasanov committed Jun 26, 2016
1 parent c5cc504 commit 43eb86b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/Geo.js
Expand Up @@ -7,6 +7,7 @@ var Geo = function() {
this.watchId = (null: ?number)
this.callback = null;
this.track = [];
this.objs = [];
};

Geo.prototype.setCallback = function(callback) {
Expand Down Expand Up @@ -40,4 +41,12 @@ Geo.prototype.stopListen = function() {
navigator.geolocation.clearWatch(this.watchId);
};

Geo.prototype.storeObj = function(obj) {
this.objs.push(obj);
}

Geo.prototype.getObjs = function() {
return this.objs;
}

module.exports = Geo;
7 changes: 7 additions & 0 deletions src/Globals.js
@@ -0,0 +1,7 @@
/**
* @providesModule Globals
*/
var EventEmitter = new(require('events').EventEmitter);
module.exports = {
EMITTER: EventEmitter,
};
22 changes: 22 additions & 0 deletions src/Mapa.js
Expand Up @@ -10,6 +10,7 @@ var {
Dimensions,
} = ReactNative;
var MapView = require('react-native-maps');
var GLOBAL = require('Globals');

var {width, height} = Dimensions.get('window');

Expand All @@ -30,7 +31,25 @@ var Liner = {
}
}

var makeChangeEventMixin = function(emitter){
return {
componentDidMount: function(){
emitter.addListener('change', this.__makeChangeEventMixin_handler_);
},
componentWillUnmount: function(){
emitter.removeListener('change', this.__makeChangeEventMixin_handler_);
},
__makeChangeEventMixin_handler_: function(payload){
this.setState(payload);
},
emitStateChange: function(payload){
emitter.emit('change', payload);
}
};
};

var Mapa = React.createClass({
mixins: [makeChangeEventMixin(GLOBAL.EMITTER)],
getInitialState: function () {
return {
line: [
Expand All @@ -42,6 +61,9 @@ var Mapa = React.createClass({
}
},

//DidMount: {listen to geo objects changed},
//DidUnmount: {stop listening},

onTrackIncrease: function(track) {
var lastPoint = track.slice(-1)[0];
if (!lastPoint) {
Expand Down
11 changes: 10 additions & 1 deletion src/index.js
Expand Up @@ -11,6 +11,7 @@ var {
var Btn = require('Btn');
var Geo = new(require('Geo'));
var Mapa = new(require('Mapa'));
var GLOBAL = require('Globals');

var Moov = React.createClass({
render: function() {
Expand Down Expand Up @@ -46,7 +47,15 @@ Geo.setCallback(function(track) {
}
}).then((obj) => {
if (obj) {
Mapa.setPointOfInterests(obj);
Geo.storeObj(obj);
GLOBAL.EMITTER.emit('change', {
line: [
{latitude: 39.73825, longitude: -124.4424},
{latitude: 39.74825, longitude: -124.4724},
{latitude: 39.75825, longitude: -124.4224},
{latitude: 39.76825, longitude: -124.4524},
]
})
}
});
Mapa.onTrackIncrease(track)
Expand Down

0 comments on commit 43eb86b

Please sign in to comment.