Skip to content

Return Observable of getItem() #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
todian opened this issue Nov 27, 2017 · 2 comments
Closed

Return Observable of getItem() #16

todian opened this issue Nov 27, 2017 · 2 comments

Comments

@todian
Copy link

todian commented Nov 27, 2017

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.

@cyrilletuzi
Copy link
Owner

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.

@todian
Copy link
Author

todian commented Nov 27, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants