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/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", diff --git a/packages/core/src/bridge.ts b/packages/core/src/bridge.ts index ffbfd58..87b9825 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,42 @@ 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); + } + } + + subscribe((event) => { + if (!event.detail) { + return; + } + switch (event.detail.type) { + case 'SetSupportedHandlers': + supportedHandlers = event.detail.data.supportedHandlers; + } + }); + 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..5829352 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 */ @@ -1699,6 +1701,7 @@ export type ReceiveEventMap = EventReceiveNames< 'VKWebAppCallGetStatusFailed' > & EventReceiveNames<'VKWebAppRecommend', 'VKWebAppRecommendResult', 'VKWebAppRecommendFailed'> & + EventReceiveNames<'SetSupportedHandlers', 'SetSupportedHandlers', 'SetSupportedHandlersFailed'> & EventReceiveNames< 'VKWebAppAddToProfile', 'VKWebAppAddToProfileResult',