Skip to content

Commit

Permalink
Merge pull request #558 from VKCOM/pavelnikitin/feature/add-supports-…
Browse files Browse the repository at this point in the history
…async-method/MA-18060

MA-18060: add supportsAsync
  • Loading branch information
pasha-nikitin-2003 authored Jul 4, 2024
2 parents ff6a2ec + de813fc commit 01c2048
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## v2.15.0

- `bridge.supports` помечен как устаревший. Используйте вместо него `bridge.supportsAsync`.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
36 changes: 36 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 @@ -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<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 +348,42 @@ 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);
}
}

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,
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
3 changes: 3 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 Expand Up @@ -1699,6 +1701,7 @@ export type ReceiveEventMap = EventReceiveNames<
'VKWebAppCallGetStatusFailed'
> &
EventReceiveNames<'VKWebAppRecommend', 'VKWebAppRecommendResult', 'VKWebAppRecommendFailed'> &
EventReceiveNames<'SetSupportedHandlers', 'SetSupportedHandlers', 'SetSupportedHandlersFailed'> &
EventReceiveNames<
'VKWebAppAddToProfile',
'VKWebAppAddToProfileResult',
Expand Down

0 comments on commit 01c2048

Please sign in to comment.