From 0c615d58ea2e5437b6886da02abb05e0af4dab06 Mon Sep 17 00:00:00 2001 From: Rishabh Rathod Date: Wed, 10 May 2023 12:37:25 +0530 Subject: [PATCH] fix: set Api.data on promise resolution (#23060) ## Description Set the `api.data` as soon as promise is resolved to avoid race condition. NOTE: We have added a logic to set the global context similar to storeValue as a temporary fix. #### PR fixes following issue(s) Fixes #23052 #### Type of change - Bug fix (non-breaking change which fixes an issue) ## Testing #### How Has This Been Tested? #### Test Plan #### Issues raised during DP testing ## Checklist: #### Dev activity - [x] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] PR is being merged under a feature flag #### QA activity: - [ ] [Speedbreak features](https://github.com/appsmithorg/TestSmith/wiki/Test-plan-implementation#speedbreaker-features-to-consider-for-every-change) have been covered - [ ] Test plan covers all impacted features and [areas of interest](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans/_edit#areas-of-interest) - [ ] Test plan has been peer reviewed by project stakeholders and other QA members - [ ] Manually tested functionality on DP - [ ] We had an implementation alignment call with stakeholders post QA Round 2 - [ ] Cypress test cases have been added and approved by SDET/manual QA - [ ] Added `Test Plan Approved` label after Cypress tests were reviewed - [ ] Added `Test Plan Approved` label after JUnit tests were reviewed --- .../src/workers/Evaluation/fns/actionFns.ts | 16 +++++++++++---- .../src/workers/Evaluation/fns/index.ts | 20 ++++++++++++++----- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/app/client/src/workers/Evaluation/fns/actionFns.ts b/app/client/src/workers/Evaluation/fns/actionFns.ts index 16fdf12e27..e82de8ed88 100644 --- a/app/client/src/workers/Evaluation/fns/actionFns.ts +++ b/app/client/src/workers/Evaluation/fns/actionFns.ts @@ -1,8 +1,9 @@ import { isTrueObject } from "@appsmith/workers/Evaluation/evaluationUtils"; import { promisify } from "./utils/Promisify"; +import type { ActionEntity } from "entities/DataTree/types"; function runFnDescriptor( - this: any, + this: ActionEntity, onSuccessOrParams?: (data: any) => unknown | Record, onError?: (e: string) => unknown, params = {}, @@ -30,7 +31,7 @@ export type TRunDescription = ReturnType; export type TRunActionType = TRunDescription["type"]; export default async function run( - this: any, + this: ActionEntity, onSuccessOrParams?: (data: any) => unknown | Record, onError?: (e: string) => unknown, params = {}, @@ -38,6 +39,13 @@ export default async function run( const executor = promisify(runFnDescriptor.bind(this)); try { const response = await executor(onSuccessOrParams, onError, params); + + // @ts-expect-error: globalThis type is not defined + const action = globalThis[this.name]; + if (action) { + action.data = response[0]; + } + if (typeof onSuccessOrParams === "function") { onSuccessOrParams.apply(this, response); return; @@ -57,7 +65,7 @@ export default async function run( } } -function clearFnDescriptor(this: any) { +function clearFnDescriptor(this: ActionEntity) { return { type: "CLEAR_PLUGIN_ACTION" as const, payload: { @@ -70,6 +78,6 @@ export type TClearArgs = Parameters; export type TClearDescription = ReturnType; export type TClearActionType = TClearDescription["type"]; -export async function clear(this: any) { +export async function clear(this: ActionEntity) { return promisify(clearFnDescriptor.bind(this))(); } diff --git a/app/client/src/workers/Evaluation/fns/index.ts b/app/client/src/workers/Evaluation/fns/index.ts index 2d7adef1d9..39c6d27324 100644 --- a/app/client/src/workers/Evaluation/fns/index.ts +++ b/app/client/src/workers/Evaluation/fns/index.ts @@ -61,6 +61,7 @@ import { watchGeoLocation, } from "./geolocationFns"; import { getFnWithGuards, isAsyncGuard } from "./utils/fnGuard"; +import type { ActionEntity } from "entities/DataTree/types"; // cloudHosting -> to use in EE // eslint-disable-next-line @typescript-eslint/no-unused-vars @@ -119,16 +120,25 @@ export const entityFns = [ { name: "run", qualifier: (entity: DataTreeEntity) => isAction(entity), - fn: (entity: DataTreeEntity, entityName: string) => - getFnWithGuards(run.bind(entity), `${entityName}.run`, [isAsyncGuard]), + fn: (entity: DataTreeEntity, entityName: string) => { + // @ts-expect-error: name is not defined on ActionEntity + entity.name = entityName; + return getFnWithGuards( + run.bind(entity as ActionEntity), + `${entityName}.run`, + [isAsyncGuard], + ); + }, }, { name: "clear", qualifier: (entity: DataTreeEntity) => isAction(entity), fn: (entity: DataTreeEntity, entityName: string) => - getFnWithGuards(clear.bind(entity), `${entityName}.clear`, [ - isAsyncGuard, - ]), + getFnWithGuards( + clear.bind(entity as ActionEntity), + `${entityName}.clear`, + [isAsyncGuard], + ), }, { name: "getGeoLocation",