Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F32222913
D9143.1765205817.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
7 KB
Referenced Files
None
Subscribers
None
D9143.1765205817.diff
View Options
diff --git a/lib/utils/keyserver-call.js b/lib/utils/keyserver-call.js
--- a/lib/utils/keyserver-call.js
+++ b/lib/utils/keyserver-call.js
@@ -1,14 +1,22 @@
// @flow
+import _memoize from 'lodash/memoize.js';
import * as React from 'react';
import { useDispatch } from 'react-redux';
+import { createSelector } from 'reselect';
import { bindCookieAndUtilsIntoCallServerEndpoint } from './action-utils.js';
-import type { CallServerEndpointOptions } from './call-server-endpoint.js';
+import type { BindServerCallsParams } from './action-utils.js';
+import type {
+ CallServerEndpoint,
+ CallServerEndpointOptions,
+} from './call-server-endpoint.js';
import { useSelector } from './redux-utils.js';
+import type { PlatformDetails } from '../types/device-types.js';
import type { Endpoint } from '../types/endpoints.js';
import type { KeyserverInfo } from '../types/keyserver-types.js';
import type { Dispatch } from '../types/redux-types.js';
+import type { ConnectionStatus } from '../types/socket-types.js';
import type { CurrentUserInfo } from '../types/user-types.js';
export type CallKeyserverEndpoint<Args: $ReadOnlyArray<mixed>> = (
@@ -34,74 +42,130 @@
+config: KeyserverCallConfig<Args>,
};
-export type ExtractAndBindCookieAndUtilsParams<
- Args: $ReadOnlyArray<mixed>,
- Return,
-> = {
+// _memoize memoizes the function by caching the result.
+// The first argument of the memoized function is used as the map cache key.
+const baseCreateBoundServerCallsSelector = (
+ // eslint-disable-next-line no-unused-vars
+ keyserverID: string,
+): (BindServerCallsParams => CallServerEndpoint) =>
+ createSelector(
+ (state: BindServerCallsParams) => state.dispatch,
+ (state: BindServerCallsParams) => state.cookie,
+ (state: BindServerCallsParams) => state.urlPrefix,
+ (state: BindServerCallsParams) => state.sessionID,
+ (state: BindServerCallsParams) => state.currentUserInfo,
+ (state: BindServerCallsParams) => state.connectionStatus,
+ (state: BindServerCallsParams) => state.lastCommunicatedPlatformDetails,
+ (
+ dispatch: Dispatch,
+ cookie: ?string,
+ urlPrefix: string,
+ sessionID: ?string,
+ currentUserInfo: ?CurrentUserInfo,
+ connectionStatus: ConnectionStatus,
+ lastCommunicatedPlatformDetails: ?PlatformDetails,
+ ) =>
+ bindCookieAndUtilsIntoCallServerEndpoint({
+ dispatch,
+ cookie,
+ urlPrefix,
+ sessionID,
+ currentUserInfo,
+ connectionStatus,
+ lastCommunicatedPlatformDetails,
+ }),
+ );
+
+type CreateBoundServerCallsSelectorType =
+ string => BindServerCallsParams => CallServerEndpoint;
+const createBoundServerCallsSelector: CreateBoundServerCallsSelectorType =
+ (_memoize(baseCreateBoundServerCallsSelector): any);
+
+export type BindKeyserverCallParams = {
+dispatch: Dispatch,
+currentUserInfo: ?CurrentUserInfo,
+keyserverInfos: { +[keyserverID: string]: KeyserverInfo },
- +keyserverCall: KeyserverCall<Args, Return>,
};
-function getCallKeyserverEndpoint<Args: $ReadOnlyArray<mixed>, Return>(
- params: ExtractAndBindCookieAndUtilsParams<Args, Return>,
-): CallKeyserverEndpoint<Args> {
- const { dispatch, keyserverInfos, currentUserInfo, keyserverCall } = params;
-
- const { config: serverCallConfig } = keyserverCall;
+export type GetCallKeyserverEndpointParams<Args: $ReadOnlyArray<mixed>> = {
+ ...BindKeyserverCallParams,
+ +keyserverCallConfig: KeyserverCallConfig<Args>,
+};
- return (
- endpoint: Endpoint,
- data: Object,
- args: Args,
- options?: ?CallServerEndpointOptions,
+const bindCallKeyserverEndpointSelector = createSelector(
+ (state: BindKeyserverCallParams) => state.dispatch,
+ (state: BindKeyserverCallParams) => state.currentUserInfo,
+ (state: BindKeyserverCallParams) => state.keyserverInfos,
+ (
+ dispatch: Dispatch,
+ currentUserInfo: ?CurrentUserInfo,
+ keyserverInfos: { +[keyserverID: string]: KeyserverInfo },
) => {
- // TODO
- if (serverCallConfig.keyserverSelection === 'fanout') {
- return Promise.resolve(undefined);
- }
- const keyserverID = serverCallConfig.keyserverIDExtractor(...args);
- const {
- cookie,
- urlPrefix,
- sessionID,
- connection,
- lastCommunicatedPlatformDetails,
- } = keyserverInfos[keyserverID];
-
- const boundCallServerEndpoint = bindCookieAndUtilsIntoCallServerEndpoint({
- dispatch,
- currentUserInfo,
- cookie,
- urlPrefix,
- sessionID,
- connectionStatus: connection.status,
- lastCommunicatedPlatformDetails,
- });
- return boundCallServerEndpoint(endpoint, data, options);
- };
-}
+ return _memoize(
+ <Args: $ReadOnlyArray<mixed>, Return>(
+ keyserverCall: KeyserverCall<Args, Return>,
+ ): ((...Args) => Promise<Return>) => {
+ const callKeyserverEndpoint = (
+ endpoint: Endpoint,
+ data: Object,
+ args: Args,
+ options?: ?CallServerEndpointOptions,
+ ) => {
+ // TODO
+ if (keyserverCall.config.keyserverSelection === 'fanout') {
+ return Promise.resolve(undefined);
+ }
+ const keyserverID = keyserverCall.config.keyserverIDExtractor(
+ ...args,
+ );
+ const {
+ cookie,
+ urlPrefix,
+ sessionID,
+ connection,
+ lastCommunicatedPlatformDetails,
+ } = keyserverInfos[keyserverID];
+
+ const boundCallServerEndpoint = createBoundServerCallsSelector(
+ keyserverID,
+ )({
+ dispatch,
+ currentUserInfo,
+ cookie,
+ urlPrefix,
+ sessionID,
+ connectionStatus: connection.status,
+ lastCommunicatedPlatformDetails,
+ });
+
+ return boundCallServerEndpoint(endpoint, data, options);
+ };
+
+ return keyserverCall.actionFunc(callKeyserverEndpoint);
+ },
+ );
+ },
+);
function useKeyserverCall<Args: $ReadOnlyArray<mixed>, Return>(
keyserverCall: KeyserverCall<Args, Return>,
- paramOverride?: ?$Shape<ExtractAndBindCookieAndUtilsParams<Args, Return>>,
+ paramOverride?: ?$Shape<BindKeyserverCallParams>,
): (...Args) => Promise<Return> {
const dispatch = useDispatch();
const keyserverInfos = useSelector(
state => state.keyserverStore.keyserverInfos,
);
const currentUserInfo = useSelector(state => state.currentUserInfo);
- return React.useMemo(() => {
- const callKeyserverEndpoint = getCallKeyserverEndpoint({
- dispatch,
- currentUserInfo,
- keyserverInfos,
- keyserverCall,
- ...paramOverride,
- });
- return keyserverCall.actionFunc(callKeyserverEndpoint);
- }, [dispatch, currentUserInfo, keyserverInfos, keyserverCall, paramOverride]);
+ const bindCallKeyserverEndpointToAction = bindCallKeyserverEndpointSelector({
+ dispatch,
+ keyserverInfos,
+ currentUserInfo,
+ ...paramOverride,
+ });
+ return React.useMemo(
+ () => bindCallKeyserverEndpointToAction(keyserverCall),
+ [bindCallKeyserverEndpointToAction, keyserverCall],
+ );
}
export { useKeyserverCall };
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Dec 8, 2:56 PM (8 h, 1 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5848778
Default Alt Text
D9143.1765205817.diff (7 KB)
Attached To
Mode
D9143: [lib] Add memoization to useKeyserverCall
Attached
Detach File
Event Timeline
Log In to Comment