diff --git a/lib/minimal-package.ts b/lib/minimal-package.ts index cf4f033..5cbcf0a 100644 --- a/lib/minimal-package.ts +++ b/lib/minimal-package.ts @@ -1 +1 @@ -export default { name: '@mondaycom/apps-sdk', version: '3.2.1' }; +export default { name: '@mondaycom/apps-sdk', version: '3.2.2-beta.1' }; diff --git a/lib/secure-storage/secure-storage.ts b/lib/secure-storage/secure-storage.ts index 1826823..4c3ec0b 100644 --- a/lib/secure-storage/secure-storage.ts +++ b/lib/secure-storage/secure-storage.ts @@ -162,6 +162,26 @@ const authenticate = async (connectionData: ConnectionData): Promise | null = null; + +const authenticateWithMutex = async (currentConnectionData: ConnectionData): Promise => { + // If there's already an authentication in progress, wait for it + if (authenticationPromise) { + return authenticationPromise; + } + + // Start a new authentication and cache the promise + authenticationPromise = authenticate(currentConnectionData); + + try { + const result = await authenticationPromise; + connectionData = result; // Update the global connection data + return result; + } finally { + // Clear the promise when done (success or failure) + authenticationPromise = null; + } +}; export class SecureStorage implements ISecureStorageInstance { @@ -170,14 +190,14 @@ export class SecureStorage implements ISecureStorageInstance { } async delete(key: string) { - connectionData = await authenticate(connectionData); + connectionData = await authenticateWithMutex(connectionData); const fullPath = generateCrudPath(key, connectionData.id); await secureStorageFetch(fullPath, connectionData, { method: 'DELETE' }); return true; } async get(key: string) { - connectionData = await authenticate(connectionData); + connectionData = await authenticateWithMutex(connectionData); const fullPath = generateCrudPath(key, connectionData.id); const result = await secureStorageFetch(fullPath, connectionData, { method: 'GET' }); if (!isDefined(result) || !isDefined(result?.data)) { @@ -192,7 +212,7 @@ export class SecureStorage implements ISecureStorageInstance { } async set(key: string, value: T) { - connectionData = await authenticate(connectionData); + connectionData = await authenticateWithMutex(connectionData); const fullPath = generateCrudPath(key, connectionData.id); const formalizedValue = isObject(value) ? value : { [MONDAY_CODE_RESERVED_PRIMITIVES_KEY]: value }; await secureStorageFetch(fullPath, connectionData, { diff --git a/package.json b/package.json index 73b5975..b28455a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@mondaycom/apps-sdk", - "version": "3.2.1", + "version": "3.2.2-beta.1", "description": "monday apps SDK for NodeJS", "main": "./dist/cjs/index.js", "module": "./dist/esm/index.js",