handle function application

Last updated: 2026-02-24T19:46:21.728Z

Metrics

LOC: 35 Complexity: 9 Params: 1

Signature

handle(message: ExtensionMessage): : Promise<unknown>

Source Code

async function handle(message: ExtensionMessage): Promise<unknown> {
  switch (message.type) {
    case "GET_FIELD_CACHE": {
      const payload = message.payload as { url?: string } | undefined;
      if (payload?.url) {
        return getFieldDetectionCacheForUrl(payload.url);
      }
      return getFieldDetectionCache();
    }

    case "SAVE_FIELD_CACHE": {
      const payload = parseSaveFieldCachePayload(message.payload);
      if (!payload) return { error: "Invalid payload for SAVE_FIELD_CACHE" };
      const cacheSettings = await getSettings();
      if (cacheSettings.cacheEnabled === false) {
        return { success: true, skipped: true };
      }
      return saveFieldDetectionCacheForUrl(payload.url, payload.fields);
    }

    case "DELETE_FIELD_CACHE": {
      const url = parseStringPayload(message.payload);
      if (!url) return { error: "Invalid payload for DELETE_FIELD_CACHE" };
      await deleteFieldDetectionCacheForUrl(url);
      return { success: true };
    }

    case "CLEAR_FIELD_CACHE":
      await clearFieldDetectionCache();
      return { success: true };

    default:
      return { error: `Unhandled type in cacheHandler: ${message.type}` };
  }
}

Dependencies (Outgoing)

graph LR handle["handle"] MessageHandler["MessageHandler"] ExtensionMessage["ExtensionMessage"] MessageType["MessageType"] getFieldDetectionCache["getFieldDetectionCache"] getFieldDetectionCacheForUrl["getFieldDetectionCacheForUrl"] saveFieldDetectionCacheForUrl["saveFieldDetectionCacheForUrl"] deleteFieldDetectionCacheForUrl["deleteFieldDetectionCacheForUrl"] clearFieldDetectionCache["clearFieldDetectionCache"] getSettings["getSettings"] parseSaveFieldCachePayload["parseSaveFieldCachePayload"] parseStringPayload["parseStringPayload"] handle -->|uses| MessageHandler handle -->|uses| ExtensionMessage handle -->|uses| MessageType handle -->|uses| getFieldDetectionCache handle -->|uses| getFieldDetectionCacheForUrl handle -->|uses| saveFieldDetectionCacheForUrl handle -->|uses| deleteFieldDetectionCacheForUrl handle -->|uses| clearFieldDetectionCache handle -->|uses| getSettings handle -->|uses| parseSaveFieldCachePayload handle -->|uses| parseStringPayload style handle fill:#dbeafe,stroke:#2563eb,stroke-width:2px click handle "8b929c06f047f82c.html" click MessageHandler "ab334f3bc9eb52d7.html" click ExtensionMessage "c70465261f6c12b8.html" click MessageType "2ef3f4e4b1044d26.html" click getFieldDetectionCache "8f2869c3e935ff86.html" click getFieldDetectionCacheForUrl "df99bb80d6972b4a.html" click saveFieldDetectionCacheForUrl "5f18074b51936498.html" click deleteFieldDetectionCacheForUrl "209937c4802c2009.html" click clearFieldDetectionCache "592635f6ea1eb5e3.html" click getSettings "99db54620b94a08b.html" click parseSaveFieldCachePayload "f7ed34bd25803201.html" click parseStringPayload "a410bc3f13516882.html"

No incoming dependencies.