Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3356793
D9121.id30913.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
D9121.id30913.diff
View Options
diff --git a/lib/utils/action-utils.js b/lib/utils/action-utils.js
--- a/lib/utils/action-utils.js
+++ b/lib/utils/action-utils.js
@@ -470,4 +470,5 @@
createBoundServerCallsSelector,
registerActiveSocket,
useServerCall,
+ bindCookieAndUtilsIntoCallServerEndpoint,
};
diff --git a/lib/utils/keyserver-call.js b/lib/utils/keyserver-call.js
new file mode 100644
--- /dev/null
+++ b/lib/utils/keyserver-call.js
@@ -0,0 +1,109 @@
+// @flow
+
+import * as React from 'react';
+import { useDispatch } from 'react-redux';
+
+import { bindCookieAndUtilsIntoCallServerEndpoint } from './action-utils.js';
+import type { CallServerEndpointOptions } from './call-server-endpoint.js';
+import { useSelector } from './redux-utils.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 { CurrentUserInfo } from '../types/user-types.js';
+
+export type CallKeyserverEndpoint<Args: $ReadOnlyArray<mixed>> = (
+ endpoint: Endpoint,
+ input: any,
+ args: Args,
+ options?: ?CallServerEndpointOptions,
+) => Promise<any>;
+
+export type ActionFunc<Args: $ReadOnlyArray<mixed>, Return> = (
+ callServerEndpoint: CallKeyserverEndpoint<Args>,
+) => (...Args) => Promise<Return>;
+
+export type KeyserverCallConfig<Args: $ReadOnlyArray<mixed>> =
+ | { +keyserverSelection: 'fanout' }
+ | {
+ +keyserverSelection: 'specific',
+ +keyserverIDExtractor: (...Args) => string,
+ };
+
+export type KeyserverCall<Args: $ReadOnlyArray<mixed>, Return> = {
+ +actionFunc: ActionFunc<Args, Return>,
+ +config: KeyserverCallConfig<Args>,
+};
+
+export type BindServerCall = <F>(serverCall: ActionFunc<F>) => F;
+
+export type ExtractAndBindCookieAndUtilsParams<
+ Args: $ReadOnlyArray<mixed>,
+ Return,
+> = {
+ +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;
+
+ return (
+ endpoint: Endpoint,
+ data: Object,
+ args: Args,
+ options?: ?CallServerEndpointOptions,
+ ) => {
+ // TODO
+ if (serverCallConfig.keyserverSelection === 'fanout') {
+ return Promise.all([]);
+ }
+ 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);
+ };
+}
+
+function useKeyserverCall<Args: $ReadOnlyArray<mixed>, Return>(
+ keyserverCall: KeyserverCall<Args, Return>,
+ paramOverride?: ?$Shape<ExtractAndBindCookieAndUtilsParams<Args, Return>>,
+): (...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]);
+}
+
+export { useKeyserverCall };
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Nov 24, 8:45 PM (20 h, 39 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2577270
Default Alt Text
D9121.id30913.diff (3 KB)
Attached To
Mode
D9121: [lib] Create useKeyserverCall
Attached
Detach File
Event Timeline
Log In to Comment