summaryrefslogtreecommitdiffstats
path: root/cds-ui/designer-client/src/app/common/core/stores/Store.ts
blob: 0be804270c00a6914c503d25e6d8ba9e6a25fe5a (plain)
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
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);
    }

    public unsubscribe() {
        this.subject.unsubscribe();
    }

}