cyrilletuzi/angular-async-local-storage

Return Observable of getItem()

todian opened this issue · 2 comments

Hello I wrap AsyncLocalStorage into another service.

@Injectable()
export class MyService {

    constructor(protected _localStorage: AsyncLocalStorage) { }

    public getUser() {
        return this._localStorage.getItem<User>('user');
    }

    public setUser(user: User) {
        this._localStorage.setItem('user', user)
            .subscribe(() => { },
            error => console.log);
    }
}

Inject service in the component and subscribe to method getUser which use getItem method..

export class AppComponent implements OnInit{

constructor(private _myService: MyService){
}

ngOnInit(): void {
    this._myService.getUser().subscribe(user=>console.log(user))
}

}

getUser produce only one value null when I add new user to via setUser method to local storage in the AppComponent subscribe doesn’t produce value but when I subscribe directly to _localStorage.getItem()
it works.

I use Angular 5 and Typescript 2.4.2.

If I understand your problem correctly, you would like getItem to send a new value each time local storage is updated. Is that so ?

If so, it's not how this lib works. getItem just return one value, when it is requested. And that is normal : local storage won't change by itself, it is your app which will change it. So if you want to track changes, it's the role of your app to do so. And it's not the local storage itself you should watch, it's just the value in your app.

See issue #4 for an example.

Yes exactly. Now is clear. Thank you for very quick answer.