mirror of
https://github.com/grafana/grafana.git
synced 2025-07-31 09:02:13 +08:00

* Externalize api and remove dependency cycles * fix tests * Update comment * Split the state observable creation in ScopesService * Make the feature flag guard more explicit * Change reduce to map
26 lines
766 B
TypeScript
26 lines
766 B
TypeScript
import { BehaviorSubject, Observable, pairwise, Subscription } from 'rxjs';
|
|
|
|
export abstract class ScopesServiceBase<T> {
|
|
private _state: BehaviorSubject<T>;
|
|
|
|
protected constructor(initialState: T) {
|
|
this._state = new BehaviorSubject<T>(Object.freeze(initialState));
|
|
}
|
|
|
|
public get state(): T {
|
|
return this._state.getValue();
|
|
}
|
|
|
|
public get stateObservable(): Observable<T> {
|
|
return this._state.asObservable();
|
|
}
|
|
|
|
public subscribeToState = (cb: (newState: T, prevState: T) => void): Subscription => {
|
|
return this._state.pipe(pairwise()).subscribe(([prevState, newState]) => cb(newState, prevState));
|
|
};
|
|
|
|
protected updateState = (newState: Partial<T>) => {
|
|
this._state.next(Object.freeze({ ...this.state, ...newState }));
|
|
};
|
|
}
|