I’m working on an app where I’d like to populate the store with some values from a service in app.ts, so I don’t have to do this request for every view that uses the data.
However, I find that I’m having troubles reading values from the store, as it seems to be undefined at the time I’m trying to access it.
Here a rough outline of the scenario:
From App.ts I dispatch two actions; an async action (I’ve tried multiple lifecycle methods to dispatch from here) and a regular function action.
Looking at redux devtools the store has been populated.
When doing a simple console.log from any consuming component, it appears that the async part of the state has not been added yet. (The slice of the state that is generated by the non-async action is populated at the time of accessing it)
Now, I’ve tried both an async / await approach before dispatching; const result = await apiCall(); this.store.dispatch('TheAction', result);
as well as; this.apiCall().then(result => { this.store.dispatch('TheAction', result); }
Both of them yielding the same empty result when trying to access it from another viewmodel than app.ts.
I even tried implementing the async action, as described in the docs, but it yields the same results (or lack thereof…)
Should this be the case, or am I simply doing something wrong here?
You want to have access in the constructor but are using the connecTo decorator with a setup inside attached. Since this hook gets triggered far after the constructor call the local variable is not yet set. You would have to create a manual subscription, as described in the docs, inside the constructor and would have access once the subscription callback is executed.
Tried your approach, and I still experience the issue where functions are being run before the store has been populated with the data. (See home.ts in particular)
Two issues:
Home is the default route, which is still not able to access the data in time
for 1. this can’t work as you’re trying to access the data right away. The subscription is what it is, a subscription, means it will get called once the state is there. Since your home is called by the router, a option would be to move this.functionDependingOnState(); into the activate hook of home. Depending on whether your state is async or not this should resolve by then.
2. same applies here. Just subscribing doesn’t necessarily mean that the callback is executed right away.
The Aurelia Store plugin is working with RxJS under the hood which promotes a reactive system. Reactive means you setup listeners to react to a change. That of course means that you can’t really do sync code, which is a good thing. By keeping your system reactive you tell your system what to do when X happens instead of imperatively telling what to do right now as that has to be repeated over and over again.
If you really need to synchronously access the current state, which again is a bad practice, but nevertheless you can do it as this:
...
constructor(private store: Store<State>) {
const currentState = (this.store as any)._state.getValue();
// use the current state
}
this will get you the currently active store. Again don’t do this if not absolutely necessary and in your specific case it would just point to the old state.
Will see if I can refactor the dependant logic to run inside another lifecycle hook instead, as I need to populate the store with data from the API first.
Do you do the prepopulation as part of the initial state or a separate API request and then firing a new dispatch? Maybe to make it more clear … do you request/obtain the data before aurelia.start or after the app is already launched?