ngxs-labs / async-storage-plugin

⏱ WIP: Async Storage Plugin
MIT License
34 stars 12 forks source link

Support States within the key parameter #157

Closed nvahalik closed 3 years ago

nvahalik commented 3 years ago

Currently the key option for the importing of the plugin only allows for State Tokens and strings. This causes an error (#123) if you pass in a StateModel.

It should be fairly easy to suppose a StateModel object since it has a name attribute which is the value that needs to be examined rather than the key itself.

for (const key of keys) {
    let val = nextState;
    if (key !== '@@STATE') {
        val = getValue(nextState, key);
    }
    try {
        this._asyncEngine.setItem(key, options.serialize(val));
    }
    catch (e) {
        console.error('Error ocurred while serializing the store value, value not updated.');
    }
}

Just check to see if the key is a StateModel and if it is, the "real name" is key.name rather than key.

marcjulian commented 3 years ago

@nvahalik do you mind creating a PR for this issue?

nvahalik commented 3 years ago

@marcjulian Sure. I'm working on it now.

Looks like this should already work based on how it's configured. It doesn't really make sense, but perhaps the issue here is a build problem and not a code problem?

When I use my locally built version of the plugin, using a StateModel works as expected. However, upon switching back to the latest release, it fails. The failure is in the transformKeyOption function:

My Transphormation Starts Today 2020-08-11 09-54-42

It reports that StateModel is undefined.

I'm unfamiliar enough with how these modules are built to know where to go from here.

nvahalik commented 3 years ago

The above was tested w/ Firefox 79 and the project is being used with the IonicAsyncStorageService driver (um, copy-pasted from the repo...) on an Ionic 4 project using Angular 7.

nvahalik commented 3 years ago

Ah, OK. I figured out what was going on. PR submitted! #158

nvahalik commented 3 years ago

Note that I was still having problems with using StateToken directly in the key list, but this appears to be some sort of AoT issue and not related to any bug in this plugin. I will close out this ticket.