Skip to content

Instantly share code, notes, and snippets.

View caroso1222's full-sized avatar

Carlos Roso caroso1222

View GitHub Profile
@caroso1222
caroso1222 / rxjs-log-usage.ts
Created January 9, 2019 05:20
rxjs-log-usage.ts
import { log } from '/path/to/utils/operators';
const source = of(1, 2, 3, 4);
source.pipe(
log(),
map(v => v ** 2),
log(),
filter(v => v > 5),
log()
@caroso1222
caroso1222 / rxjs-log-step-final.ts
Created January 9, 2019 05:12
rxjs-log-step-final.ts
export function log() {
return function logFn<T>(source: Observable<T>) {
const output = new Observable<T>(observer => {
const subscription = source.subscribe(
val => {
console.log(val);
observer.next(val);
},
err => {
console.error(err);
@caroso1222
caroso1222 / rxjs-log-step-5.ts
Created January 9, 2019 05:04
rxjs-log-step-5.ts
export function log() {
return function logFn<T>(source: Observable<T>) {
const output = new Observable<T>(observer => {
source.subscribe(
val => {
console.log(val);
observer.next(val);
},
err => {
console.error(err);
@caroso1222
caroso1222 / rxjs-log-step-4.ts
Created January 9, 2019 05:01
rxjs-log-step-4.ts
export function log() {
return function logFn<T>(source: Observable<T>) {
const output = new Observable<T>(observer => {
source.subscribe(
val => {
console.log(val);
observer.next(val);
},
err => console.error(err),
() => console.log('%ccomplete', 'color: green')
@caroso1222
caroso1222 / rxjs-log-step-3.ts
Created January 9, 2019 04:50
rxjs-log-step-3.ts
export function log() {
return function logFn<T>(source: Observable<T>) {
const output = new Observable<T>(observer => {
source.subscribe(
val => console.log(val),
err => console.error(err),
() => console.log('%ccomplete', 'color: green')
);
});
return output;
@caroso1222
caroso1222 / rxjs-log-step-2.ts
Created January 9, 2019 04:39
rxjs-log-step-2.ts
export function log() {
return function logFn<T>(source: Observable<T>) {
const output = new Observable<T>(observer => {});
return output;
};
}
@caroso1222
caroso1222 / rxjs-log-step-1.ts
Created January 9, 2019 04:27
rxjs-log-step-1.ts
export function log() {
return function logFn() {};
}
@caroso1222
caroso1222 / rxjs-log.ts
Created January 9, 2019 03:42
rxjs-log
const source = of('World');
// with tap
source.pipe(
tap(x => console.log(x))
).subscribe(x => {
this.doSomething(x);
});
// with map
@caroso1222
caroso1222 / rxjs-pausable-example.ts
Created December 12, 2018 04:46
rxjs-pausable-example
import { pausable, PausableObservable } from 'rxjs-pausable';
const source = interval(1000).pipe(
map(x => `Hello ${x}!`),
pauseable()
) as PausableObservable<string>;
source.subscribe(console.log);
setTimeout(() => source.pause(), 5000)
@caroso1222
caroso1222 / rxjs-pausable-operator.ts
Last active December 15, 2018 17:31
rxjs-pausable-operator
class PausableObservable<T> extends Observable<T> {
private pauser: BehaviorSubject<boolean>;
pause(){
this.pauser.next(true);
}
resume(){
this.pauser.next(false);
}