Skip to content

Commit

Permalink
MA-18060: add supportsAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-nikitin-2022 committed Jun 11, 2024
1 parent ff6a2ec commit cceecfa
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
25 changes: 25 additions & 0 deletions packages/core/src/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ export const DESKTOP_METHODS = [
: ['VKWebAppShowImages']),
];

/** Cache for supported methods */
let supportedHandlers: AnyRequestMethodName[];

/** Android VK Bridge interface. */
const androidBridge: Record<AnyRequestMethodName, (serializedData: string) => void> | undefined =
IS_CLIENT_SIDE ? (window as any).AndroidBridge : undefined;
Expand Down Expand Up @@ -232,6 +235,8 @@ export function createVKBridge(version: string): VKBridge {
* @returns Result of checking.
*/
function supports<K extends AnyRequestMethodName>(method: K): boolean {
console.warn('This method is deprecated. Use supportsAsync instead.');

if (IS_ANDROID_WEBVIEW) {
// Android support check
return !!(androidBridge && typeof androidBridge[method] === 'function');
Expand Down Expand Up @@ -342,12 +347,32 @@ export function createVKBridge(version: string): VKBridge {
*/
const sendPromise = promisifySend(send, subscribe, instanceId);

async function supportsAsync(method: AnyRequestMethodName): Promise<boolean> {
if (IS_ANDROID_WEBVIEW || IS_IOS_WEBVIEW) {
return supports(method);
}

if (supportedHandlers) {
return supportedHandlers.includes(method);
}

try {
const response = await sendPromise('SetSupportedHandlers');
supportedHandlers = response.supportedHandlers;
return supportedHandlers.includes(method);
} catch (error) {
supportedHandlers = ['VKWebAppInit'];
return supportedHandlers.includes(method);
}
}

return {
send: sendPromise,
sendPromise,
subscribe,
unsubscribe,
supports,
supportsAsync,
isWebView,
isIframe,
isEmbedded,
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/types/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,18 @@ export interface VKBridge {
*
* @param method Method (event) name to check.
* @returns Result of checking.
* @deprecated This method is deprecated. Use supportsAsync instead.
*/
supports: <K extends AnyRequestMethodName>(method: K) => boolean;

/**
* Checks if a method is supported on runtime platform.
*
* @param method Method (event) name to check.
* @returns The Promise object with result of checking.
*/
supportsAsync: <K extends AnyRequestMethodName>(method: K) => Promise<boolean>;

/**
* Checks whether the runtime is a WebView.
*
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/types/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,7 @@ export type RequestPropsMap = {
VKWebAppCallGetStatus: {};
VKWebAppRecommend: {};
VKWebAppAddToProfile: AddToProfileRequest;
SetSupportedHandlers: {};
};

/**
Expand Down Expand Up @@ -1327,6 +1328,7 @@ export type ReceiveDataMap = {
VKWebAppCallFinished: CallFinishedResponse;
VKWebAppRecommend: { result: true };
VKWebAppAddToProfile: AddToProfileResponse;
SetSupportedHandlers: { supportedHandlers: Array<keyof RequestPropsMap> };
};
/* eslint-enable @typescript-eslint/ban-types */

Expand Down

0 comments on commit cceecfa

Please sign in to comment.