优化
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
import { getLinkPreview } from 'link-preview-js';
|
||||
|
||||
/**
|
||||
* 请求管理
|
||||
*/
|
||||
const cacheRequestList: Record<string, Promise<any>> = {};
|
||||
|
||||
/**
|
||||
* 获取网页元数据信息
|
||||
* @param url 网址
|
||||
* @returns
|
||||
*/
|
||||
export async function fetchLinkPreview(url: string): Promise<any> {
|
||||
if (cacheRequestList[url]) {
|
||||
// 如果有正在请求的信息
|
||||
return Promise.resolve(cacheRequestList[url]);
|
||||
}
|
||||
|
||||
const promise = getLinkPreview(url);
|
||||
cacheRequestList[url] = promise;
|
||||
|
||||
return Promise.resolve(promise).finally(() => {
|
||||
setTimeout(() => {
|
||||
delete cacheRequestList[url];
|
||||
}, 2 * 1000); // 窗口期, 请求完毕后2s内依旧会复用原来的接口
|
||||
});
|
||||
|
||||
// return promise;
|
||||
}
|
||||
Reference in New Issue
Block a user