使用ngrxOnRunEffects优化正在聆听的效果 - Ngrx Effects

时间:2018-06-14 01:36:56

标签: ngrx ngrx-effects

我正在尝试使用ngrxOnRunEffects来控制我班级中的效果应该何时开始收听。我在效果类中添加了ngrxOnRunEffects,如下所示: 这是一个非常简单的版本,除了返回resolvedEffects

之外什么都不做
ngrxOnRunEffects(resolvedEffects$: Observable<EffectNotification>) {
    return this.actions$
      .pipe(
        exhaustMap((res) => {

            return resolvedEffects$;
        })
      );
}

这是延迟加载模块的一部分。如果在模块导入中将此效果指定为forFeature(featureClass),则当我第一次访问该页面时,此类中的任何效果都会执行。但当我离开并返回页面时,此课程中的所有effects都开始工作。但是,如果使用forRoot(featureClass),则此effects中的所有class也会按预期首次发挥作用。

我使用ngrxOnRunEffects

实际尝试实现的目标

在我们的应用程序中,我们为每个路由器状态更改调度ROUTER_NAVIGATION操作。路由器状态名称的维护方式是每个页面都具有父状态,然后是子路由器状态。例如,特定网页的根目录为pageA,则子项为pageA.child1pageA.child2等等...对应每个网页,我们有一个Effects class 。所以我的想法是ngrxOnRunEffects会根据当前路线切换班级中的所有效果。基本上,只有当您处于特定视图时,效果才会被聆听。 以下是我试图实现的样本

return this.actions$.pipe(
          ofType(ROUTER_NAVIGATION),
          filter((action: RouterNavigationAction) => {
              const routerState: any = action.payload.routerState;
             // start listening when the user navigates to the current page
              return routerState.name.indexOf('pageA') > -1;
          }),
          exhaustMap(() => {
                return resolvedEffects$.pipe(
                  takeUntil(
                    this.actions$
                      .pipe(
                        ofType(ROUTER_NAVIGATION),
                        filter((action: RouterNavigationAction) => {
                            const routerState: any = action.payload.routerState;
                           // stop listening when the user navigates away from this page
                            return routerState.fromState.name === '' || routerState.fromState.name.indexOf('pageA') > -1;
                        })
                      )
                  )
                )
            }
          )
        );

这基本上是向优化迈出的一步,因此所有效果都不会监听each调度操作。 以上是否可以实现?此外,当effects用户navigates下次回来时,NgRx: 6.0.0-beta.3 Angular: 6.2.0 Node: 8.11.1 Affected Browsers: Chrome and possibly others Operating System: MacOS 10.13.5 会开始收听吗?

public class MainActivity extends AppCompatActivity {

String html;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    html = getTheHtml("http://www.posh24.se/kandisar");

    Log.i("html: ", "" + html);
    Log.i("Length", "" + html.length());
}

public class DownloadTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... urls) {
        // String result = null;
        URL url;
        HttpURLConnection connection = null;
        StringBuilder response = null;
        try {
            url = new URL(urls[0]);
            connection = (HttpURLConnection) url.openConnection();

            int responseCode = connection.getResponseCode();

            if(responseCode == HttpURLConnection.HTTP_OK){
                BufferedReader br = new BufferedReader((new InputStreamReader(connection.getInputStream())));
                response = new StringBuilder();

                String inputLine;

                while((inputLine = br.readLine())!= null) {
                    response.append(inputLine);
                    response.append("\n");
                }
                br.close();
            }
            return response.toString();
//                InputStream in = connection.getInputStream();
//                InputStreamReader reader = new InputStreamReader(in);
//                int data = reader.read();
//
//                while(data != -1){
//                    char current = (char) data;
//                    result += current;
//                    data = reader.read();
//                }
//                return result;
        } catch (Exception e) {
            e.printStackTrace();
            return "Failed";
        }
    }
}

public String getTheHtml (String url){
    String result = null;
    DownloadTask task = new DownloadTask();
    try {
        result = task.execute(url).get();
        return result;
    } catch (Exception e) {
        e.printStackTrace();
        return "Failed";
    }
}
}

0 个答案:

没有答案