[lib] Create useKeyserverCall
Summary:
issue: https://linear.app/comm/issue/ENG-4680/refactor-useservercall
We need to refactor useServerCall function (lib/utils/action-utils.js). Before there was only one keyserver, but as we move into multikeyserver world, an action might want to call one specific keyserver, or all of them. For each action we will thus have to define an object of type
export type KeyserverCall<Args: $ReadOnlyArray<mixed>, Return> = { +actionFunc: ActionFunc<Args, Return>, +config: | { +keyserverSelection: 'fanout' } | { +keyserverSelection: 'specific', +keyserverIDExtractor: (...Args) => string, }, };
Where "fanout" means that we call all keyservers, and keyserverIDExtractor is a function that can extract keyserver id from actions arguments.
We cannot completely remove useServerCall function yet, because we would first need to create such objects for all actions. So for now I'm creating useKeyserverCall alongside useServerCall (the name is supposed to be changed
anyway).
In useServerCall implementation memoization was done using a combination of _memoize, createSelector and useMemo. I will do this in the next diff, and provide an explanation of what it does exactly. Thats why
createBoundServerCallsSelector is omitted for now.
See test plan for example of usage
Test Plan:
Refactored serachMessages, and created a searchMessagesActionObj:
const searchMessages = ( callServerEndpoint: CallKeyserverEndpoint<[SearchMessagesRequest]>, ): ((request: SearchMessagesRequest) => Promise<SearchMessagesResponse>) => async request => { const response = await callServerEndpoint('search_messages', request, [ request, ]); return { messages: response.messages, endReached: response.endReached, }; }; const searchMessagesActionObj: KeyserverCall< [SearchMessagesRequest], SearchMessagesResponse, > = { actionFunc: searchMessages, config: { keyserverSelection: 'specific', keyserverIDExtractor: () => '256' }, };
in useSearchMessages fined callSearchMessages as:
const callSearchMessages = useKeyserverCall(searchMessagesActionObj);
and checked that searching messages works correctly.
Reviewers: michal, kamil, ginsu
Reviewed By: michal
Subscribers: ashoat, tomek
Differential Revision: https://phab.comm.dev/D9121