blob: c6995787d7ce65707e0daffbdb30630712f695bd (
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
27
|
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);
console.log('current state', this.subject);
}
public unsubscribe() {
this.subject.unsubscribe();
}
}
|