Skip to content

Instantly share code, notes, and snippets.

@fireflysemantics
Created January 11, 2020 07:48
Show Gist options
  • Save fireflysemantics/465c1dd13ea7e541bab4625536cb650f to your computer and use it in GitHub Desktop.
Save fireflysemantics/465c1dd13ea7e541bab4625536cb650f to your computer and use it in GitHub Desktop.
import { Injectable, OnDestroy } from '@angular/core'
import { OStore} from '@fireflysemantics/slice'
import { Observable } from 'rxjs'
import { MenuItems} from './model'
import { untilDestroyed } from 'ngx-take-until-destroy'
export const ACTIVE_MENU = "ACTIVE_MENU"
export const AUTHENTICATED = "AUTHENTICATED"
export const SAVED = "SAVED"
@Injectable({providedIn: 'root'})
export class AppStateService implements OnDestroy {
public authenticated = false
public os:OStore = new OStore()
public activeMenu$:Observable<string>
public authenticated$:Observable<boolean>
public saved$:Observable<boolean>
constructor() {
this.os.post(ACTIVE_MENU, MenuItems.NONE)
this.os.post(AUTHENTICATED, false)
this.os.post(SAVED, false)
this.activeMenu$ = this.os.observe(ACTIVE_MENU)
this.authenticated$ = this.os.observe(AUTHENTICATED)
this.saved$ = this.os.observe(SAVED)
//this.activeMenu$.pipe(untilDestroyed(this)).subscribe(m=>console.log(m))
//this.authenticated$.pipe(untilDestroyed(this)).subscribe(auth=>console.log(`Authenticated: ${auth}`))
}
login() {
this.authenticated = true;
this.os.put(AUTHENTICATED,this.authenticated)
}
logout() {
this.authenticated = false;
this.os.put(AUTHENTICATED,this.authenticated)
}
save() {
this.os.put(SAVED, true)
}
ngOnDestroy() {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment