Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

内置 flru 避免打包问题 #111

Merged
merged 2 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading