aboutsummaryrefslogtreecommitdiffstats
path: root/cds-ui/designer-client/src/app/common/core/stores/Store.ts
blob: 1d5b0afc1be2d09b6b6c9e60d6584f1bc6c2424b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import {Observable, BehaviorSubject} from 'rxjs';
import { Injectable } from '@angular/core';

export class Store<T> {
    state$: Observable<T>;
    private subject: BehaviorSubject<T>;

    protected constructor(initialState: T) {
        this.subject = new BehaviorSubject(initialState);
        this.state$ = this.subject.asObservable();
    }

    get state(): T {
        return this.subject.getValue();
    }

    protected setState(nextState: T): void {
        console.log('setting state', this.subject);
        this.subject.next(nextState);
    }

}