From cceecfa0bd6947a6b448ce7494fcfaff94f9a389 Mon Sep 17 00:00:00 2001 From: N Date: Tue, 11 Jun 2024 17:20:55 +0300 Subject: [PATCH] MA-18060: add supportsAsync --- packages/core/src/bridge.ts | 25 +++++++++++++++++++++++++ packages/core/src/types/bridge.ts | 9 +++++++++ packages/core/src/types/data.ts | 2 ++ 3 files changed, 36 insertions(+) diff --git a/packages/core/src/bridge.ts b/packages/core/src/bridge.ts index ffbfd58..4cec630 100644 --- a/packages/core/src/bridge.ts +++ b/packages/core/src/bridge.ts @@ -119,6 +119,9 @@ export const DESKTOP_METHODS = [ : ['VKWebAppShowImages']), ]; +/** Cache for supported methods */ +let supportedHandlers: AnyRequestMethodName[]; + /** Android VK Bridge interface. */ const androidBridge: Record void> | undefined = IS_CLIENT_SIDE ? (window as any).AndroidBridge : undefined; @@ -232,6 +235,8 @@ export function createVKBridge(version: string): VKBridge { * @returns Result of checking. */ function supports(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'); @@ -342,12 +347,32 @@ export function createVKBridge(version: string): VKBridge { */ const sendPromise = promisifySend(send, subscribe, instanceId); + async function supportsAsync(method: AnyRequestMethodName): Promise { + 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, diff --git a/packages/core/src/types/bridge.ts b/packages/core/src/types/bridge.ts index 903d712..6c50bda 100644 --- a/packages/core/src/types/bridge.ts +++ b/packages/core/src/types/bridge.ts @@ -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: (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: (method: K) => Promise; + /** * Checks whether the runtime is a WebView. * diff --git a/packages/core/src/types/data.ts b/packages/core/src/types/data.ts index c1ecd32..0f5261b 100644 --- a/packages/core/src/types/data.ts +++ b/packages/core/src/types/data.ts @@ -1206,6 +1206,7 @@ export type RequestPropsMap = { VKWebAppCallGetStatus: {}; VKWebAppRecommend: {}; VKWebAppAddToProfile: AddToProfileRequest; + SetSupportedHandlers: {}; }; /** @@ -1327,6 +1328,7 @@ export type ReceiveDataMap = { VKWebAppCallFinished: CallFinishedResponse; VKWebAppRecommend: { result: true }; VKWebAppAddToProfile: AddToProfileResponse; + SetSupportedHandlers: { supportedHandlers: Array }; }; /* eslint-enable @typescript-eslint/ban-types */