Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何生成Mobx.autorun以防止类构造函数中触发?_Javascript_Typescript_Mobx - Fatal编程技术网

Javascript 如何生成Mobx.autorun以防止类构造函数中触发?

Javascript 如何生成Mobx.autorun以防止类构造函数中触发?,javascript,typescript,mobx,Javascript,Typescript,Mobx,在这个类中,我使用一个初始化的bool状态来产生Mobx.autorun执行。Otherwize“this”未完全分配,会导致错误。有没有其他/更干净的方法可以做到这一点 class GameMaster{ private _initialized:boolean = false; private _store:IDomainStore; private _moveDisposer:Lambda; /** * * @param store - client or

在这个类中,我使用一个初始化的bool状态来产生Mobx.autorun执行。Otherwize“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{
      ...
    }
 ...
}

我认为这是解决问题的好方法,但是初始化字段也应该是可见的。否则更改
\u initialized
不会导致自动运行重新运行

但是在这种情况下,我不确定初始化变量在您的中究竟实现了什么,因为在自动运行之后的第一个语句是将initialized设置为true

所以我不完全确定您要实现什么:将自动运行/
present
调用推迟到构造函数末尾,还是跳过第一个
present
调用

更新的答案

如果您想防止副作用(在这种情况下发送
present
),有一个简单的模式。线索是确保你计算出副作用所需的任何值,但不要自己触发副作用。在你的例子中,这看起来

constructor(store:IDomainStore){
    let firstRun = true;
    this._moveDisposer = autorun(()=>{
        // make sure all information is tracked
        const presenceInfo = <IIntent>{
            fromId: 'GeoLocation.service',
            toIds: [Meteor.userId()],
            wish: actions.playerActions.types.CHANGE_PLAYER_GEO_COORDINATES,
            data: [System.GeolocationService.coordinates.lng, System.GeolocationService.coordinates.lat]
        }
        // but prevent the side effect in the first run
        if(!firstRun) {
            this.present(presenceInfo);
        } else {
            firstRun = false;
        }
    });
  }
构造函数(存储:idomnstore){
让firstRun=true;
这个._moveDisposer=自动运行(()=>{
//确保跟踪所有信息
const presenceInfo={
fromId:'地理位置.服务',
toIds:[Meteor.userId()],
愿望:actions.playerActions.types.CHANGE\u PLAYER\u GEO\u坐标,
数据:[System.GeolocationService.coordinates.lng,System.GeolocationService.coordinates.lat]
}
//但是在第一次跑步时要防止副作用
如果(!firstRun){
此。当前(presenceInfo);
}否则{
firstRun=false;
}
});
}

(请注意,将来可能不再需要该标志,因为存在一个将param
firstRun
传递给自动运行函数的现有标志)。

您能否指出哪些变量被修饰为可观察变量?我希望自动运行在您当前的设置中不会触发。并且在构建过程中可能确实触发了
坐标
,或者出于初始化目的自动运行了一次?我希望防止自动运行在没有明显变化的情况下触发其功能。因为present()使用了一些在构造函数调用结束前不可用的实例变量。即使其依赖项没有更改,自动运行行为也会运行。不是吗?我的意思是在初始化时。更正:如果我将
\u initialized
设置为可观察对象,我将在相同的问题中运行,因为自动运行将在构造函数调用结束前触发。我只需等待服务发送另一个坐标更改。smart!我很接近。谢谢。尽管我想我一直在寻找第一轮的提案。
constructor(store:IDomainStore){
    let firstRun = true;
    this._moveDisposer = autorun(()=>{
        // make sure all information is tracked
        const presenceInfo = <IIntent>{
            fromId: 'GeoLocation.service',
            toIds: [Meteor.userId()],
            wish: actions.playerActions.types.CHANGE_PLAYER_GEO_COORDINATES,
            data: [System.GeolocationService.coordinates.lng, System.GeolocationService.coordinates.lat]
        }
        // but prevent the side effect in the first run
        if(!firstRun) {
            this.present(presenceInfo);
        } else {
            firstRun = false;
        }
    });
  }