Skip to content

Commit

Permalink
Merge pull request #484 from VKCOM/syarkin/add_instance-id_to_request_id
Browse files Browse the repository at this point in the history
add bridge instance id to request_id
  • Loading branch information
Yarkin13 authored Nov 21, 2023
2 parents d974acb + 90db045 commit 5eb58c5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
6 changes: 5 additions & 1 deletion packages/core/src/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
RequestProps,
RequestIdProp,
} from './types/bridge';
import { createInstanceId } from './utils';

/** Is the client side runtime environment */
export const IS_CLIENT_SIDE = typeof window !== 'undefined';
Expand Down Expand Up @@ -147,6 +148,9 @@ export function createVKBridge(version: string): VKBridge {
/** List of functions that subscribed on events. */
const subscribers: VKBridgeSubscribeHandler[] = [];

/** Uniq instance ID */
const instanceId = createInstanceId();

/**
* Sends an event to the runtime env. In the case of Android/iOS application
* env is the application itself. In the case of the browser, the parent
Expand Down Expand Up @@ -336,7 +340,7 @@ export function createVKBridge(version: string): VKBridge {
* Enhanced send functions for the ability to receive response data in
* the Promise object.
*/
const sendPromise = promisifySend(send, subscribe);
const sendPromise = promisifySend(send, subscribe, instanceId);

return {
send: sendPromise,
Expand Down
10 changes: 7 additions & 3 deletions packages/core/src/promisifySend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ function createCounter() {

/**
* Creates interface for resolving request promises by request id's (or not).
*
* @param instanceId Uniq bridge instance ID.
*/
function createRequestResolver() {
function createRequestResolver(instanceId: string) {
/**
* @prop resolve Resolve function.
* @prop reject Reject function.
Expand All @@ -44,7 +46,7 @@ function createRequestResolver() {
* @returns New request id of the added controller.
*/
add(controller: PromiseController, customId?: number | string): number | string {
const id = customId != null ? customId : counter.next();
const id = customId != null ? customId : `${counter.next()}_${instanceId}`;

promiseControllers[id] = controller;

Expand Down Expand Up @@ -80,6 +82,7 @@ function createRequestResolver() {
*
* @param sendEvent Send event function.
* @param subscribe Subscribe event function.
* @param instanceId Uniq bridge instance ID.
* @returns Send function which returns the Promise object.
*/
export function promisifySend(
Expand All @@ -88,8 +91,9 @@ export function promisifySend(
props?: RequestProps<K> & RequestIdProp,
) => void,
subscribe: (fn: VKBridgeSubscribeHandler) => void,
instanceId: string,
) {
const requestResolver = createRequestResolver();
const requestResolver = createRequestResolver(instanceId);

// Subscribe to receive a data
subscribe((event) => {
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function createInstanceId() {
const allNumbersAndLetters = 36;
const positionAfterZeroAnDot = 2;
const keyLength = 3;
return Math.random()
.toString(allNumbersAndLetters)
.substring(positionAfterZeroAnDot, positionAfterZeroAnDot + keyLength);
}

0 comments on commit 5eb58c5

Please sign in to comment.