9 Commits
Author SHA1 Message Date
Pete Miller 5cc0360a83 [createInterfaceApi] typescript allows mutations to have array params (#36581)
components/ai_chat/resources/page/api/ai_chat_api.ts had trouble with typescript intellisense due to the process file mutation functions now having an array as their first param. Apparently due to contravariance checks
2026-05-20 14:17:36 -07:00
Pete Miller f3e633afeb [createInterfaceApi] fix prefetch being cancelled when data update happens (#35350)
* [createInterfaceApi] fix prefetch being cancelled when data update happens

Instead of simply cancelling the prefetch query, we cancel and then queue the fetch again

- fix test where conversationHistory was getting reset
2026-04-07 20:13:35 -07:00
Pete Miller 00e7084fd0 [createInterfaceApi] Supplement partial updates with placeholder data when no real data has been received (#34961)
This especially effected "state" queries which are only ever updated via update calls and not fetch results
2026-03-24 22:31:59 +01:00
Pete Miller a3aec9de82 [AI Chat] createInterfaceAPI used for child conversation entries frame (#32892)
* [AI Chat] child frame createInterfaceAPI use
This migrates the child conversation content frame to use createInterfaceApi instead of a singleton API class and a manual context data store. Context properties are marked as deprecated to facilitate a slower move.

This continues the migration of AI Chat to use createInterfaceApi, reducing the amount of overhead to call, cache and mock mojo calls and events.
Previously:
- createInterfaceApi - a browser proxy store #29601
- Introduce createInterfaceApi infrastructure to AI Chat #33688
- [AI Chat] Migrate AIChatContext to createInterfaceApi pattern #33690
- [AI Chat] Migrate ConversationContext to createInterfaceApi pattern #33689

Fixes an issue in createInterfaceApi exposed by a flakey test:
* create interface api cancels any in-progress fetches of an endpoint that is being updated manually
I noticed this can happen in tests when the test provides data but the endpoint is setup to prefetch. The prefetch resolves after the manual api.myEndpoint.update call.
2026-03-05 21:20:01 +00:00
Pete Miller 4729a5afcb [createInterfaceApi] fix subscribers not getting invoked when multiple events with the same arguments are called (#33967) 2026-02-17 13:44:37 -08:00
Pete Miller d922370d6b Introduce createInterfaceApi infrastructure to AI Chat (#33688)
* Add new API infrastructure for AI Chat

Add new createInterfaceApi-based API files that will be used to migrate
AI Chat from the old PageAPI singleton pattern. This commit adds the
infrastructure without changing any consumers:

- ai_chat_api.ts: New createAIChatApi() factory for service/UI handler
- conversation_api.ts: New createConversationApi() factory
- bind_conversation.ts: Conversation binding helpers
- mock_interfaces.ts: Mock implementations for testing

The existing api/index.ts (old getAPI() singleton) remains unchanged
so all current consumers continue to work.
2026-02-12 23:41:45 +00:00
Pete Miller c9860a26d9 [createInterfaceApi] fix event data hook useCurrentMyEvent() (#33865)
[createInterfaceApi] fix event hook useCurrentMyEvent()
2026-02-12 21:32:25 +00:00
Pete Miller eb2c3bb8bc [createInterfaceApi] Use globally-unique key always, remove key and queryClient config (#33864) 2026-02-11 23:09:22 +00:00
Pete Miller 3e5b3073de createInterfaceApi - a browser proxy store (#29601)
* createInterfaceApi - a browser proxy store

A way to generate a state store browser proxy from any remote API, designed for mojom (or extension) APIs

The intention is to create something requiring minimal configuration and remove often repeated patterns to do with WebUI <-> Browser communication and
- storing and caching fetch results across UI components
- event listening to data updates and upadting local cache
- passing through actions to UI components
- mocking data for unit tests and storybook

This is not intended to replace all local state management. The intention is to manage only the data that is owned by a remote (e.g. the browser) and present it to the UI. Any local-only state should still be managed by the relevant framework (i.e. React, Lit, or Svelte) or a separate state manager.
However, this does add another relevant utility:

Used to send an instance of an api to a tree of React components. However, since it accepts any function it can also conveniently be used to store global state for that tree, using React hooks (`useState`, `useMemo`) or derived state from values retrieved from the API.
React Context is not that performant, as the whole tree will re-render when any pieces of state change, so this should be used sparingly. Instead, hooks for each individual endpoint should be used via the API instance which is passed down via Context. The API instance itself won't change and cause re-renders, but the individual hooks will cause subscriptions to be made to each piece of data.

A helper to generate endpoints from an interface. Prevents having to re-declare the mojom function signature as Typescript can deduce it from the generated mojom JS.

A helper to mark an interface function as an 'event'. Prevents having to re-declare the mojom function signature as Typescript can deduce it from the generated mojom JS.

See `components/common/api/readme.md` for examples.
2026-02-08 15:54:40 -08:00