Repository: pmndrs/zustand
File: src/middleware.ts (TypeScript)

Code before:
    // a workaround to solve the issue of not storing rehydrated state in sync storage
    // the set(state) value would be later overridden with initial state by create()
    // to avoid this, we merge the state from localStorage into the initial state.
    let stateFromStorageInSync: S | undefined
    const postRehydrationCallback = onRehydrateStorage?.(get()) || undefined
    // bind is used to avoid `TypeError: Illegal invocation` error
    toThenable(storage.getItem.bind(storage))(name)
      .then((storageValue) => {
        if (storageValue) {
          return deserialize(storageValue)
        }
      })
      .then((deserializedStorageValue) => {
        if (deserializedStorageValue) {
          if (deserializedStorageValue.version !== version) {
            if (migrate) {
              return migrate(
                deserializedStorageValue.state,
                deserializedStorageValue.version
              )
            }
            console.error(
              `State loaded from storage couldn't be migrated since no migrate function was provided`
            )
          } else {
            stateFromStorageInSync = deserializedStorageValue.state
          }
        }
      })
      .then((migratedState) => {
        if (migratedState) {
          stateFromStorageInSync = migratedState as S
        }

        stateFromStorageInSync = merge(
          stateFromStorageInSync as S,
          configResult
        )

        set(stateFromStorageInSync as S)
        return setItem()
      })
      .then(() => {
        postRehydrationCallback?.(stateFromStorageInSync, undefined)
      })
      .catch((e: Error) => {
        postRehydrationCallback?.(undefined, e)
      })

    return stateFromStorageInSync || configResult
  }

Review comment (refactor):
```suggestion
            return deserializedStorageValue.state
```
Now, it seems we can simplify this and remove L375 if-statement.

Code after:
    // a workaround to solve the issue of not storing rehydrated state in sync storage
    // the set(state) value would be later overridden with initial state by create()
    // to avoid this, we merge the state from localStorage into the initial state.
    let stateFromStorage: S | undefined
    const postRehydrationCallback = onRehydrateStorage?.(get()) || undefined
    // bind is used to avoid `TypeError: Illegal invocation` error
    toThenable(storage.getItem.bind(storage))(name)
      .then((storageValue) => {
        if (storageValue) {
          return deserialize(storageValue)
        }
      })
      .then((deserializedStorageValue) => {
        if (deserializedStorageValue) {
          if (deserializedStorageValue.version !== version) {
            if (migrate) {
              return migrate(
                deserializedStorageValue.state,
                deserializedStorageValue.version
              )
            }
            console.error(
              `State loaded from storage couldn't be migrated since no migrate function was provided`
            )
          } else {
            return deserializedStorageValue.state
          }
        }
      })
      .then((migratedState) => {
        stateFromStorage = merge(migratedState as S, configResult)

        set(stateFromStorage as S, true)
        return setItem()
      })
      .then(() => {
        postRehydrationCallback?.(stateFromStorage, undefined)
      })
      .catch((e: Error) => {
        postRehydrationCallback?.(undefined, e)
      })

    return stateFromStorage || configResult
  }