zaterdag 4 maart 2023

rxjs

Rxjs is a great library for declarative programming. The main advantage of declarative programming, in my opinion, is the fact that related code is together glued in one place.

For instance, I wanted to buffer incoming messages and have them emitted every second. That was easy in Rxjs.

Angular:

public originalStream$: Subject<SpecificObj> = new Subject<SpecificObj>();
public bufferedStream$: Observable<SpecificObj[]> = this.originalStream$.pipe(bufferTime(500), filter(arr => arr.length >0));

in the init:
this.bufferedStream$.subscribe((values: SpecificObj[]) => {
for (let newObj of values) {
}
});
in the code: originalStream$.next(obj); Example pure javascript: let container = $(document.body);
function log(val) {
container.append(`<div>${val}</div>`);
}

const { of, interval } = rxjs;

//const stream = of(1,2,3,4);
const stream = interval(1000);

stream.subscribe(val => log(val));

https://dev.to/hssanbzlm/building-autocomplete-feature-rxjs-with-vanilla-javascript-c7
https://rxjs.dev/guide/overview
https://eliteionic.com/tutorials/imperative-vs-declarative-programming-with-rxjs-search-filter/#a-declarative-search-filter
https://lodash.com/
https://johnlindquist.com/rxjs-pipes/

Geen opmerkingen:

Een reactie posten