Implementing a Generic StateManagierService that can handle any type, allowing users to receive new state and data upon state change. However, something seems to be missing.
export class StateManagierService<T> {
private _state$: BehaviorSubject<T>;
protected constructor (initialState: T) {
console.log("initialState",initialState)
this._state$ = new BehaviorSubject(initialState);
}
get state$ (): Observable<T> {
return this._state$.asObservable();
}
get state (): T {
return this._state$.getValue();
}
changeState (nextState: T): void {
if(this.state === nextState || nextState === null) return;
this._state$.next(nextState);
}
}
There are various States that users can define
export enum state1{
A =1,
B
}
The Wraper Class will create objects and allow for additional data or properties to be added
export class Wrap {
constructor(stateId: state1, data?:any){
console.log("stateId", stateId, data)
}
}
@Injectable({
providedIn: 'root'
})
export class serviceImpl extends StateManagierService<Wrap> {
constructor(){
super(new Wrap(state1.A, 'text1')); // Expecting a Wrap object to be set but it is not happening
}
}
A Test Service to verify functionality
class hello {
constructor(private serviceImpl: serviceImpl){
}
one(){
this.serviceImpl.state$.subscribe(res=>{
console.log('res', res) // Expecting a Wrap object, what could I be doing wrong?
})
this.serviceImpl.changeState(new Wrap(state1.B, 'text23'))
}
}