ngxs / store

🚀 NGXS - State Management for Angular
http://ngxs.io
MIT License
3.54k stars 402 forks source link

Initial State is not flushed to the storage on the first page load (localStorage used) #185

Closed un33k closed 6 years ago

un33k commented 6 years ago

[x] Bug

Versions

    "@ngxs/devtools-plugin": "^2.0.0-rc.24",
    "@ngxs/logger-plugin": "^2.0.0-rc.24",
    "@ngxs/storage-plugin": "^2.0.0-rc.24",
    "@ngxs/store": "^2.0.0-rc.22",
    "@angular/core": "5.2.9",

Repro steps

Observed behavior

Initial page load doesn't create an initial state in localStorage.

Desired behavior

  // storage.plugin.ts
  if (isInitAction) {
      for (const key of keys) {
        let val = this._engine.getItem(key);

        if (val !== 'undefined' && val !== null) {
          val = options.deserialize(val);

          if (key !== '@@STATE') {
            state = setValue(state, key, val);
          } else {
            state = val;
          }
        } else {
           this._engine.setItem(key, options.serialize(state));     // <- here (possible solution)
        }
      }
    }
amcdnl commented 6 years ago

Is this still an issue?

un33k commented 6 years ago

@amcdnl tested it, handles it gracefully now.