Skip to content

Instantly share code, notes, and snippets.

@jhades
Last active August 3, 2021 05:07
Show Gist options
  • Save jhades/160352a73dba15726b1498576a362e55 to your computer and use it in GitHub Desktop.
Save jhades/160352a73dba15726b1498576a362e55 to your computer and use it in GitHub Desktop.
Functional Reactive Programming for Angular Developers - RxJs and Observables
const obs = interval(1000).pipe(take(5));
const obs = interval(1000)
.pipe(
take(5),
tap(i => console.log(i) )
);
const obs = interval(1000)
.pipe(
take(5),
tap(i => console.log("obs value "+ i) )
);
obs.subscribe(value => console.log("observer 1 received " + value));
obs.subscribe(value => console.log("observer 2 received " + value));
const obs = interval(500)
.pipe(
take(5),
map(i => 2 * i )
);
<form [ngForm]="form" (ngSubmit)="onSubmit()">
<p>
<label>First Name:</label>
<input type="text" ngControl="firstName">
</p>
</form>
this.form.valueChanges
.pipe(
map((value) => {
value.firstName = value.firstName.toUpperCase();
return value;
}),
filter((value) => this.form.valid)
)
.subscribe(validValue => ...);
const obs = interval(500).pipe(take(5));
var reduced = obs.pipe(
reduce((state, value) => state + value , 0)
);
reduced.subscribe(total => console.log("total =" + total));
const obs = interval(500).pipe(take(5));
var scanObs = obs.pipe(
scan((state, value) => state + value , 0)
);
scanObs.subscribe(total => console.log(total));
const obs = interval(500)
.pipe(
take(5),
tap(i => console.log("obs value "+ i)),
shareReplay()
);
obs.subscribe(value => console.log("observer 1 received " + value));
obs.subscribe(value => console.log("observer 2 received " + value));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment