Skip to content

Commit

Permalink
页面导航解耦组件解耦 (#9)
Browse files Browse the repository at this point in the history
* 升级 io 缓存办法

确保首次请求即便未返回,重复的请求也指向同一份数据

* 页面导航解耦组件解耦
  • Loading branch information
MotooriKashin authored Aug 2, 2024
1 parent e30d113 commit 2fea563
Show file tree
Hide file tree
Showing 21 changed files with 822 additions and 372 deletions.
9 changes: 4 additions & 5 deletions src/io/com/bilibili/account/api/member/getCardByMid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import { Account } from "../../idenx";
export async function getCardByMid(
mid: string | number,
) {
if (CATCH[mid]) return CATCH[mid];
const url = new URL(Account + '/api/member/getCardByMid');
url.searchParams.set('mid', <string>mid);
const response = await fetch(url);
const json = await response.json();
return <IGetCardByMid>(CATCH[mid] = json.card);
CATCH[mid] || (CATCH[mid] = fetch(url));
const json = await (await CATCH[mid]).clone().json();
return <IGetCardByMid>json.card;
}

const CATCH: Record<string, IGetCardByMid> = {};
const CATCH: Record<string, Promise<Response>> = {};

interface IGetCardByMid {
DisplayRank: string;
Expand Down
7 changes: 3 additions & 4 deletions src/io/com/bilibili/api/pgc/season/web/related/recommend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import { Api } from "../../../..";

/** 获取番剧推荐 */
export async function recommend(season_id: number) {
if (CATCH[season_id]) return CATCH[season_id];
const url = new URL(Api + '/pgc/season/web/related/recommend');
url.searchParams.set('season_id', <any>season_id);
const response = await fetch(url, { credentials: 'include' });
return CATCH[season_id] = <IRecommend[]>(await response.json()).data.season;
CATCH[season_id] || (CATCH[season_id] = fetch(url, { credentials: 'include' }));
return <IRecommend[]>(await (await CATCH[season_id]).clone().json()).data.season;
}

/** 同一请求缓存 */
const CATCH: Record<number, IRecommend[]> = {};
const CATCH: Record<number, Promise<Response>> = {};

interface IRecommend {
actor: string;
Expand Down
7 changes: 3 additions & 4 deletions src/io/com/bilibili/api/pgc/view/v2/app/season.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ import { EP_STATUS } from "../../../../../../../stat";
/** APP端pgc信息,或可用于查询区域限制番剧 */
export async function pgcAppSeason({ season_id, ep_id, access_key }: IPgcAppSeasonIn) {
const key = ep_id ? `ep_id=${ep_id}` : `season_id=${season_id}`;
if (CATCH[key]) return CATCH[key];
const url = new URL(Api + '/pgc/view/v2/app/season');
ep_id ? url.searchParams.set('ep_id', <any>ep_id) : (season_id && url.searchParams.set('season_id', <any>season_id));
access_key && url.searchParams.set('access_key', <any>access_key);
const response = await fetch(sign(url, '1d8b6e7d45233436'), access_key ? undefined : { credentials: 'include' });
return <IPgcAppSeason>(CATCH[key] = (await response.json()).data);
CATCH[key] || (CATCH[key] = fetch(sign(url, '1d8b6e7d45233436'), access_key ? undefined : { credentials: 'include' }));
return <IPgcAppSeason>(await ((await CATCH[key]).clone()).json()).data;
}

/** 同一请求缓存 */
const CATCH: Record<string, IPgcAppSeason> = {};
const CATCH: Record<string, Promise<Response>> = {};

interface IPgcAppSeasonIn {
/** 必须二选一 */
Expand Down
7 changes: 3 additions & 4 deletions src/io/com/bilibili/api/pgc/view/web/season.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import { EP_STATUS } from "../../../../../../stat";

export async function season(params: ISeasonIn) {
const param = params.ep_id ? `ep_id=${params.ep_id}` : `season_id=${params.season_id}`;
if (CATCH[param]) return CATCH[param];
const response = await fetch(Api + `/pgc/view/web/season?${param}`, { credentials: 'include' });
return CATCH[param] = <ISeason>(await response.json()).result;
CATCH[param] || (CATCH[param] = fetch(Api + `/pgc/view/web/season?${param}`, { credentials: 'include' }));
return <ISeason>(await (await CATCH[param]).clone().json()).result;
}

/** 同一请求缓存 */
const CATCH: Record<string, ISeason> = {};
const CATCH: Record<string, Promise<Response>> = {};

interface ISeasonIn {
ep_id?: number;
Expand Down
7 changes: 3 additions & 4 deletions src/io/com/bilibili/api/pgc/view/web/season/user/section.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { Api } from "../../../../..";

export async function pgcSection(season_id: number) {
if (CATCH[season_id]) return CATCH[season_id];
const url = new URL(Api + '/pgc/web/season/section');
url.searchParams.set('season_id', <any>season_id);
const response = await fetch(url, { credentials: 'include' });
return <IPgcSection>(CATCH[season_id] = (await response.json()).result);
CATCH[season_id] || (CATCH[season_id] = fetch(url, { credentials: 'include' }));
return <IPgcSection>(await (await CATCH[season_id]).clone().json()).result;
}

const CATCH: Record<number, IPgcSection> = {};
const CATCH: Record<number, Promise<Response>> = {};

interface IPgcSection {
main_section: {
Expand Down
7 changes: 3 additions & 4 deletions src/io/com/bilibili/api/pugv/view/web/season.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import { Api } from "../../..";
/** 课程基本信息 */
export async function pugvSeason(params: ISeasonIn) {
const param = params.season_id ? `season_id=${params.season_id}` : `ep_id=${params.ep_id}`;
if (CATCH[param]) return CATCH[param];
const response = await fetch(Api + `/pugv/view/web/season?${param}`, { credentials: 'include' });
return CATCH[param] = <ISeason>(await response.json()).data;
CATCH[param] || (CATCH[param] = fetch(Api + `/pugv/view/web/season?${param}`, { credentials: 'include' }));
return <ISeason>(await (await CATCH[param]).clone().json()).data;
}

/** 同一请求缓存 */
const CATCH: Record<string, ISeason> = {};
const CATCH: Record<string, Promise<Response>> = {};

interface ISeasonIn {
ep_id?: number;
Expand Down
9 changes: 4 additions & 5 deletions src/io/com/bilibili/api/x/article/cards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ export async function cards(param: ICardsIn | string[]) {
}
if (!arr.length) throw new Error('输入参数不能为空!');
const ids = arr.join(',');
if (CATCH[ids]) return CATCH[ids];
const url = new URL(Api + '/x/article/cards');
url.searchParams.set('ids', ids);
const response = await fetch(url);
const json = await response.json();
return CATCH[ids] = <Record<string, ICardsOut>>json.data;
CATCH[ids] || (CATCH[ids] = fetch(url));
const json = await (await CATCH[ids]).clone().json();
return <Record<string, ICardsOut>>json.data;
}

/** 同一请求缓存 */
const CATCH: Record<string, Record<string, ICardsOut>> = {};
const CATCH: Record<string, Promise<Response>> = {};

interface ICardsIn {
av?: number | number[],
Expand Down
7 changes: 3 additions & 4 deletions src/io/com/bilibili/api/x/player/pagelist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import { Api } from "../..";
* @param aid
*/
export async function pagelist(aid: number) {
if (CATCH[aid]) return CATCH[aid];
const url = new URL(Api + '/x/player/pagelist');
url.searchParams.set('aid', <any>aid);
const response = await fetch(url, { credentials: 'include' });
return CATCH[aid] = <IPagelist[]>(await response.json()).data;
CATCH[aid] || (CATCH[aid] = fetch(url, { credentials: 'include' }));
return <IPagelist[]>(await (await CATCH[aid]).clone().json()).data;
}

/** 同一请求缓存 */
const CATCH: Record<number, IPagelist[]> = {};
const CATCH: Record<number, Promise<Response>> = {};

interface IPagelist {
cid: number;
Expand Down
9 changes: 4 additions & 5 deletions src/io/com/bilibili/api/x/space/acc/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import { Api } from "../../..";

export async function accInfo(mid: string | number,
) {
if (CATCH[mid]) return CATCH[mid];
const url = new URL(Api + '/x/space/acc/info');
url.searchParams.set('mid', <string>mid);
const response = await fetch(url, { credentials: 'include' });
const json = await response.json();
return <IAccInfo>(CATCH[mid] = json.data);
CATCH[mid] || (CATCH[mid] = fetch(url, { credentials: 'include' }));
const json = await (await CATCH[mid]).clone().json();
return <IAccInfo>json.data;
}

const CATCH: Record<string, IAccInfo> = {};
const CATCH: Record<string, Promise<Response>> = {};

interface IAccInfo {
birthday: string;
Expand Down
7 changes: 5 additions & 2 deletions src/io/com/bilibili/api/x/v2/history/toview/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import { Api } from "../../../..";
export async function toviewWeb(ps?: number) {
const url = new URL(Api + '/x/v2/history/toview/web');
ps && url.searchParams.set('ps', <any>ps);
const response = await fetch(url, { credentials: 'include' });
return <IToviewWeb[]>(await response.json()).data.list;
CATCH[ps || -1] || (CATCH[ps || -1] = fetch(url, { credentials: 'include' }));
return <IToviewWeb[]>(await (await CATCH[ps || -1]).clone().json()).data.list;
}

/** 同一请求缓存 */
const CATCH: Record<number, Promise<Response>> = {};

export interface IToviewWeb {
add_at: number;
aid: number;
Expand Down
7 changes: 3 additions & 4 deletions src/io/com/bilibili/api/x/v3/fav/resource/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export async function favResourceList(
pn = 1,
) {
const key = `${media_id},${pn}`;
if (CATCH[key]) return CATCH[key];
const url = new URL(Api + '/x/v3/fav/resource/list');
url.searchParams.set('media_id', <any>media_id);
url.searchParams.set('pn', <any>pn);
Expand All @@ -21,11 +20,11 @@ export async function favResourceList(
url.searchParams.set('type', '0');
url.searchParams.set('tid', '0');
url.searchParams.set('platform', 'web');
const response = await fetch(url, { credentials: 'include' });
return <IFavResourceList>(CATCH[key] = (await response.json()).data);
CATCH[key] || (CATCH[key] = fetch(url, { credentials: 'include' }));
return <IFavResourceList>(await (await CATCH[key]).clone().json()).data;
}

const CATCH: Record<string, IFavResourceList> = {};
const CATCH: Record<string, Promise<Response>> = {};

interface IFavResourceList {
has_more: boolean;
Expand Down
7 changes: 3 additions & 4 deletions src/io/com/bilibili/api/x/web-interface/archive/related.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import { Api } from "../../..";
* @param aid 本视频aid
*/
export async function related(aid: number) {
if (CATCH[aid]) return CATCH[aid];
const url = new URL(Api + '/x/web-interface/archive/related');
url.searchParams.set('aid', <any>aid);
const response = await fetch(url, { credentials: 'include' });
return CATCH[aid] = <IRelated[]>(await response.json()).data;
CATCH[aid] || (CATCH[aid] = fetch(url, { credentials: 'include' }));
return <IRelated[]>(await (await CATCH[aid]).clone().json()).data;
}

/** 同一请求缓存 */
const CATCH: Record<number, IRelated[]> = {};
const CATCH: Record<number, Promise<Response>> = {};

export interface IRelated {
aid: number;
Expand Down
9 changes: 4 additions & 5 deletions src/io/com/bilibili/api/x/web-interface/card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@ import { Api } from "../..";
export async function card(
mid: string | number,
) {
if (CATCH[mid]) return CATCH[mid];
const url = new URL(Api + '/x/web-interface/card');
url.searchParams.set('mid', <string>mid);
url.searchParams.set('photo', '1');
const response = await fetch(url, { credentials: 'include' });
const json = await response.json();
return <ICard>(CATCH[mid] = json.data);
CATCH[mid] || (CATCH[mid] = fetch(url, { credentials: 'include' }));
const json = await (await CATCH[mid]).clone().json();
return <ICard>json.data;
}

card.noCatch = function (mid: string | number,) {
delete CATCH[mid];
}

const CATCH: Record<string, ICard> = {};
const CATCH: Record<string, Promise<Response>> = {};

interface ICard {
archive_count: number;
Expand Down
7 changes: 3 additions & 4 deletions src/io/com/bilibili/api/x/web-interface/view/detail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import { Api } from "../../..";
import { IRelated } from "../archive/related";

export async function detail(aid: number) {
if (CATCH[aid]) return CATCH[aid];
const url = new URL(Api + '/x/web-interface/view/detail');
url.searchParams.set('aid', <any>aid);
const response = await fetch(url, { credentials: 'include' });
return CATCH[aid] = <IDetail>(await response.json()).data;
CATCH[aid] || (CATCH[aid] = fetch(url, { credentials: 'include' }));
return <IDetail>(await (await CATCH[aid]).clone().json()).data;
}

/** 同一请求缓存 */
const CATCH: Record<number, IDetail> = {};
const CATCH: Record<number, Promise<Response>> = {};

interface IDetail {
Card: ICard;
Expand Down
7 changes: 3 additions & 4 deletions src/io/com/bilibili/api/x/web-show/page/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import { Api } from "../../..";
* @param resource_id 资源id
*/
export async function header(resource_id = 142) {
if (CATCH[resource_id]) return CATCH[resource_id];
const url = new URL(Api + '/x/web-show/page/header');
url.searchParams.set('resource_id', <any>resource_id);
const response = await fetch(url, { credentials: 'include' });
return CATCH[resource_id] = <IHeader>(await response.json()).data;
CATCH[resource_id] || (CATCH[resource_id] = fetch(url, { credentials: 'include' }));
return <IHeader>(await (await CATCH[resource_id]).clone().json()).data;
}

/** 同一请求缓存 */
const CATCH: Record<number, IHeader> = {};
const CATCH: Record<number, Promise<Response>> = {};

interface IHeader {
is_split_layer: number;
Expand Down
Loading

0 comments on commit 2fea563

Please sign in to comment.