I want to have my component update when a change is made to the session storage.
I’m using 5.4 and not TS. I think I need an observer, but there are two impediments.
One, the example is in TS and I’m new enough to the web landscape that I can’t convert it to simple JS.
Two, the documentation here and on other forums strongly discourage the use of observers.
I’m not sure how far this will get you because I haven’t dug into the details, but there’s an addon for autotracking local storage, so maybe you could use a similar approach?
Thanks. I was able to work through my issue using a different technique. I didn’t need a special observer. I just added a tracked variable to the service and then in the setter for the session storage, I set that variable, which in turn triggered the update I was looking for.
That worked so well and so easy, that for session storage of widespread interest, just setting a tracked value to represent the change will work.
However, if the session storage use should grow, and my simple solution becomes unwieldy, I will need something like you pointed out.
That addon does not support sessionStorage. But it may give you an idea how you could implement a decorator to synchronize a tracked property with the sessionStorage. You can remove some complexity as sessionStorage is not synchronized between tabs. So no need to listen for storage event in that case.