Skip to content

Commit

Permalink
内置 flru 避免打包问题 (#111)
Browse files Browse the repository at this point in the history
* fix: move flru to codebase #110

* chore: bump version
  • Loading branch information
xiaoiver authored Jan 17, 2024
1 parent 2fcb3dd commit cb8571a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@antv/util",
"version": "3.3.5",
"version": "3.3.6",
"license": "MIT",
"sideEffects": false,
"main": "lib/index.js",
Expand Down Expand Up @@ -90,7 +90,6 @@
},
"dependencies": {
"fast-deep-equal": "^3.1.3",
"flru": "^1.0.2",
"gl-matrix": "^3.3.0",
"tslib": "^2.3.1"
},
Expand Down
45 changes: 44 additions & 1 deletion src/lodash/memoize.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,49 @@
import flru from 'flru';
import isFunction from './is-function';

function flru(max: number) {
let num, curr, prev;
const limit = max || 1;

function keep(key, value) {
if (++num > limit) {
prev = curr;
reset(1);
++num;
}
curr[key] = value;
}

function reset(isPartial?: number) {
num = 0;
curr = Object.create(null);
isPartial || (prev = Object.create(null));
}

reset();

return {
clear: reset,
has: function (key) {
return curr[key] !== void 0 || prev[key] !== void 0;
},
get: function (key) {
var val = curr[key];
if (val !== void 0) return val;
if ((val = prev[key]) !== void 0) {
keep(key, val);
return val;
}
},
set: function (key, value) {
if (curr[key] !== void 0) {
curr[key] = value;
} else {
keep(key, value);
}
},
};
}

/**
* _.memoize(calColor);
* _.memoize(calColor, (...args) => args[0]);
Expand Down

0 comments on commit cb8571a

Please sign in to comment.