zoukankan      html  css  js  c++  java
  • [RxJS] Loading Spinner with busyDelayMs & busyMinDurationMs api

    const { merge, timer, Subject, combineLatest } = require("rxjs");
    const {
      tap,
      mapTo,
      skip,
      filter,
      startWith,
      takeUntil,
      switchMap,
    } = require("rxjs/operators");
    
    const DELAY = 500;
    const MIN_DURATION = 1200;
    
    const taskStartSubject = new Subject();
    const taskStart = taskStartSubject.asObservable();
    const taskEndSubject = new Subject();
    const taskEnd = taskEndSubject.asObservable();
    const busyDelayTimer = timer(DELAY);
    const busyMinDurationTimer = timer(MIN_DURATION + DELAY);
    const busyDelayTimerStart = taskStart.pipe(switchMap(() => busyDelayTimer));
    const busyDelayTimerEnd = busyDelayTimerStart.pipe(takeUntil(taskEnd));
    const emitOnTaskEnd = taskEnd.pipe(mapTo(1));
    const emitOnDelayTimerEnd = busyDelayTimerEnd.pipe(mapTo(-1));
    const emitOnMinDurationEnd = busyMinDurationTimer.pipe(mapTo(-1));
    
    ////////////// start/////////////////
    
    const raceBetweenTaskAndDelay = combineLatest([
      emitOnTaskEnd.pipe(startWith(null)),
      emitOnDelayTimerEnd.pipe(startWith(null)),
    ]).pipe(skip(1));
    const taskEndBeforeDelay = raceBetweenTaskAndDelay.pipe(
      filter(([taskEndFirst, timerEndFirst]) => {
        return taskEndFirst === 1 && timerEndFirst === null;
      })
    );
    const shouldNotShowSpinner = taskEndBeforeDelay.pipe(mapTo(false));
    const taskEndAfterTimeout = raceBetweenTaskAndDelay.pipe(
      filter(([taskEndFirst, timerEndFirst]) => {
        return taskEndFirst === null && timerEndFirst === -1;
      })
    );
    const shouldShowSpinner = taskEndAfterTimeout.pipe(mapTo(true));
    const showSpinner = shouldShowSpinner.pipe(
      tap(() => {
        console.timeLog("spinner");
        console.log("show");
      })
    );
    
    /////////////// end ///////////////
    
    const raceBetweenTaskAndMinDuration = combineLatest([
      emitOnTaskEnd.pipe(startWith(null)),
      emitOnMinDurationEnd.pipe(startWith(null)),
    ]).pipe(skip(1));
    const hideSpinnerUntilMinDurationEnd = raceBetweenTaskAndMinDuration.pipe(
      filter(([taskEndFirst, timerEndFirst]) => {
        return taskEndFirst === 1 && timerEndFirst === null;
      })
    );
    const hideSpinnerAfterTimerAndTaskEnd = raceBetweenTaskAndMinDuration.pipe(
      filter(([taskEndFirst, timerEndFirst]) => {
        return taskEndFirst === 1 && timerEndFirst === -1;
      })
    );
    const hideSpinner = merge(
      // case 1: should not show spinner at all
      shouldNotShowSpinner,
      // case 2: task end, but wait until min duration timer ends
      combineLatest([hideSpinnerUntilMinDurationEnd, emitOnMinDurationEnd]),
      // case 3: task takes a long time, wait unitl its end
      hideSpinnerAfterTimerAndTaskEnd
    ).pipe(
      tap(() => {
        console.timeLog("spinner");
        console.log("hide");
      })
    );
    const Spinner = showSpinner.pipe(takeUntil(hideSpinner));
    
    // test
    Spinner.subscribe();
    
    console.log("task start");
    console.time("spinner");
    taskStartSubject.next();
    
    // Case 1: Should not show spinner
    
    setTimeout(() => {
      taskEndSubject.next();
    }, 50);
    
    // Case 2: Should show spinner when busyMinDurationMs end
    /*
    setTimeout(() => {
      taskEndSubject.next();
    }, 600);
    */
    // Case 3: Should show spinner until task ends
    /*
    setTimeout(() => {
      taskEndSubject.next();
    }, 2000);
    */
  • 相关阅读:
    nexus搭建maven私服及私服jar包上传和下载
    Java数据结构和算法(八)——递归
    postgresql数据库的 to_date 和 to_timestamp 将 字符串转换为时间格式
    postgreSql的字段名称是小写的这个一定要注意
    Mybatis异常There is no getter for property named 'XXX' in 'class java.lang.String'
    postgresql 获取所有表名、字段名、字段类型、注释
    克隆指定的分支和切换分支
    git branch不显示本地分支的问题
    git中Please enter a commit message to explain why this merge is necessary.
    企业微信开发步骤 1.拿到企业的id,在我的企业,拖到最下面拿到企业id 2.拿到SECRET,这个secret只有先创建应用才会产生出来
  • 原文地址:https://www.cnblogs.com/Answer1215/p/12908045.html
Copyright © 2011-2022 走看看