Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MA-18060: add supportsAsync #558

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 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.
nshvyryaev marked this conversation as resolved.
Show resolved Hide resolved
* @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,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');
nshvyryaev marked this conversation as resolved.
Show resolved Hide resolved
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
Loading