zoukankan      html  css  js  c++  java
  • [RxJS] Encapsulate complex imperative logic in a simple observable

    Two very important features of the Observable primitive is that they can be activated by subscribing to them and disposed off when we are not interested in them anymore. All good observables need to clean up after themselves when they are disposed off. We took advantage of this when we wrote the observable that switches to showing the loader when the count is positive, and stops listening to it when the count is zero. In this lesson, we will learn how to encapsulate all the complex imperative logic for showing and hiding the spinner, inside a simple observable that can be plugged into any stream.

      shouldShowSpinner.pipe(
        switchMap(() => showSpinner.pipe(takeUntil(shouldHideSpinner)))
      )

    Inside Observable constructor, you can put some side effect code. For example, when 'showSpinner' get triggered, we will show the spinner.

    When the 'showSpinner' observable complete, it will call the unsubscribe function we return to hide spinner.

    const showSpinner = new Observable(() => {
      const loadingSpinnerPromise = initLoadingSpinner();
    
      loadingSpinnerPromise.then(spinner => {
        spinner.show()
      })
    
      return () => {
        loadingSpinnerPromise.then(spinner => {
          spinner.hide();
        })
      }
    });
  • 相关阅读:
    Android Studio Gradle 添加.so 支持文件
    poj 3270 更换使用
    linux通过使用mail发送电子邮件
    php 上传文件 $_FILES['']['type']的值
    浅谈Base64编码
    expect实现ssh自动登录
    C++ 多源码文件简单组织
    linux下修改hostid
    SQLite/嵌入式数据库
    类内数组声明,“类外”指定大小
  • 原文地址:https://www.cnblogs.com/Answer1215/p/12660529.html
Copyright © 2011-2022 走看看