From 470154f8f98338170cbd7498b70c36082ff05258 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Fri, 1 Mar 2019 11:26:00 +0100 Subject: [PATCH] add action cache to action manager --- .../src/ts/store/action_manager_mixin.ts | 11 ++++++++--- .../tests/store/action_manager_mixin_test.ts | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 web/static/tests/store/action_manager_mixin_test.ts 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"]); +});