Skip to content

Issue: Requests stay pending when using MMKV dispatcher storage #125

@fa11erX

Description

@fa11erX

Describe the bug
First, thank you for this amazing library! I recently discovered Hyper Fetch and I’m really surprised it’s not more popular given its quality.
However, I’m facing an issue when adding custom storage (MMKV) to the fetchDispatcher and submitDispatcher.

When I configure them with my storage implementation, all requests stay in pending state and are never sent.
If I remove the storage from fetchDispatcher/submitDispatcher (or use an empty new Dispatcher({})), everything works fine.

To Reproduce
Steps to reproduce the behavior:

  1. Configure a client with MMKV storage adapters for cache, fetchDispatcher, and submitDispatcher.
  2. Create a simple GET request with .createRequest().
  3. Try to execute the request.
  4. The request never leaves pending state.

Expected behavior
The request should be executed and return a response, just like when no storage is provided.

Code Snippets

Client configuration:

export const $hyperFetch = new Client({
  url: ENV.EXPO_PUBLIC_API_URL,
  appManager: () =>
    new AppManager({
      initiallyOnline: () =>
        NetInfo.fetch().then((state) => !!state.isConnected),
      onlineEvent: (setOnline) => {
        return NetInfo.addEventListener((state) => {
          setOnline(!!state.isConnected);
        });
      },
    }),
  cache: () => new Cache({ storage: cacheStorage }),
  fetchDispatcher: () => new Dispatcher({ storage: fetchDispatcherStorage }),
  submitDispatcher: () => new Dispatcher({ storage: submitDispatcherStorage }),
})
  .onAuth((request) => {
    const cookie = authClient.getCookie();
    return request.setHeaders({
      ...request.headers,
      Cookie: `${cookie}`,
    });
  })
  .setDebug(true)
  .addPlugin(DevtoolsPlugin({ appName: "Infinity-dev" }));

Storage

export const fetchDispatcherStorage: DispatcherStorageType = {
  set: (key, data) => fetchDispatcherMMKV.set(key, JSON.stringify(data)),
  get: (key) => {
    const value = fetchDispatcherMMKV.getString(key);
    return value ? JSON.parse(value) : undefined;
  },
  keys: () => fetchDispatcherMMKV.getAllKeys(),
  entries: function* () {
    const keys = fetchDispatcherMMKV.getAllKeys();
    for (const key of keys) {
      yield [key, JSON.parse(fetchDispatcherMMKV.getString(key) || "null")] as [string, any];
    }
  },
  delete: (key) => fetchDispatcherMMKV.delete(key),
  clear: () => fetchDispatcherMMKV.clearAll(),
};

Example request:

const getNotifications = $hyperFetch.createRequest()({
  endpoint: "/notifications",
  method: "GET",
  cacheKey: "GET_NOTIFICATIONS",
});

Desktop (please complete the following information):

  • OS: macOS
  • Browser: (not applicable, React Native)
  • Version: latest

Smartphone (please complete the following information):

  • Device: iPhone Simulator
  • OS: iOS (Expo / React Native)
  • Browser: N/A

Additional context

  • If I remove the fetchDispatcher and submitDispatcher storage, everything works.
  • If I set them as new Dispatcher({}), everything works.
  • With MMKV storage, requests remain in pending state forever.
  • Environment: Expo + React Native.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions