mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[REM] utils: remove memoize, parseXML function
This commit is contained in:
+10
-1
@@ -1,5 +1,4 @@
|
|||||||
import { VNode, h } from "./vdom";
|
import { VNode, h } from "./vdom";
|
||||||
import { parseXML } from "./utils";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Owl QWeb Engine
|
* Owl QWeb Engine
|
||||||
@@ -110,6 +109,16 @@ export const UTILS = {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function parseXML(xml: string): Document {
|
||||||
|
const parser = new DOMParser();
|
||||||
|
const doc = parser.parseFromString(xml, "text/xml");
|
||||||
|
if (doc.getElementsByTagName("parsererror").length) {
|
||||||
|
throw new Error("Invalid XML in template");
|
||||||
|
}
|
||||||
|
return doc;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
// Compilation Context
|
// Compilation Context
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -4,14 +4,12 @@
|
|||||||
* We have here a small collection of utility functions:
|
* We have here a small collection of utility functions:
|
||||||
*
|
*
|
||||||
* - escape
|
* - escape
|
||||||
* - memoize
|
|
||||||
* - debounce
|
* - debounce
|
||||||
* - patch
|
* - patch
|
||||||
* - unpatch
|
* - unpatch
|
||||||
* - loadTemplates
|
* - loadTemplates
|
||||||
* - loadJS
|
* - loadJS
|
||||||
* - whenReady
|
* - whenReady
|
||||||
* - parseXML
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function escape(str: string | number | undefined): string {
|
export function escape(str: string | number | undefined): string {
|
||||||
@@ -29,26 +27,6 @@ export function escape(str: string | number | undefined): string {
|
|||||||
.replace(/`/g, "`");
|
.replace(/`/g, "`");
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a function, that, as long as it continues to be invoked, will not
|
* Returns a function, that, as long as it continues to be invoked, will not
|
||||||
* be triggered. The function will be called after it stops being called for
|
* be triggered. The function will be called after it stops being called for
|
||||||
@@ -173,11 +151,3 @@ export function whenReady(fn) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parseXML(xml: string): Document {
|
|
||||||
const parser = new DOMParser();
|
|
||||||
const doc = parser.parseFromString(xml, "text/xml");
|
|
||||||
if (doc.getElementsByTagName("parsererror").length) {
|
|
||||||
throw new Error("Invalid XML in template");
|
|
||||||
}
|
|
||||||
return doc;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
escape,
|
escape,
|
||||||
memoize,
|
|
||||||
debounce,
|
debounce,
|
||||||
patch,
|
patch,
|
||||||
unpatch
|
unpatch
|
||||||
@@ -18,26 +17,6 @@ describe("escape", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
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);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("debounce", () => {
|
describe("debounce", () => {
|
||||||
test("works as expected", () => {
|
test("works as expected", () => {
|
||||||
jest.useFakeTimers();
|
jest.useFakeTimers();
|
||||||
|
|||||||
Reference in New Issue
Block a user