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

Code before:
    }
    if (blacklist) {
      blacklist.forEach((key) => delete state[key])
    }

    if (storage) {
      toThenable(serialize)({ state, version }).then((serializedValue) => {
        storage?.setItem(name, serializedValue)
      })
    }
  }

  const savedSetState = api.setState

  api.setState = (state, replace) => {
    savedSetState(state, replace)
    void setItem()
  }

  // rehydrate initial state with existing stored state

  // 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.
  // TODO: find a better solution for this
  let stateFromStorage: any
  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) {
            stateFromStorage = migrate(
              deserializedStorageValue.state,
              deserializedStorageValue.version
            )
            return stateFromStorage
          }
        } else {
          stateFromStorage = deserializedStorageValue.state
          set(deserializedStorageValue.state)
        }
      }
    })
    .then((migratedState) => {

Review comment (suggestion):
Okay, I understand why this is required. I think it's the right solution.
Let's rename it like:
```suggestion
  let stateFromStorageInSync: S | undefined
```

The type `S` should probably be `Partial<S>` not only here, but all over the place. Maybe we should tackle it in a separate PR.

Code after:
  }

  const thenableSerialize = toThenable(serialize)

  const setItem = (): Thenable<void> => {
    const state = { ...get() }

    if (whitelist) {
      ;(Object.keys(state) as (keyof S)[]).forEach((key) => {
        !whitelist.includes(key) && delete state[key]
      })
    }
    if (blacklist) {
      blacklist.forEach((key) => delete state[key])
    }

    let errorInSync: Error | undefined
    const thenable = thenableSerialize({ state, version })
      .then((serializedValue) =>
        (storage as StateStorage).setItem(name, serializedValue)
      )
      .catch((e) => {
        errorInSync = e
      })
    if (errorInSync) {
      throw errorInSync
    }
    return thenable
  }

  const savedSetState = api.setState

  api.setState = (state, replace) => {
    savedSetState(state, replace)
    void setItem()
  }

  // rehydrate initial state with existing stored state

  // 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)
      }
    })