-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
devtools.ts
198 lines (169 loc) · 5.58 KB
/
devtools.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { Injectable, Inject, ErrorHandler, OnDestroy } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import {
Action,
ActionReducer,
ActionsSubject,
INITIAL_STATE,
ReducerObservable,
ScannedActionsSubject,
StateObservable,
} from '@ngrx/store';
import {
merge,
Observable,
Observer,
queueScheduler,
ReplaySubject,
Subscription,
} from 'rxjs';
import { map, observeOn, scan, skip, withLatestFrom } from 'rxjs/operators';
import * as Actions from './actions';
import { STORE_DEVTOOLS_CONFIG, StoreDevtoolsConfig } from './config';
import { DevtoolsExtension } from './extension';
import { LiftedState, liftInitialState, liftReducerWith } from './reducer';
import {
liftAction,
unliftState,
shouldFilterActions,
filterLiftedState,
} from './utils';
import { DevtoolsDispatcher } from './devtools-dispatcher';
import { PERFORM_ACTION } from './actions';
@Injectable()
export class StoreDevtools implements Observer<any>, OnDestroy {
private liftedStateSubscription: Subscription;
private extensionStartSubscription: Subscription;
public dispatcher: ActionsSubject;
public liftedState: Observable<LiftedState>;
public state: StateObservable;
constructor(
dispatcher: DevtoolsDispatcher,
actions$: ActionsSubject,
reducers$: ReducerObservable,
extension: DevtoolsExtension,
scannedActions: ScannedActionsSubject,
errorHandler: ErrorHandler,
@Inject(INITIAL_STATE) initialState: any,
@Inject(STORE_DEVTOOLS_CONFIG) config: StoreDevtoolsConfig
) {
const liftedInitialState = liftInitialState(initialState, config.monitor);
const liftReducer = liftReducerWith(
initialState,
liftedInitialState,
errorHandler,
config.monitor,
config
);
const liftedAction$ = merge(
merge(actions$.asObservable().pipe(skip(1)), extension.actions$).pipe(
map(liftAction)
),
dispatcher,
extension.liftedActions$
).pipe(observeOn(queueScheduler));
const liftedReducer$ = reducers$.pipe(map(liftReducer));
const liftedStateSubject = new ReplaySubject<LiftedState>(1);
this.liftedStateSubscription = liftedAction$
.pipe(
withLatestFrom(liftedReducer$),
scan<
[any, ActionReducer<LiftedState, Actions.All>],
{
state: LiftedState;
action: any;
}
>(
({ state: liftedState }, [action, reducer]) => {
let reducedLiftedState = reducer(liftedState, action);
// On full state update
// If we have actions filters, we must filter completely our lifted state to be sync with the extension
if (action.type !== PERFORM_ACTION && shouldFilterActions(config)) {
reducedLiftedState = filterLiftedState(
reducedLiftedState,
config.predicate,
config.actionsSafelist,
config.actionsBlocklist
);
}
// Extension should be sent the sanitized lifted state
extension.notify(action, reducedLiftedState);
return { state: reducedLiftedState, action };
},
{ state: liftedInitialState, action: null as any }
)
)
.subscribe(({ state, action }) => {
liftedStateSubject.next(state);
if (action.type === Actions.PERFORM_ACTION) {
const unliftedAction = (action as Actions.PerformAction).action;
scannedActions.next(unliftedAction);
}
});
this.extensionStartSubscription = extension.start$.subscribe(() => {
this.refresh();
});
const liftedState$ =
liftedStateSubject.asObservable() as Observable<LiftedState>;
const state$ = liftedState$.pipe(map(unliftState)) as StateObservable;
Object.defineProperty(state$, 'state', {
value: toSignal(state$, { manualCleanup: true, requireSync: true }),
});
this.dispatcher = dispatcher;
this.liftedState = liftedState$;
this.state = state$;
}
ngOnDestroy(): void {
// Even though the store devtools plugin is recommended to be
// used only in development mode, it can still cause a memory leak
// in microfrontend applications that are being created and destroyed
// multiple times during development. This results in excessive memory
// consumption, as it prevents entire apps from being garbage collected.
this.liftedStateSubscription.unsubscribe();
this.extensionStartSubscription.unsubscribe();
}
dispatch(action: Action) {
this.dispatcher.next(action);
}
next(action: any) {
this.dispatcher.next(action);
}
error(error: any) {}
complete() {}
performAction(action: any) {
this.dispatch(new Actions.PerformAction(action, +Date.now()));
}
refresh() {
this.dispatch(new Actions.Refresh());
}
reset() {
this.dispatch(new Actions.Reset(+Date.now()));
}
rollback() {
this.dispatch(new Actions.Rollback(+Date.now()));
}
commit() {
this.dispatch(new Actions.Commit(+Date.now()));
}
sweep() {
this.dispatch(new Actions.Sweep());
}
toggleAction(id: number) {
this.dispatch(new Actions.ToggleAction(id));
}
jumpToAction(actionId: number) {
this.dispatch(new Actions.JumpToAction(actionId));
}
jumpToState(index: number) {
this.dispatch(new Actions.JumpToState(index));
}
importState(nextLiftedState: any) {
this.dispatch(new Actions.ImportState(nextLiftedState));
}
lockChanges(status: boolean) {
this.dispatch(new Actions.LockChanges(status));
}
pauseRecording(status: boolean) {
this.dispatch(new Actions.PauseRecording(status));
}
}