diff --git a/app/client/src/workers/Evaluation/__tests__/Actions.test.ts b/app/client/src/workers/Evaluation/__tests__/Actions.test.ts index 55491be532..454335c5b0 100644 --- a/app/client/src/workers/Evaluation/__tests__/Actions.test.ts +++ b/app/client/src/workers/Evaluation/__tests__/Actions.test.ts @@ -4,7 +4,6 @@ import { createEvaluationContext, EvalContext, } from "workers/Evaluation/evaluate"; -import uniqueId from "lodash/uniqueId"; import { MessageType } from "utils/MessageUtil"; import { addDataTreeToContext, diff --git a/app/client/src/workers/Evaluation/fns/LocalStorage.ts b/app/client/src/workers/Evaluation/fns/LocalStorage.ts new file mode 100644 index 0000000000..d1b4212270 --- /dev/null +++ b/app/client/src/workers/Evaluation/fns/LocalStorage.ts @@ -0,0 +1,30 @@ +import get from "lodash/get"; + +export default function initLocalStorage(ctx: typeof globalThis) { + function getItem(key: string) { + //@ts-expect-error no types + return get(ctx.appsmith.store, key); + } + function setItem(key: string, value: any) { + //@ts-expect-error no types + ctx.storeValue(key, value); + } + function removeItem(key: string) { + //@ts-expect-error no types + ctx.removeValue(key); + } + function clear() { + //@ts-expect-error no types + ctx.clearStore(); + } + const localStorage = { + getItem, + setItem, + removeItem, + clear, + }; + Object.defineProperty(ctx, "localStorage", { + enumerable: false, + value: localStorage, + }); +} diff --git a/app/client/src/workers/Evaluation/fns/__tests__/LocalStorage.test.ts b/app/client/src/workers/Evaluation/fns/__tests__/LocalStorage.test.ts new file mode 100644 index 0000000000..1e01a3ca0f --- /dev/null +++ b/app/client/src/workers/Evaluation/fns/__tests__/LocalStorage.test.ts @@ -0,0 +1,102 @@ +import { addPlatformFunctionsToEvalContext } from "ce/workers/Evaluation/Actions"; +import { ENTITY_TYPE } from "design-system"; +import { PluginType } from "entities/Action"; +import { DataTree } from "entities/DataTree/dataTreeFactory"; +import { createEvaluationContext } from "workers/Evaluation/evaluate"; +import initLocalStorage from "../LocalStorage"; + +describe("Tests localStorage implementation in worker", () => { + const dataTree: DataTree = { + action1: { + actionId: "123", + pluginId: "", + data: {}, + config: {}, + datasourceUrl: "", + pluginType: PluginType.API, + dynamicBindingPathList: [], + name: "action1", + bindingPaths: {}, + reactivePaths: {}, + isLoading: false, + run: {}, + clear: {}, + responseMeta: { isExecutionSuccess: false }, + ENTITY_TYPE: ENTITY_TYPE.ACTION, + dependencyMap: {}, + logBlackList: {}, + }, + }; + const workerEventMock = jest.fn(); + self.postMessage = workerEventMock; + self.ALLOW_ASYNC = true; + const evalContext = createEvaluationContext({ + dataTree, + resolvedFunctions: {}, + isTriggerBased: true, + context: {}, + }); + + addPlatformFunctionsToEvalContext(evalContext); + initLocalStorage(evalContext as any); + it("setItem()", () => { + const key = "some"; + const value = "thing"; + jest.useFakeTimers(); + evalContext.localStorage.setItem(key, value); + jest.runAllTimers(); + expect(workerEventMock).lastCalledWith({ + messageType: "DEFAULT", + body: { + data: [ + { + payload: { + key: "some", + value: "thing", + persist: true, + }, + type: "STORE_VALUE", + }, + ], + method: "PROCESS_STORE_UPDATES", + }, + }); + }); + it("getItem()", () => { + expect(evalContext.localStorage.getItem("some")).toBe("thing"); + }); + it("removeItem()", () => { + evalContext.localStorage.removeItem("some"); + jest.runAllTimers(); + expect(workerEventMock).lastCalledWith({ + messageType: "DEFAULT", + body: { + data: [ + { + payload: { + key: "some", + }, + type: "REMOVE_VALUE", + }, + ], + method: "PROCESS_STORE_UPDATES", + }, + }); + }); + it("clear()", () => { + evalContext.localStorage.clear(); + jest.runAllTimers(); + expect(workerEventMock).lastCalledWith({ + messageType: "DEFAULT", + body: { + data: [ + { + payload: null, + type: "CLEAR_STORE", + }, + ], + method: "PROCESS_STORE_UPDATES", + }, + }); + }); +}); diff --git a/app/client/src/workers/Evaluation/fns/storeFns.ts b/app/client/src/workers/Evaluation/fns/storeFns.ts index 52465338c0..05b4b294d7 100644 --- a/app/client/src/workers/Evaluation/fns/storeFns.ts +++ b/app/client/src/workers/Evaluation/fns/storeFns.ts @@ -20,7 +20,7 @@ export function initStoreFns(ctx: typeof globalThis) { persist, }, }; - set(self, ["appsmith", "store", key], value); + set(ctx, ["appsmith", "store", key], value); triggerCollector.collect(requestPayload); return Promise.resolve({}); } @@ -33,14 +33,14 @@ export function initStoreFns(ctx: typeof globalThis) { }, }; //@ts-expect-error no types for store - delete self.appsmith.store[key]; + delete ctx.appsmith.store[key]; triggerCollector.collect(requestPayload); return Promise.resolve({}); } function clearStore() { //@ts-expect-error no types for store - self.appsmith.store = {}; + ctx.appsmith.store = {}; triggerCollector.collect({ type: "CLEAR_STORE", payload: null, diff --git a/app/client/src/workers/Evaluation/handlers/setupEvalEnv.ts b/app/client/src/workers/Evaluation/handlers/setupEvalEnv.ts index 4ed9281e3b..320c5897b3 100644 --- a/app/client/src/workers/Evaluation/handlers/setupEvalEnv.ts +++ b/app/client/src/workers/Evaluation/handlers/setupEvalEnv.ts @@ -6,6 +6,7 @@ import overrideTimeout from "../TimeoutOverride"; import { EvalWorkerSyncRequest } from "../types"; import userLogs from "../UserLog"; import { addPlatformFunctionsToEvalContext } from "@appsmith/workers/Evaluation/Actions"; +import initLocalStorage from "../fns/LocalStorage"; export default function() { const libraries = resetJSLibraries(); @@ -26,6 +27,7 @@ export default function() { interceptAndOverrideHttpRequest(); setupDOM(); addPlatformFunctionsToEvalContext(self); + initLocalStorage(self); return true; }