Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | 158x 158x 158x 279x 279x 279x 279x 279x 279x 279x 279x 279x | import { HttpErrorResponse } from '@angular/common/http';
import { makeAutoObservable, observable, reaction, toJS } from 'mobx';
import { catchError, Observable, throwError } from 'rxjs';
import { tap } from 'rxjs/operators';
import { Ref } from '../model/ref';
import { printError } from '../util/http';
export type progress = (msg?: string, p?: number) => void;
export class EventBus {
event = '';
ref?: Ref = {} as any;
repost?: Ref = {} as any;
errors: string[] = [];
progressMessages: string[] = [];
progressNum = 0;
progressDen = 0;
constructor() {
makeAutoObservable(this, {
ref: observable.ref,
errors: observable.shallow,
runAndReload: false,
runAndRefresh: false,
catchError$: false,
isRef: false,
});
reaction(() => this.event, () => console.log('🚌️ Event Bus:', this.event, this.event === 'error' ? toJS(this.errors) : '', toJS(this.ref)));
}
fire(event: string, ref?: Ref, repost?: Ref) {
this.event = event;
this.ref = ref;
this.repost = repost;
}
fireError(errors: string[], ref?: Ref) {
this.event = 'error';
this.errors = [...errors];
if (ref) {
this.ref = ref;
}
}
/**
* Download latest revision of ref from the server and then trigger the
* 'refresh' event.
*/
reload(ref?: Ref) {
this.event = 'reload';
if (ref) {
this.ref = ref;
}
this.repost = undefined;
}
/**
* Notify latest version of ref is not available.
*/
refresh(ref?: Ref) {
this.event = 'refresh';
if (ref) {
this.ref = ref;
}
this.repost = undefined;
}
/**
* Clear event bus state for sending duplicate events.
*/
reset() {
this.event = '';
this.ref = undefined;
this.repost = undefined;
}
runAndReload(o: Observable<any>, ref?: Ref) {
return this.runAndReload$(o, ref).subscribe();
}
runAndReload$(o: Observable<any>, ref?: Ref) {
return this.catchError$(o, ref).pipe(tap(() => this.reload(ref)));
}
runAndRefresh(o: Observable<any>, ref?: Ref) {
this.runAndRefresh$(o, ref).subscribe();
}
runAndRefresh$(o: Observable<any>, ref?: Ref) {
return this.catchError$(o, ref).pipe(tap(() => this.refresh(ref)));
}
catchError$(o: Observable<any>, ref?: Ref) {
return o.pipe(
catchError((err: HttpErrorResponse) => {
this.fireError(printError(err), ref);
return throwError(() => err);
})
);
}
isRef(r: Ref) {
return this.ref?.url === r.url && this.ref.origin === r.origin;
}
clearProgress(steps = 0) {
if (!steps || !this.progressDen || this.progressNum >= this.progressDen) {
this.progressMessages = [];
this.progressNum = 0;
this.progressDen = steps;
} else {
this.progressDen += steps;
}
}
msg(msg: string) {
this.progressMessages.push(msg)
}
steps(steps = 1) {
this.progressDen += steps;
}
progress(msg?: string, steps = 1) {
if (msg) this.progressMessages.push(msg);
if (steps) this.progressNum += steps;
}
}
|