Skip to content

Commit

Permalink
Merge pull request #586 from VKCOM/fix/supports-async-no-warning
Browse files Browse the repository at this point in the history
fix: do not show deprecation warning when calling bridge.supportsAsync
  • Loading branch information
pajecawav-vk authored Jan 13, 2025
2 parents 5d7e01a + de55b47 commit 44a420e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
4 changes: 4 additions & 0 deletions packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v2.15.4

- Исправили лишние ворнинги в консоли при вызове `bridge.supportsAsync`

## v2.15.3

- Добавлены типы для метода `VKWebAppTrackEvent`
Expand Down
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.15.3",
"version": "2.15.4",
"description": "Connects a Mini App with VK client",
"license": "MIT",
"main": "dist/index.js",
Expand Down
40 changes: 22 additions & 18 deletions packages/core/src/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export const DESKTOP_METHODS = [
];

/** Cache for supported methods */
let supportedHandlers: AnyRequestMethodName[];
let supportedHandlers: Set<AnyRequestMethodName>;

/** Android VK Bridge interface. */
const androidBridge: Record<AnyRequestMethodName, (serializedData: string) => void> | undefined =
Expand Down Expand Up @@ -228,16 +228,7 @@ export function createVKBridge(version: string): VKBridge {
}
}

/**
* Checks if a method is supported on runtime platform.
*
* @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.');

function supportsInner<K extends AnyRequestMethodName>(method: K): boolean {
if (IS_ANDROID_WEBVIEW) {
// Android support check
return !!(androidBridge && typeof androidBridge[method] === 'function');
Expand All @@ -262,6 +253,19 @@ export function createVKBridge(version: string): VKBridge {
return false;
}

/**
* Checks if a method is supported on runtime platform.
*
* @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('bridge.supports method is deprecated. Use bridge.supportsAsync instead.');

return supportsInner(method);
}

/**
* Checks whether the runtime is a WebView.
*
Expand Down Expand Up @@ -350,21 +354,21 @@ export function createVKBridge(version: string): VKBridge {

async function supportsAsync(method: AnyRequestMethodName): Promise<boolean> {
if (IS_ANDROID_WEBVIEW || IS_IOS_WEBVIEW) {
return supports(method);
return supportsInner(method);
}

if (supportedHandlers) {
return supportedHandlers.includes(method);
return supportedHandlers.has(method);
}

try {
const response = await sendPromise('SetSupportedHandlers');
supportedHandlers = response.supportedHandlers;
return supportedHandlers.includes(method);
supportedHandlers = new Set(response.supportedHandlers);
} catch (error) {
supportedHandlers = ['VKWebAppInit'];
return supportedHandlers.includes(method);
supportedHandlers = new Set(['VKWebAppInit'] as const);
}

return supportedHandlers.has(method);
}

subscribe((event) => {
Expand All @@ -373,7 +377,7 @@ export function createVKBridge(version: string): VKBridge {
}
switch (event.detail.type) {
case 'SetSupportedHandlers':
supportedHandlers = event.detail.data.supportedHandlers;
supportedHandlers = new Set(event.detail.data.supportedHandlers);
}
});

Expand Down

0 comments on commit 44a420e

Please sign in to comment.