matcheek
matcheek

Reputation: 5147

Rewrite Rxjs 4 in Rxjs 6

How do I rewrite this Rxjs 4 snippet in Rxjs 6?

const click$ = Rx.Observable.fromEvent($('.btn'), 'click').share();

click$
  .scan((a, b) => a + 1, 0)
  .bufferTime(4000, null, 3)
  .filter(buffer => buffer.length > 0)
  .concatMap(buffer => Rx.Observable.of(buffer).delay(1000))
  .timestamp()
  .subscribe(console.log);

https://jsbin.com/pacicubeci/1/edit?js,console,output

Here's my attempt:

import { $ } from "jquery";
import { Observable } from "rxjs";
import { bufferTime, filter, scan, concatMap, timestamp } from "rxjs/operators";

const click$ = Rx.Observable.fromEvent($(".btn"), "click").share();

click$
  .scan((a, b) => a + 1, 0)
  .bufferTime(1000, null, 3)
  .filter(buffer => buffer.length > 0)
  .concatMap(buffer => Rx.Observable.of(buffer).delay(1000))
  .timestamp()
  .subscribe(console.log);

https://angularjs-g8w7z9.stackblitz.io

Upvotes: 0

Views: 57

Answers (1)

Bojan Kogoj
Bojan Kogoj

Reputation: 5649

I hope this is what you're looking for

const click$ = fromEvent(document, "click").pipe(share());

click$
  .pipe(
    scan((a, b) => a + 1, 0),
    bufferTime(1000, null, 3),
    filter(buffer => buffer.length > 0),
    concatMap(buffer => of(buffer).pipe(delay(1000))),
    timestamp()
  )
  .subscribe(console.log);

Operators are now in a pipe.

https://stackblitz.com/edit/angularjs-qczayg?file=app.js

Upvotes: 1

Related Questions