• [RxJS] Append values to a stream using startWith and endWith (countdown$)


    // begin lesson code
    import { interval, fromEvent, of } from 'rxjs';
    import { scan, mapTo, takeWhile, takeUntil, tap, startWith, endWith } from 'rxjs/operators';
    
    /*
     * CODE FOR FOR FIRST SECTION OF LESSON
     */
    const numbers$ = of(1,2,3);
    
    numbers$.pipe(
      /*
       * startWith lets you seed a stream with 1:M values.
       * On subscription, these values will be emitted
       * immediately, followed by any future values from
       * the source.
       */
      startWith('a', 'b', 'c'),
      /*
       * You can also end a stream with any number of values,
       * emitted on completion.
       */
      endWith('d', 'e', 'f')
    ).subscribe(console.log);
    /*
    a
    b
    c
    1
    2
    3
    d
    e
    f
    */
    const countdown = document.getElementById('countdown');
    const message = document.getElementById('message');
    const abortButton = document.getElementById('abort');
    
    // streams
    const counter$ = interval(1000);
    const abort$ = fromEvent(abortButton, 'click');
    
    const COUNTDOWN_FROM = 10;
    
    counter$
      .pipe(
        mapTo(-1),
        scan((accumulator, current) => {
          return accumulator + current;
        }, COUNTDOWN_FROM),
        takeWhile(value => value >= 0),
        takeUntil(abort$),
        /*
         * With startWith, we can seed the stream with
         * the starting countdown value.
         */
        startWith(COUNTDOWN_FROM)
      )
      .subscribe((value: any) => {
        countdown.innerHTML = value;
        if (!value) {
          message.innerHTML = 'Liftoff!';
        }
      });
  • 相关阅读:
    if
    C#
    C#
    C#
    .net 5.0
    .net 5.0
    .net 5.0
    设计模式
    GAN网络中采用导向滤波的论文
    pytorch~多loss的选择
  • 原文地址:https://www.cnblogs.com/Answer1215/p/16816410.html
Copyright © 2020-2023  润新知