You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
964 B

3 years ago
/**
* Simple memoize
* wxs doesn't support fn.apply, so this memoize only support up to 2 args
*/
3 years ago
/* eslint-disable */
3 years ago
function isPrimitive(value) {
var type = typeof value;
3 years ago
return type === 'boolean' || type === 'number' || type === 'string' || type === 'undefined' || value === null;
} // mock simple fn.call in wxs
3 years ago
function call(fn, args) {
if (args.length === 2) {
return fn(args[0], args[1]);
}
if (args.length === 1) {
return fn(args[0]);
}
return fn();
}
function serializer(args) {
if (args.length === 1 && isPrimitive(args[0])) {
return args[0];
}
3 years ago
3 years ago
var obj = {};
3 years ago
3 years ago
for (var i = 0; i < args.length; i++) {
obj['key' + i] = args[i];
}
3 years ago
3 years ago
return JSON.stringify(obj);
}
function memoize(fn) {
var cache = {};
3 years ago
return function () {
3 years ago
var key = serializer(arguments);
3 years ago
3 years ago
if (cache[key] === undefined) {
cache[key] = call(fn, arguments);
}
return cache[key];
};
}
3 years ago
module.exports = memoize;