From 742bcb33f2972bbc56ecaa71fd2ec22e88c91545 Mon Sep 17 00:00:00 2001 From: N Date: Tue, 11 Jun 2024 17:20:55 +0300 Subject: [PATCH 1/3] MA-18060: add supportsAsync --- packages/core/src/bridge.ts | 26 ++++++++++++++++++++++++++ packages/core/src/types/bridge.ts | 9 +++++++++ packages/core/src/types/data.ts | 2 ++ 3 files changed, 37 insertions(+) diff --git a/packages/core/src/bridge.ts b/packages/core/src/bridge.ts index ffbfd58..50e2121 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; @@ -230,8 +233,11 @@ export function createVKBridge(version: string): VKBridge { * * @param method Method (event) name to check. * @returns Result of checking. + * @deprecated This method is deprecated. Use supportsAsync instead. */ 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 +348,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 */ From 70cc13c543795c4d1b01da0e08cda5206733affc Mon Sep 17 00:00:00 2001 From: N Date: Wed, 26 Jun 2024 22:34:48 +0300 Subject: [PATCH 2/3] MA-18060: add subscription --- packages/core/CHANGELOG.md | 3 +++ packages/core/src/bridge.ts | 10 ++++++++++ packages/core/src/types/data.ts | 1 + 3 files changed, 14 insertions(+) create mode 100644 packages/core/CHANGELOG.md diff --git a/packages/core/CHANGELOG.md b/packages/core/CHANGELOG.md new file mode 100644 index 0000000..cb428a3 --- /dev/null +++ b/packages/core/CHANGELOG.md @@ -0,0 +1,3 @@ +## v2.15.0 + +- `bridge.supports` помечен как устаревший. Используйте вместо него `bridge.supportsAsync`. diff --git a/packages/core/src/bridge.ts b/packages/core/src/bridge.ts index 50e2121..87b9825 100644 --- a/packages/core/src/bridge.ts +++ b/packages/core/src/bridge.ts @@ -367,6 +367,16 @@ export function createVKBridge(version: string): VKBridge { } } + subscribe((event) => { + if (!event.detail) { + return; + } + switch (event.detail.type) { + case 'SetSupportedHandlers': + supportedHandlers = event.detail.data.supportedHandlers; + } + }); + return { send: sendPromise, sendPromise, diff --git a/packages/core/src/types/data.ts b/packages/core/src/types/data.ts index 0f5261b..5829352 100644 --- a/packages/core/src/types/data.ts +++ b/packages/core/src/types/data.ts @@ -1701,6 +1701,7 @@ export type ReceiveEventMap = EventReceiveNames< 'VKWebAppCallGetStatusFailed' > & EventReceiveNames<'VKWebAppRecommend', 'VKWebAppRecommendResult', 'VKWebAppRecommendFailed'> & + EventReceiveNames<'SetSupportedHandlers', 'SetSupportedHandlers', 'SetSupportedHandlersFailed'> & EventReceiveNames< 'VKWebAppAddToProfile', 'VKWebAppAddToProfileResult', From de813fc42fe3c2b490ee1ec56adf712fb177c601 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Thu, 4 Jul 2024 10:13:20 +0000 Subject: [PATCH 3/3] bump(@vkontakte/vk-bridge): from 2.14.2 to 2.15.0 --- packages/core/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/package.json b/packages/core/package.json index 1c1aa44..be9ea63 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,6 +1,6 @@ { "name": "@vkontakte/vk-bridge", - "version": "2.14.2", + "version": "2.15.0", "description": "Connects a Mini App with VK client", "license": "MIT", "main": "dist/index.js",