diff --git a/web/static/src/ts/store/action_manager_mixin.ts b/web/static/src/ts/store/action_manager_mixin.ts index 42b061e8..6ecdbdc3 100644 --- a/web/static/src/ts/store/action_manager_mixin.ts +++ b/web/static/src/ts/store/action_manager_mixin.ts @@ -43,6 +43,8 @@ export function actionManagerMixin>( Base: T ) { return class extends Base { + actionCache: { [key: number]: Promise } = {}; + async doAction(request: ActionRequest) { if (typeof request === "number") { await this.loadAction(request); @@ -68,13 +70,16 @@ export function actionManagerMixin>( } } - loadAction(id: number) { - return this.rpc({ + loadAction(id: number): Promise { + if (id in this.actionCache) { + return this.actionCache[id]; + } + return (this.actionCache[id] = this.rpc({ route: "web/action/load", params: { action_id: id } - }); + })); } }; } diff --git a/web/static/tests/store/action_manager_mixin_test.ts b/web/static/tests/store/action_manager_mixin_test.ts new file mode 100644 index 00000000..c8cbd6c8 --- /dev/null +++ b/web/static/tests/store/action_manager_mixin_test.ts @@ -0,0 +1,19 @@ +import { makeTestStore } from "../helpers"; + +//------------------------------------------------------------------------------ +// Tests +//------------------------------------------------------------------------------ + +test("does not reload action if already done", async () => { + const routes: string[] = []; + const store = makeTestStore({ rpc: async route => routes.push(route) }); + expect(routes).toEqual([]); + + store.doAction(32); + + expect(routes).toEqual(["web/action/load"]); + + store.doAction(32); + + expect(routes).toEqual(["web/action/load"]); +});