mirror of https://github.com/msgbyte/tailchat
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
697 B
TypeScript
30 lines
697 B
TypeScript
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;
|
|
}
|