-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNgRx-Debug.service.ts
81 lines (72 loc) · 2.19 KB
/
NgRx-Debug.service.ts
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
// Found this on the web at home, its a debugger service for ngrx
//@NgModule({
// ... other imports and declarations
//providers: [NgRxDebugService],
// ...
//})
export class AppModule { }
import { Injectable, NgZone } from '@angular/core';
import { Store } from '@ngrx/store';
import { Actions } from '@ngrx/effects';
import { tap } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class NgRxDebugService {
private readonly ACTION_DISPATCH = 'NgRx Action Dispatched';
private readonly STATE_CHANGE = 'NgRx State Changed';
private readonly EFFECT_TRIGGERED = 'NgRx Effect Triggered';
constructor(
private store: Store,
private actions$: Actions,
private ngZone: NgZone
) {}
init() {
this.interceptActions();
this.interceptStateChanges();
this.interceptEffects();
}
private interceptActions() {
const originalDispatch = this.store.dispatch;
this.store.dispatch = (action: any) => {
console.group(this.ACTION_DISPATCH);
console.log('Action:', action);
console.groupEnd();
return originalDispatch.call(this.store, action);
};
}
private interceptStateChanges() {
let previousState: any;
this.store.subscribe(state => {
this.ngZone.run(() => {
console.group(this.STATE_CHANGE);
console.log('Previous State:', previousState);
console.log('Current State:', state);
console.log('Changes:', this.getStateChanges(previousState, state));
console.groupEnd();
previousState = { ...state };
});
});
}
private interceptEffects() {
this.actions$.pipe(
tap(action => {
console.group(this.EFFECT_TRIGGERED);
console.log('Effect Action:', action);
console.groupEnd();
})
).subscribe();
}
private getStateChanges(previous: any, current: any) {
if (!previous) return current;
return Object.keys(current).reduce((changes, key) => {
if (previous[key] !== current[key]) {
changes[key] = {
previous: previous[key],
current: current[key]
};
}
return changes;
}, {} as any);
}
}