@heycar/heycars-map 2.13.0-relay1 → 2.14.0-walkOptimize1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +35 -0
- package/dist/v2/api/cdn.d.ts +1 -2
- package/dist/v2/api/cdn.js +1 -3
- package/dist/v2/api/contants.d.ts +3 -0
- package/dist/v2/api/contants.js +6 -0
- package/dist/v2/business-components/AbsoluteAddressBox/AbsoluteAddressBox.js +7 -1
- package/dist/v2/business-components/AuxiliaryPassanger/AuxiliaryPassanger.d.ts +6 -0
- package/dist/v2/business-components/AuxiliaryPassanger/AuxiliaryPassanger.js +50 -0
- package/dist/v2/business-components/AuxiliaryPassanger/index.d.ts +1 -0
- package/dist/v2/business-components/AuxiliaryPassanger/index.js +4 -0
- package/dist/v2/business-components/BusinessTaxiServiceMap/BusinessTaxiServiceMap.js +6 -0
- package/dist/v2/business-components/BusinessTaxiServiceMap/BusinessTaxiServiceSection.js +1 -3
- package/dist/v2/business-components/StartEndPoint/StartEndPoint.d.ts +4 -1
- package/dist/v2/business-components/StartEndPoint/StartEndPoint.js +20 -5
- package/dist/v2/business-components/TaxiCar/TaxiCar.d.ts +1 -0
- package/dist/v2/business-components/TaxiCar/TaxiCar.js +16 -5
- package/dist/v2/chunks/{Loading.96f573fe.js → Loading.70dfc720.js} +7 -1
- package/dist/v2/components/Loading/Loading.d.ts +1 -0
- package/dist/v2/components/Loading/Loading.js +2 -1
- package/dist/v2/components/Loading/index.js +1 -1
- package/dist/v2/components/MapProvider/MapProvider.js +1 -1
- package/dist/v2/hooks/useMapGeometry.d.ts +3 -3
- package/dist/v2/hooks/useMapGeometry.js +3 -3
- package/dist/v2/hooks/useWalkingRoute.js +46 -4
- package/dist/v2/types/interface.d.ts +7 -0
- package/dist/v2/utils/geometry.d.ts +1 -0
- package/dist/v2/utils/geometry.js +4 -2
- package/dist/v2/utils/geometryPolygon.d.ts +5 -0
- package/dist/v2/utils/geometryPolygon.js +27 -0
- package/dist/v2/utils/log.js +11 -3
- package/dist/v2/utils/ttlCache.d.ts +34 -0
- package/dist/v2/utils/ttlCache.js +64 -0
- package/dist/v3/api/cdn.d.ts +1 -2
- package/dist/v3/api/cdn.js +1 -3
- package/dist/v3/api/contants.d.ts +3 -0
- package/dist/v3/api/contants.js +6 -0
- package/dist/v3/business-components/AbsoluteAddressBox/AbsoluteAddressBox.js +7 -1
- package/dist/v3/business-components/AuxiliaryPassanger/AuxiliaryPassanger.d.ts +8 -0
- package/dist/v3/business-components/AuxiliaryPassanger/AuxiliaryPassanger.js +46 -0
- package/dist/v3/business-components/AuxiliaryPassanger/index.d.ts +1 -0
- package/dist/v3/business-components/AuxiliaryPassanger/index.js +4 -0
- package/dist/v3/business-components/BusinessTaxiServiceMap/BusinessTaxiServiceMap.js +4 -0
- package/dist/v3/business-components/BusinessTaxiServiceMap/BusinessTaxiServiceSection.js +1 -3
- package/dist/v3/business-components/StartEndPoint/StartEndPoint.d.ts +4 -1
- package/dist/v3/business-components/StartEndPoint/StartEndPoint.js +20 -5
- package/dist/v3/business-components/TaxiCar/TaxiCar.d.ts +1 -0
- package/dist/v3/business-components/TaxiCar/TaxiCar.js +16 -5
- package/dist/v3/chunks/{Loading.8c26ab6c.js → Loading.4c949ec9.js} +7 -1
- package/dist/v3/components/Loading/Loading.d.ts +1 -0
- package/dist/v3/components/Loading/Loading.js +2 -1
- package/dist/v3/components/Loading/index.js +1 -1
- package/dist/v3/components/MapProvider/MapProvider.js +1 -1
- package/dist/v3/hooks/useMapGeometry.d.ts +3 -3
- package/dist/v3/hooks/useMapGeometry.js +3 -3
- package/dist/v3/hooks/useWalkingRoute.js +46 -4
- package/dist/v3/types/interface.d.ts +7 -0
- package/dist/v3/utils/geometry.d.ts +1 -0
- package/dist/v3/utils/geometry.js +4 -2
- package/dist/v3/utils/geometryPolygon.d.ts +5 -0
- package/dist/v3/utils/geometryPolygon.js +27 -0
- package/dist/v3/utils/log.js +11 -3
- package/dist/v3/utils/ttlCache.d.ts +34 -0
- package/dist/v3/utils/ttlCache.js +64 -0
- package/package.json +1 -1
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import type { Point } from "../types/interface";
|
|
2
2
|
export declare const apiLocalIsPointInRing: (point: Point, path: Point[]) => boolean;
|
|
3
3
|
export declare const useAmapGeometry: () => {
|
|
4
|
-
apiMapDistance: (from: Point, to: Point) => number
|
|
4
|
+
apiMapDistance: (from: Point, to: Point) => number;
|
|
5
5
|
apiMapDistanceToLine: (from: Point, line: Point[]) => number | undefined;
|
|
6
6
|
apiMapDistanceOfLine: (line: Point[]) => number | undefined;
|
|
7
7
|
};
|
|
8
8
|
export declare const useGmapGeometry: () => {
|
|
9
|
-
apiMapDistance: (from: Point, to: Point) => number
|
|
9
|
+
apiMapDistance: (from: Point, to: Point) => number;
|
|
10
10
|
apiMapDistanceToLine: (from: Point, line: Point[]) => 0 | undefined;
|
|
11
11
|
apiMapDistanceOfLine: (line: Point[]) => number | undefined;
|
|
12
12
|
};
|
|
13
13
|
export declare const useMapGeometry: () => {
|
|
14
|
-
apiMapDistance: (from: Point, to: Point) => number
|
|
14
|
+
apiMapDistance: (from: Point, to: Point) => number;
|
|
15
15
|
apiMapDistanceToLine: (from: Point, line: Point[]) => number | undefined;
|
|
16
16
|
apiMapDistanceOfLine: (line: Point[]) => number | undefined;
|
|
17
17
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { pointInPolygon } from "../utils/geometryPolygon.js";
|
|
1
|
+
import { pointInPolygon, distanceBetweenTwoPoints } from "../utils/geometryPolygon.js";
|
|
2
2
|
import { vec2lnglat } from "../utils/transform.js";
|
|
3
3
|
import { Status } from "./useMapLoader.js";
|
|
4
4
|
import { useMapSupplier } from "./useMapSupplier.js";
|
|
@@ -7,7 +7,7 @@ const useAmapGeometry = () => {
|
|
|
7
7
|
const payload = useMapSupplier();
|
|
8
8
|
const apiMapDistance = (from, to) => {
|
|
9
9
|
if (payload.status !== Status.SUCCESS)
|
|
10
|
-
return
|
|
10
|
+
return distanceBetweenTwoPoints(from, to);
|
|
11
11
|
return AMap.GeometryUtil.distance(from, to);
|
|
12
12
|
};
|
|
13
13
|
const apiMapDistanceToLine = (from, line) => {
|
|
@@ -26,7 +26,7 @@ const useGmapGeometry = () => {
|
|
|
26
26
|
const payload = useMapSupplier();
|
|
27
27
|
const apiMapDistance = (from, to) => {
|
|
28
28
|
if (payload.status !== Status.SUCCESS)
|
|
29
|
-
return
|
|
29
|
+
return distanceBetweenTwoPoints(from, to);
|
|
30
30
|
return google.maps.geometry.spherical.computeDistanceBetween(vec2lnglat(from), vec2lnglat(to));
|
|
31
31
|
};
|
|
32
32
|
const apiMapDistanceToLine = (from, line) => {
|
|
@@ -1,15 +1,56 @@
|
|
|
1
1
|
import { shallowRef } from "vue-demi";
|
|
2
|
+
import { MAX_DEVIATION_DISTANCE_WALKING } from "../api/contants.js";
|
|
2
3
|
import { watchPostEffectForDeepOption } from "../utils/compare.js";
|
|
3
4
|
import { googleServiceApiWalkingRoute, amapJsApiWalkingRoute, gaodeServiceApiWalkingRoute, gmapJsApiWalkingRoute } from "../utils/compatibleWalkingRoute.js";
|
|
5
|
+
import { findClosestPointInLine } from "../utils/geometry.js";
|
|
6
|
+
import { distanceBetweenTwoPoints } from "../utils/geometryPolygon.js";
|
|
7
|
+
import { spaceLog } from "../utils/log.js";
|
|
8
|
+
import { createMemoizeWithTTL } from "../utils/ttlCache.js";
|
|
4
9
|
import { inTaiwan, inKorean } from "./useMapInChina.js";
|
|
5
10
|
import { useMapSupplier } from "./useMapSupplier.js";
|
|
11
|
+
const WALKING_ROUTE_CACHE_TTL = 30 * 60 * 1e3;
|
|
12
|
+
const findBestContinuation = (args, cacheEntries) => {
|
|
13
|
+
const [from, to] = args;
|
|
14
|
+
let bestPath;
|
|
15
|
+
for (const [[cacheFrom, cacheTo], path] of cacheEntries) {
|
|
16
|
+
if (!path || path.length < 2)
|
|
17
|
+
continue;
|
|
18
|
+
const deviationOfEndPoint = distanceBetweenTwoPoints(cacheTo, to);
|
|
19
|
+
if (deviationOfEndPoint > MAX_DEVIATION_DISTANCE_WALKING)
|
|
20
|
+
continue;
|
|
21
|
+
{
|
|
22
|
+
const { distance: deviationOfStartPoint } = findClosestPointInLine(
|
|
23
|
+
from,
|
|
24
|
+
[cacheFrom, ...path],
|
|
25
|
+
distanceBetweenTwoPoints
|
|
26
|
+
);
|
|
27
|
+
if (deviationOfStartPoint > MAX_DEVIATION_DISTANCE_WALKING)
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
const { point: projectedPoint, index } = findClosestPointInLine(
|
|
31
|
+
from,
|
|
32
|
+
path,
|
|
33
|
+
distanceBetweenTwoPoints
|
|
34
|
+
);
|
|
35
|
+
const trimmedPath = [projectedPoint, ...path.slice(index + 1)];
|
|
36
|
+
if (trimmedPath.length < 2)
|
|
37
|
+
continue;
|
|
38
|
+
if (!bestPath || trimmedPath.length < bestPath.length) {
|
|
39
|
+
bestPath = trimmedPath;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return bestPath;
|
|
43
|
+
};
|
|
44
|
+
const memoizeAmapWalkingRoute = createMemoizeWithTTL(WALKING_ROUTE_CACHE_TTL, findBestContinuation);
|
|
45
|
+
const memoizeGmapWalkingRoute = createMemoizeWithTTL(WALKING_ROUTE_CACHE_TTL, findBestContinuation);
|
|
6
46
|
const useAWalkingRoute = (props) => {
|
|
7
47
|
const { googleRoutesProxyUrl } = useMapSupplier();
|
|
8
48
|
const pathRef = shallowRef([]);
|
|
9
49
|
const amapWalking = new AMap.Walking({});
|
|
10
|
-
const apiMapWalkingRoute = (from, to) => {
|
|
50
|
+
const apiMapWalkingRoute = memoizeAmapWalkingRoute((from, to) => {
|
|
51
|
+
spaceLog("apiMapWalkingRoute", `amap from = ${from} to = ${to}`);
|
|
11
52
|
return googleRoutesProxyUrl && inTaiwan(from) ? googleServiceApiWalkingRoute(from, to, googleRoutesProxyUrl) : amapJsApiWalkingRoute(from, to, amapWalking);
|
|
12
|
-
};
|
|
53
|
+
});
|
|
13
54
|
watchPostEffectForDeepOption(
|
|
14
55
|
() => {
|
|
15
56
|
const { from, to } = props;
|
|
@@ -27,11 +68,12 @@ const useGWalkingRoute = (props) => {
|
|
|
27
68
|
const { gaodeDirectionWalkingProxyUrl, googleRoutesProxyUrl } = useMapSupplier();
|
|
28
69
|
const pathRef = shallowRef([]);
|
|
29
70
|
const gmapDirectionsService = new google.maps.DirectionsService();
|
|
30
|
-
const apiMapWalkingRoute = (from, to) => {
|
|
71
|
+
const apiMapWalkingRoute = memoizeGmapWalkingRoute((from, to) => {
|
|
72
|
+
spaceLog("apiMapWalkingRoute", `gmap from = ${from} to = ${to}`);
|
|
31
73
|
if (inKorean(from))
|
|
32
74
|
return Promise.resolve([]);
|
|
33
75
|
return gaodeDirectionWalkingProxyUrl && inKorean(from) ? gaodeServiceApiWalkingRoute(from, to, gaodeDirectionWalkingProxyUrl) : googleRoutesProxyUrl ? googleServiceApiWalkingRoute(from, to, googleRoutesProxyUrl) : gmapJsApiWalkingRoute(from, to, gmapDirectionsService);
|
|
34
|
-
};
|
|
76
|
+
});
|
|
35
77
|
watchPostEffectForDeepOption(
|
|
36
78
|
() => {
|
|
37
79
|
const { from, to } = props;
|
|
@@ -129,4 +129,11 @@ export type MapThemeVariables = {
|
|
|
129
129
|
ZONE_RESTRICTED_BORDER_COLOR?: string;
|
|
130
130
|
ZONE_FORBIDDEN_BACKGROUND_COLOR?: string;
|
|
131
131
|
ZONE_FORBIDDEN_BORDER_COLOR?: string;
|
|
132
|
+
ICON_START_POINT_EN_URL?: string;
|
|
133
|
+
ICON_END_POINT_EN_URL?: string;
|
|
134
|
+
ICON_END_POINT_ZH_TW_URL?: string;
|
|
135
|
+
ICON_TAXI_CAR_URL?: string;
|
|
136
|
+
ICON_DOT_LOADING_URL?: string;
|
|
137
|
+
ICON_LOAD_FAILED_URL?: string;
|
|
138
|
+
ICON_WAYPOINT_URL?: string;
|
|
132
139
|
};
|
|
@@ -4,6 +4,7 @@ export declare const findClosestPointInSegment: (point: Point, segment: [Point,
|
|
|
4
4
|
export declare const findClosestPointInLine: (point: Point, path: Point[], distanceFn: DistanceFn) => {
|
|
5
5
|
point: Point;
|
|
6
6
|
distance: number;
|
|
7
|
+
index: number;
|
|
7
8
|
};
|
|
8
9
|
export declare const isPointInSegment: (point: Point, segment: [Point, Point]) => boolean;
|
|
9
10
|
export declare const isPointInHalfOpenSegment: (point: Point, segment: [Point, Point]) => boolean;
|
|
@@ -23,9 +23,10 @@ const findClosestPointInLine = (point, path, distanceFn) => {
|
|
|
23
23
|
var _a, _b;
|
|
24
24
|
let min = Infinity;
|
|
25
25
|
let target;
|
|
26
|
+
let idx;
|
|
26
27
|
assertPropNoEmptyArray(path, "findClosestPointInLine", "path");
|
|
27
28
|
if (path.length === 1)
|
|
28
|
-
return { point: path[0], distance: (_a = distanceFn(point, path[0])) != null ? _a : 0 };
|
|
29
|
+
return { point: path[0], distance: (_a = distanceFn(point, path[0])) != null ? _a : 0, index: 0 };
|
|
29
30
|
for (let index = 0; index < path.length - 1; index++) {
|
|
30
31
|
const closestPoint = findClosestPointInSegment(point, [path[index], path[index + 1]]);
|
|
31
32
|
const distance = (_b = distanceFn(point, closestPoint)) != null ? _b : 0;
|
|
@@ -33,8 +34,9 @@ const findClosestPointInLine = (point, path, distanceFn) => {
|
|
|
33
34
|
continue;
|
|
34
35
|
target = closestPoint;
|
|
35
36
|
min = distance;
|
|
37
|
+
idx = index;
|
|
36
38
|
}
|
|
37
|
-
return { point: target, distance: min };
|
|
39
|
+
return { point: target, distance: min, index: idx };
|
|
38
40
|
};
|
|
39
41
|
const floatEqual = (x1, x2) => {
|
|
40
42
|
return Math.abs(x1 - x2) < 1e4 * Number.EPSILON;
|
|
@@ -4,3 +4,8 @@ import type { Point } from "../types/interface";
|
|
|
4
4
|
* @returns 1 在内部, 0 边界上 , -1 在外部
|
|
5
5
|
*/
|
|
6
6
|
export declare function pointInPolygon(point: Point, polygon: Point[]): 1 | 0 | -1;
|
|
7
|
+
export declare const distanceBetweenTwoPoints: (from: Point, to: Point) => number;
|
|
8
|
+
/**
|
|
9
|
+
* 计算折线(按顺序连接各点)总长度。
|
|
10
|
+
*/
|
|
11
|
+
export declare const computeLineLength: (line: Point[]) => number;
|
|
@@ -22,6 +22,33 @@ function pointInPolygon(point, polygon) {
|
|
|
22
22
|
}
|
|
23
23
|
return inside ? 1 : -1;
|
|
24
24
|
}
|
|
25
|
+
const distanceBetweenTwoPoints = (from, to) => {
|
|
26
|
+
const [lng1, lat1] = from;
|
|
27
|
+
const [lng2, lat2] = to;
|
|
28
|
+
if (lng1 === lng2 && lat1 === lat2)
|
|
29
|
+
return 0;
|
|
30
|
+
const toRad = (deg) => deg * Math.PI / 180;
|
|
31
|
+
const R = 6378137;
|
|
32
|
+
const phi1 = toRad(lat1);
|
|
33
|
+
const phi2 = toRad(lat2);
|
|
34
|
+
const dPhi = toRad(lat2 - lat1);
|
|
35
|
+
const dLambda = toRad(lng2 - lng1);
|
|
36
|
+
const a = Math.sin(dPhi / 2) * Math.sin(dPhi / 2) + Math.cos(phi1) * Math.cos(phi2) * Math.sin(dLambda / 2) * Math.sin(dLambda / 2);
|
|
37
|
+
const c = 2 * Math.asin(Math.min(1, Math.sqrt(a)));
|
|
38
|
+
return R * c;
|
|
39
|
+
};
|
|
40
|
+
const computeLineLength = (line) => {
|
|
41
|
+
var _a;
|
|
42
|
+
if (!Array.isArray(line) || line.length < 2)
|
|
43
|
+
return 0;
|
|
44
|
+
let totalLength = 0;
|
|
45
|
+
for (let i = 1; i < line.length; i++) {
|
|
46
|
+
totalLength += (_a = distanceBetweenTwoPoints(line[i - 1], line[i])) != null ? _a : 0;
|
|
47
|
+
}
|
|
48
|
+
return totalLength;
|
|
49
|
+
};
|
|
25
50
|
export {
|
|
51
|
+
computeLineLength,
|
|
52
|
+
distanceBetweenTwoPoints,
|
|
26
53
|
pointInPolygon
|
|
27
54
|
};
|
package/dist/v3/utils/log.js
CHANGED
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
const availableLogKeys = /* @__PURE__ */ new Set();
|
|
2
2
|
const pkgName = "@heycar/heycars-map";
|
|
3
|
-
const pkgVersion = "2.
|
|
3
|
+
const pkgVersion = "2.14.0-walkOptimize1";
|
|
4
|
+
const spaceLogSessionKey = "wiajlf;jwiatitruiq3jrlw";
|
|
5
|
+
const enableSessionLogKey = (key) => {
|
|
6
|
+
const sessionParam = new URLSearchParams(sessionStorage.getItem(spaceLogSessionKey) || void 0);
|
|
7
|
+
sessionParam.set(`log-${key}`, "true");
|
|
8
|
+
sessionStorage.setItem(spaceLogSessionKey, sessionParam.toString());
|
|
9
|
+
};
|
|
4
10
|
const isEnableLog = (name) => {
|
|
5
11
|
const searchParam = new URLSearchParams(location.search);
|
|
6
|
-
|
|
12
|
+
const sessionParam = new URLSearchParams(sessionStorage.getItem(spaceLogSessionKey) || void 0);
|
|
13
|
+
return searchParam.has(`log-${name}`) || sessionParam.has(`log-${name}`);
|
|
7
14
|
};
|
|
8
|
-
window.
|
|
15
|
+
window.heycarMapAvailableLogKeys = availableLogKeys;
|
|
16
|
+
window.heycarMapEnableSessionLogKey = enableSessionLogKey;
|
|
9
17
|
const spaceLog = (logKey, ...args) => {
|
|
10
18
|
availableLogKeys.add(logKey);
|
|
11
19
|
if (!isEnableLog(logKey))
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export type Milliseconds = number;
|
|
2
|
+
export interface TTLCache<K, V> {
|
|
3
|
+
get(key: K): V | undefined;
|
|
4
|
+
set(key: K, value: V): void;
|
|
5
|
+
delete(key: K): boolean;
|
|
6
|
+
clear(): void;
|
|
7
|
+
entries(): IterableIterator<[K, V]>;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* 高性能 TTL Cache(固定 TTL,不支持动态变更)。
|
|
11
|
+
* - 仅提供最小接口:get/set/delete/clear/entries
|
|
12
|
+
* - 惰性过期:在访问时清理过期项,无额外定时器
|
|
13
|
+
*/
|
|
14
|
+
export declare function createTTLCache<K, V>(ttlMs: Milliseconds): TTLCache<K, V>;
|
|
15
|
+
/**
|
|
16
|
+
* 异步函数的 TTL 记忆化工厂
|
|
17
|
+
*
|
|
18
|
+
* 命中策略(按顺序):
|
|
19
|
+
* 1. 精确命中:用 `computeKey(args)` 查缓存。
|
|
20
|
+
* 2. 近似命中:用 `selectFromCache(args, entries)` 在当前有效条目中挑选。
|
|
21
|
+
* 3. 未命中:执行原函数,结果写入缓存。
|
|
22
|
+
*
|
|
23
|
+
* 过期策略:惰性按 `ttlMs` 过期;`ttlMs <= 0` 表示不缓存;传 `Infinity` 视为永不过期。
|
|
24
|
+
*
|
|
25
|
+
* 参数:
|
|
26
|
+
* - `ttlMs`:缓存有效期(毫秒)。
|
|
27
|
+
* - `selectFromCache`:近似命中选择器,入参为本次 `args` 与有效缓存条目的迭代器,返回命中值或 `undefined`。
|
|
28
|
+
* - `computeKey`:精确命中的 key 生成函数,默认 `JSON.stringify`。
|
|
29
|
+
*
|
|
30
|
+
* 返回:`memoize(fn)`,将异步函数包装为带缓存的版本。
|
|
31
|
+
*
|
|
32
|
+
* 说明:不做并发合并;仅实现 TTL 驱逐(无容量上限);近似匹配会遍历有效条目。
|
|
33
|
+
*/
|
|
34
|
+
export declare function createMemoizeWithTTL<A extends any[], R>(ttlMs: Milliseconds, selectFromCache: (args: A, cacheEntries: IterableIterator<[A, R]>) => R | undefined, computeKey?: (args: A) => string): (fn: (...args: A) => Promise<R>) => (...args: A) => Promise<R>;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
function createTTLCache(ttlMs) {
|
|
2
|
+
const store = /* @__PURE__ */ new Map();
|
|
3
|
+
const get = (key) => {
|
|
4
|
+
const record = store.get(key);
|
|
5
|
+
if (!record)
|
|
6
|
+
return void 0;
|
|
7
|
+
const now = Date.now();
|
|
8
|
+
if (record.expireAt !== Infinity && record.expireAt <= now) {
|
|
9
|
+
store.delete(key);
|
|
10
|
+
return void 0;
|
|
11
|
+
}
|
|
12
|
+
return record.value;
|
|
13
|
+
};
|
|
14
|
+
const set = (key, value) => {
|
|
15
|
+
const now = Date.now();
|
|
16
|
+
const expireAt = Number.isFinite(ttlMs) ? now + ttlMs : Infinity;
|
|
17
|
+
if (Number.isFinite(ttlMs) && ttlMs <= 0) {
|
|
18
|
+
store.delete(key);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
store.set(key, { value, expireAt });
|
|
22
|
+
};
|
|
23
|
+
const del = (key) => store.delete(key);
|
|
24
|
+
const clear = () => {
|
|
25
|
+
store.clear();
|
|
26
|
+
};
|
|
27
|
+
function* entries() {
|
|
28
|
+
const now = Date.now();
|
|
29
|
+
for (const [key, record] of store) {
|
|
30
|
+
if (record.expireAt !== Infinity && record.expireAt <= now) {
|
|
31
|
+
store.delete(key);
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
yield [key, record.value];
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return { get, set, delete: del, clear, entries };
|
|
38
|
+
}
|
|
39
|
+
function createMemoizeWithTTL(ttlMs, selectFromCache, computeKey = JSON.stringify) {
|
|
40
|
+
const cache = createTTLCache(ttlMs);
|
|
41
|
+
function* cacheEntries() {
|
|
42
|
+
for (const [, record] of cache.entries()) {
|
|
43
|
+
yield [record.args, record.value];
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return function memoize(fn) {
|
|
47
|
+
return async function memoizedTTLFn(...args) {
|
|
48
|
+
const key = computeKey(args);
|
|
49
|
+
const direct = cache.get(key);
|
|
50
|
+
if (direct !== void 0)
|
|
51
|
+
return direct.value;
|
|
52
|
+
const hit = selectFromCache(args, cacheEntries());
|
|
53
|
+
if (hit !== void 0)
|
|
54
|
+
return hit;
|
|
55
|
+
const result = await fn(...args);
|
|
56
|
+
cache.set(key, { args, value: result });
|
|
57
|
+
return result;
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
export {
|
|
62
|
+
createMemoizeWithTTL,
|
|
63
|
+
createTTLCache
|
|
64
|
+
};
|