ipn: make LocalBackend.loadStateWithLock easier to follow.

Signed-off-by: David Anderson <dave@natulte.net>
pull/10/head
David Anderson 2020-02-13 16:38:36 -08:00 committed by Dave Anderson
parent 96fd8b4230
commit 5f45b7c765
1 changed files with 31 additions and 26 deletions

View File

@ -359,14 +359,27 @@ func (b *LocalBackend) popBrowserAuthNow() {
} }
func (b *LocalBackend) loadStateWithLock(key StateKey, prefs *Prefs) error { func (b *LocalBackend) loadStateWithLock(key StateKey, prefs *Prefs) error {
switch { if prefs == nil && key == "" {
case key != "" && prefs != nil: panic("state key and prefs are both unset")
}
if key == "" {
// Frontend fully owns the state, we just need to obey it.
b.logf("Using frontend prefs")
b.prefs = *prefs
b.stateKey = ""
return nil
}
if prefs != nil {
// Backend owns the state, but frontend is trying to migrate
// state into the backend.
b.logf("Importing frontend prefs into backend store") b.logf("Importing frontend prefs into backend store")
if err := b.store.WriteState(key, prefs.ToBytes()); err != nil { if err := b.store.WriteState(key, prefs.ToBytes()); err != nil {
return fmt.Errorf("store.WriteState: %v", err) return fmt.Errorf("store.WriteState: %v", err)
} }
fallthrough }
case key != "":
b.logf("Using backend prefs") b.logf("Using backend prefs")
bs, err := b.store.ReadState(key) bs, err := b.store.ReadState(key)
if err != nil { if err != nil {
@ -383,14 +396,6 @@ func (b *LocalBackend) loadStateWithLock(key StateKey, prefs *Prefs) error {
return fmt.Errorf("PrefsFromBytes: %v", err) return fmt.Errorf("PrefsFromBytes: %v", err)
} }
b.stateKey = key b.stateKey = key
case prefs != nil:
b.logf("Using frontend prefs")
b.prefs = *prefs
b.stateKey = ""
default:
panic("state key and prefs are unset")
}
return nil return nil
} }