diff --git a/src/ShopNet/Order/Get.ts b/src/ShopNet/Order/Get.ts index 8972b54..aa4e9b4 100644 --- a/src/ShopNet/Order/Get.ts +++ b/src/ShopNet/Order/Get.ts @@ -1,5 +1,5 @@ import { useConst } from "@fluentui/react-hooks"; -import { useLiveQuery } from "dexie-react-hooks"; +import { useState } from "react"; import { IComment } from "~/Components/Order/Comment"; import { ICartItem } from "~/Components/ShopCart"; import { Logger } from "~/Helpers/Logger"; @@ -78,8 +78,9 @@ export abstract class OrderGet extends OrderEntity { */ public static useItems(orderId: number, pLog: Logger) { const log = useConst(() => pLog.With(...this.Log, "Items")); + const [res, setRes] = useState([]); - const { data: meta } = this.useSWR< + this.useSWR< { Types: number[]; Quantity: number; @@ -90,63 +91,59 @@ export abstract class OrderGet extends OrderEntity { { defaultParams: [orderId], onError: log.error, - } - ); + async onSuccess(meta) { + const items: ICartItem[] = []; + let index = 0; - const res = useLiveQuery(async () => { - if (!meta) - return undefined; + for (const combo of meta) { + const variType: Record = {}; + let prodId = 0; - const items: ICartItem[] = []; - let index = 0; + for (const typeId of combo.Types) { + const type = await ProductData.Type(typeId); - for (const combo of meta) { - const variType: Record = {}; - let prodId = 0; + if (!type) { + log.warn(`[Mismatch] Type ${typeId} not found. Order : ${orderId}`); + continue; + } - for (const typeId of combo.Types) { - const type = await ProductData.Type(typeId); + const vari = await ProductData.Variant(type.VariantId); - if (!type) { - log.warn(`[Mismatch] Type ${typeId} not found. Order : ${orderId}`); - continue; - } + if (!vari) { + log.warn(`[Mismatch] Variant ${type.VariantId} not found. Type : ${typeId}, Order : ${orderId}`); + continue; + } - const vari = await ProductData.Variant(type.VariantId); + variType[vari.Name] = type.Name; + prodId = vari.ProductId; + } - if (!vari) { - log.warn(`[Mismatch] Variant ${type.VariantId} not found. Type : ${typeId}, Order : ${orderId}`); - continue; - } + const prod = await ProductData.Product(prodId); - variType[vari.Name] = type.Name; - prodId = vari.ProductId; - } - - const prod = await ProductData.Product(prodId); + if (!prod) { + log.warn(`[Mismatch] Product ${prodId} not found. Order : ${orderId}`); + continue; + } - if (!prod) { - log.warn(`[Mismatch] Product ${prodId} not found. Order : ${orderId}`); - continue; - } + const [_, cover] = await ProductGet.PhotoList(prodId, log); - const [_, cover] = await ProductGet.PhotoList(prodId, log); + if (!cover) + log.warn(`Product ${prodId} has no photo`); - if (!cover) - log.warn(`Product ${prodId} has no photo`); + items.push({ + Id: index++, + ProdId: prodId, + Cover: cover || "", + Name: prod.Name, + Type: variType, + Quantity: combo.Quantity, + }); + } - items.push({ - Id: index++, - ProdId: prodId, - Cover: cover || "", - Name: prod.Name, - Type: variType, - Quantity: combo.Quantity, - }); + setRes(items); + } } - - return items; - }); + ); return res; } diff --git a/src/ShopNet/SignalR.ts b/src/ShopNet/SignalR.ts index 8936078..14e069b 100644 --- a/src/ShopNet/SignalR.ts +++ b/src/ShopNet/SignalR.ts @@ -3,6 +3,7 @@ import { HubConnectionState } from "@microsoft/signalr"; import { useRequest } from "ahooks"; import { Options } from "ahooks/lib/useRequest/src/types"; import dayjs, { Dayjs } from "dayjs"; +import Dexie from "dexie"; import { Subject } from "rxjs"; import { EmptyResponseError, NotLoginError, NotTrueError } from "~/Helpers/Exceptions"; import type { Logger } from "~/Helpers/Logger"; @@ -124,15 +125,15 @@ export abstract class SignalR { // TODO:导致 LiveQuery 无限刷新 const update = async () => { - const res = await Promise.resolve(this.Invoke(methodName, key, find?.Version)); + const res = await Dexie.waitFor(this.Invoke(methodName, key, find?.Version)); if (res === true) { - setCache(find!); + setCache(find!) return find!; } if (!res) { - Shared.Sto.delete(index); + await Shared.Sto.delete(index); throw new EmptyResponseError(); } @@ -159,7 +160,7 @@ export abstract class SignalR { /** * @author Aloento - * @since 1.3.0 + * @since 1.3.5 * @version 0.1.0 */ protected static useSWR( diff --git a/src/ShopNet/Table.ts b/src/ShopNet/Table.ts index d53179d..f7e0fac 100644 --- a/src/ShopNet/Table.ts +++ b/src/ShopNet/Table.ts @@ -36,7 +36,7 @@ export class Table { if (find) { if ( - (expire && await Promise.resolve(expire(find))) || + (expire && await Dexie.waitFor(expire(find))) || (typeof find.Exp === "number" && find.Exp < dayjs().unix()) ) { await this.Sto.delete(key); @@ -63,7 +63,7 @@ export class Table { ): Promise { const res = await this.Get(key, expire); if (res) return res; - return this.Set(key, await Promise.resolve(fac()), exp); + return this.Set(key, await Dexie.waitFor(fac()), exp); } /** @@ -78,7 +78,8 @@ export class Table { if (exp === null) { await this.Sto.put({ - Id: id, Exp: exp, + Id: id, + Exp: exp, Val: val }); @@ -90,7 +91,8 @@ export class Table { throw RangeError(`Expire time [${time}] cannot be less than now [${dayjs().unix()}]`); await this.Sto.put({ - Id: id, Exp: time, + Id: id, + Exp: time, Val: val });