From cb8571ad1dcb367cf291d777a9526999f901d3e6 Mon Sep 17 00:00:00 2001 From: xiaoiver Date: Wed, 17 Jan 2024 09:42:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=86=85=E7=BD=AE=20flru=20=E9=81=BF=E5=85=8D?= =?UTF-8?q?=E6=89=93=E5=8C=85=E9=97=AE=E9=A2=98=20(#111)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: move flru to codebase #110 * chore: bump version --- package.json | 3 +-- src/lodash/memoize.ts | 45 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 9dceffb..678523b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@antv/util", - "version": "3.3.5", + "version": "3.3.6", "license": "MIT", "sideEffects": false, "main": "lib/index.js", @@ -90,7 +90,6 @@ }, "dependencies": { "fast-deep-equal": "^3.1.3", - "flru": "^1.0.2", "gl-matrix": "^3.3.0", "tslib": "^2.3.1" }, diff --git a/src/lodash/memoize.ts b/src/lodash/memoize.ts index 5b2b430..97a0ef6 100644 --- a/src/lodash/memoize.ts +++ b/src/lodash/memoize.ts @@ -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]);