mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
add memoize function and use it for makeEnvironment
This commit is contained in:
@@ -15,7 +15,7 @@ let wl: any[] = [];
|
|||||||
(<any>window).wl = wl;
|
(<any>window).wl = wl;
|
||||||
|
|
||||||
interface Meta<T extends WEnv> {
|
interface Meta<T extends WEnv> {
|
||||||
id: number;
|
readonly id: number;
|
||||||
// name: string;
|
// name: string;
|
||||||
// template: string;
|
// template: string;
|
||||||
vnode: VNode | null;
|
vnode: VNode | null;
|
||||||
|
|||||||
@@ -31,3 +31,23 @@ export function idGenerator(): (() => number) {
|
|||||||
let nextID = 1;
|
let nextID = 1;
|
||||||
return () => nextID++;
|
return () => nextID++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type HashFn = (args: any[]) => string;
|
||||||
|
|
||||||
|
export function memoize<R, T extends (...args: any[]) => R>(
|
||||||
|
f: T,
|
||||||
|
hash?: HashFn
|
||||||
|
): T {
|
||||||
|
if (!hash) {
|
||||||
|
hash = args => args.map(a => String(a)).join(",");
|
||||||
|
}
|
||||||
|
let cache: { [key: string]: R } = {};
|
||||||
|
function memoizedFunction(...args: any[]) {
|
||||||
|
let hashValue = hash!(args);
|
||||||
|
if (!(hashValue in cache)) {
|
||||||
|
cache[hashValue] = f(...args);
|
||||||
|
}
|
||||||
|
return cache[hashValue];
|
||||||
|
}
|
||||||
|
return memoizedFunction as T;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { QWeb } from "./core/qweb_vdom";
|
import { QWeb } from "./core/qweb_vdom";
|
||||||
import { idGenerator } from "./core/utils";
|
import { idGenerator, memoize } from "./core/utils";
|
||||||
import { WEnv } from "./core/widget";
|
import { WEnv } from "./core/widget";
|
||||||
import { ActionManager, IActionManager } from "./services/action_manager";
|
import { ActionManager, IActionManager } from "./services/action_manager";
|
||||||
import { Ajax, IAjax } from "./services/ajax";
|
import { Ajax, IAjax } from "./services/ajax";
|
||||||
@@ -32,7 +32,17 @@ export interface Env extends WEnv {
|
|||||||
// Code
|
// Code
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
export function makeEnvironment(): Env {
|
/**
|
||||||
|
* makeEnvironment returns the main environment for the application.
|
||||||
|
*
|
||||||
|
* Note that it does not make much sense (except for tests) to have more than
|
||||||
|
* one environment. For example, with two environment, the router code in one
|
||||||
|
* environment will probably interfere with the code from the other environment.
|
||||||
|
*
|
||||||
|
* For this reason, the result of makeEnvironment is memoized: every call to
|
||||||
|
* this function will actually return the same environment.
|
||||||
|
*/
|
||||||
|
export const makeEnvironment = memoize(function(): Env {
|
||||||
const qweb = new QWeb();
|
const qweb = new QWeb();
|
||||||
const router = new Router();
|
const router = new Router();
|
||||||
const ajax = new Ajax();
|
const ajax = new Ajax();
|
||||||
@@ -55,4 +65,4 @@ export function makeEnvironment(): Env {
|
|||||||
rpc: ajax.rpc,
|
rpc: ajax.rpc,
|
||||||
debug: false
|
debug: false
|
||||||
};
|
};
|
||||||
}
|
});
|
||||||
|
|||||||
@@ -1,4 +1,9 @@
|
|||||||
import { escape, htmlTrim, idGenerator } from "../../src/ts/core/utils";
|
import {
|
||||||
|
escape,
|
||||||
|
htmlTrim,
|
||||||
|
idGenerator,
|
||||||
|
memoize
|
||||||
|
} from "../../src/ts/core/utils";
|
||||||
|
|
||||||
describe("escape", () => {
|
describe("escape", () => {
|
||||||
test("normal strings", () => {
|
test("normal strings", () => {
|
||||||
@@ -34,3 +39,23 @@ describe("idGenerator", () => {
|
|||||||
expect(gen()).toBe(3);
|
expect(gen()).toBe(3);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("memoize", () => {
|
||||||
|
test("return correct value", () => {
|
||||||
|
const f = memoize((a, b) => a + b);
|
||||||
|
expect(f(1, 3)).toBe(4);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("does not recompute if not needed", () => {
|
||||||
|
let nCalls = 0;
|
||||||
|
function origFunction(a: number, b: number): number {
|
||||||
|
nCalls++;
|
||||||
|
return a + b;
|
||||||
|
}
|
||||||
|
const memoized = memoize(origFunction);
|
||||||
|
|
||||||
|
expect(memoized(1, 3)).toBe(4);
|
||||||
|
expect(memoized(1, 3)).toBe(4);
|
||||||
|
expect(nCalls).toBe(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user