Skip to content
This repository has been archived by the owner on Mar 24, 2024. It is now read-only.

Commit

Permalink
Refactor ProductGet and SignalR classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Aloento committed Feb 27, 2024
1 parent 1ea80c5 commit d313d33
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 33 deletions.
50 changes: 19 additions & 31 deletions src/ShopNet/Product/Get.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { useConst } from "@fluentui/react-hooks";
import { useAsyncEffect } from "ahooks";
import { useState } from "react";
import type { Logger } from "~/Helpers/Logger";
import { useSWR } from "~/Helpers/useSWR";
import { IComboItem } from "~/Pages/Admin/Product/Combo";
import type { IGallery } from "~/Pages/Gallery";
import { ProductData } from "./Data";
Expand Down Expand Up @@ -115,13 +113,16 @@ export abstract class ProductGet extends ProductData {
* @liveSafe
* @deprecated Use {@link usePhotoList} if possible.
*/
public static async PhotoList(prodId: number, pLog: Logger) {
public static async PhotoList(prodId: number, pLog: Logger): Promise<[ProductData.Photo[], string]> {
const log = pLog.With(...this.Log, "PhotoList");
const ids = await this.GetTimeCache<number[]>(prodId, this.photoList, (x) => x, prodId).catch(log.error);
return this.makePhotoList(prodId, ids || [], log);
}

private static async makePhotoList(prodId: number, ids: number[], log: Logger): Promise<[ProductData.Photo[], string]> {
const index = this.Index(prodId, this.photoList);
await this.getLocker(index);
this.reqPool.add(index);

const ids = await this.Invoke<number[]>(this.photoList, prodId)
.finally(() => this.reqPool.delete(index));

let list = [];
let cover = "";

Expand Down Expand Up @@ -150,31 +151,18 @@ export abstract class ProductGet extends ProductData {
/**
* @author Aloento
* @since 1.4.0
* @version 0.1.0
* @version 0.2.0
*/
public static usePhotoList(prodId: number, pLog: Logger) {
const log = useConst(() => pLog.With(...this.Log, "PhotoList"));
const [list, setList] = useState<ProductData.Photo[]>();
const [cover, setCover] = useState<string>();

const req = this.useTimeCache<number[]>(prodId, this.photoList, {
defaultParams: [prodId],
onError: log.error,
});

useAsyncEffect(async () => {
const ids = req.data;
if (!ids)
return;

const [list, cover] = await this.makePhotoList(prodId, ids, log);
setList(list);
setCover(cover);
}, [req.data]);
const req = useSWR(
this.Index(prodId, this.photoList),
() => this.PhotoList(prodId, pLog),
{
defaultParams: [prodId],
onError: pLog.error,
}
);

return {
...req,
data: [list, cover] as const
}
return req;
}
}
4 changes: 2 additions & 2 deletions src/ShopNet/SignalR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,14 @@ export abstract class SignalR {
* @since 1.3.5
* @version 0.1.0
*/
private static readonly reqPool = new Set<string>();
protected static readonly reqPool = new Set<string>();

/**
* @author Aloento
* @since 1.3.5
* @version 0.1.0
*/
private static async getLocker(key: string) {
protected static async getLocker(key: string) {
if (this.reqPool.has(key))
return new Promise<void>(res => {
const t = setTimeout(() => this.reqPool.delete(key), 10000);
Expand Down

0 comments on commit d313d33

Please sign in to comment.