Skip to content

Instantly share code, notes, and snippets.

View NyaGarcia's full-sized avatar
🐈

Nya NyaGarcia

🐈
View GitHub Profile
@NyaGarcia
NyaGarcia / generate-object.ts
Created April 23, 2020 14:37
Creating an Observable with generate(), passing an object as parameter
const evenNumber$ = generate({
initialState: 2,
condition: x => x <= 10,
iterate: x => x + 2
});
evenNumber$.subscribe(number => console.log(number));
// Output: 2, 4, 6, 8, 10
@NyaGarcia
NyaGarcia / generate-endless-loop.ts
Created April 23, 2020 14:45
Creating an endless loop with generate()
const endles$ = generate({ initialState: 1, iterate: x => +1 });
@NyaGarcia
NyaGarcia / of.ts
Created April 23, 2020 14:55
Creating several Observables with of()
import { of } from "rxjs";
// Creating an Observable of numbers
const number$ = of(1, 2, 3, 4, 5);
// Creating an Observable of strings
const pokemon$ = of("Squirtle", "Charmander", "Bulbasur");
// Creating an Observable of arrays
const fruit$ = of(["Strawberry", "Cherry"], ["Lemon", "Orange"]);
@NyaGarcia
NyaGarcia / of-subscription.ts
Created April 23, 2020 15:07
Subscribing to Observables created with of()
number$.subscribe(number => console.log(number));
// Output: 1 2 3 4 5
pokemon$.subscribe(pokemon => console.log(pokemon));
// Output: Squirtle Charmander Bulbasur
fruit$.subscribe(fruit => console.log(fruit));
// Output: ["Strawberry", "Cherry"] ["Lemon", "Cherry"]
iceCream$.subscribe(iceCream => console.log(iceCream));
@NyaGarcia
NyaGarcia / from.ts
Last active April 23, 2020 18:22
Creating an Observable with from()
// Creating an Observable from an array of strings
const fruit$ = from(["Strawberry", "Cherry", "Blackberry"]);
// Creating an Observable from a Map object
const pokemon$ = from(
new Map([
["Squirtle", "Water"],
["Charmander", "Fire"],
["Bulbasur", "Grass"]
])
@NyaGarcia
NyaGarcia / from-subscription.ts
Created April 23, 2020 18:28
Subscribing to Observables created with from()
letter$.subscribe(letter => console.log(letter));
// Output: "R","x","J","S"," ","i","s"," ","c","o","o","l"
fruit$.subscribe(console.log);
// Output: "Strawberry", "Cherry", "Blackberry"
pokemon$.subscribe(console.log);
// Output: ["Squirtle", "Water"], ["Charmander", "Fire"], ["Bulbasur", "Grass"]
promise$.subscribe(promise => console.log(promise));
@NyaGarcia
NyaGarcia / of-from-comparation.ts
Last active April 23, 2020 18:53
Comparation of Observables created with from() and of()
const fruitsOf$ = of(["Strawberry", "Cherry"], ["Lemon", "Orange"]);
const fruitsFrom$ = from(["Strawberry", "Cherry", "Blackberry"]);
fruitOf$.subscribe(fruit => console.log(fruit));
// Output: ["Strawberry", "Cherry"] ["Lemon", "Cherry"]
fruitFrom$.subscribe(console.log);
// Output: "Strawberry", "Cherry", "Blackberry"
@NyaGarcia
NyaGarcia / fromEvent-subscription.ts
Created April 24, 2020 12:47
Subscribing to Observables created with fromEvent()
click$.subscribe(click => console.log(click));
// Output: MouseEvent {isTrusted: true}
keyPressed$.subscribe(console.log);
// Output: KeyboardEvent {isTrusted: true}
scroll$.subscribe(scroll => console.log(scroll));
// Output: UIEvent {isTrusted: true}
copie$.subscribe(console.log);
@NyaGarcia
NyaGarcia / interval.ts
Created April 24, 2020 13:41
Creating Observables with interval()
import { interval } from "rxjs";
// Observable will emit incremental numbers, one every second (1000ms)
const number$ = interval(1000);
// We didn't pass the interval size parameter, so default value is 0ms
const superFastNumber$ = interval();
number$.subscribe(console.log);
@NyaGarcia
NyaGarcia / timer.ts
Created April 24, 2020 14:13
Creating Observables with timer()
import { timer } from "rxjs";
// Observable will emit one value (0) after 2s and complete
const onlyOneValue$ = timer(2000);
// Observable will wait 5s, then start emitting values every second
const number$ = timer(5000, 1000);
onlyOneValue$.subscribe(console.log);