在这个类中,我使用初始化的 bool 状态来产生 Mobx.autorun 执行。否则 'this' 没有被完全赋值并导致错误。是否有另一种/更清洁的方法来做到这一点?
class GameMaster{
private _initialized:boolean = false;
private _store:IDomainStore;
private _moveDisposer:Lambda;
/**
*
* @param store - client or server store
*/
constructor(store:IDomainStore){
this._store = store;
console.log(this._store);
//todo abstract services to decouple client device from GameMaster because it is also used on the server.
this._moveDisposer = autorun(()=>{
// prevent firing in the constructor
if(this._initialized) {
this.present(
<IIntent>{
fromId: 'GeoLocation.service',
toIds: [Meteor.userId()],
wish: actions.playerActions.types.CHANGE_PLAYER_GEO_COORDINATES,
data: [System.GeolocationService.coordinates.lng, System.GeolocationService.coordinates.lat]
});
}
});
this._initialized = true;
}
public present(intent:IIntent):boolean{
...
}
...
}
这是我在另一个文件中观察到的:
@observable coordinates = {
lng:0,
lat:0
};