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

Commit

Permalink
The most significant changes involve the replacement of the `dexie-re…
Browse files Browse the repository at this point in the history
…act-hooks` library with the `useState` hook from React in the `Get.ts` file, and the replacement of the `useLiveQuery` hook with a manual implementation of state management and data fetching. These changes were made to have more control over state management and data fetching. Additionally, the `Promise.resolve` method was replaced with `Dexie.waitFor` in several files to ensure promises are resolved before proceeding with the next operation.

Here are the changes in detail:

1. The `dexie-react-hooks` library was replaced with the `useState` hook from React in the `Get.ts` file for better state management within the component.
2. The `useLiveQuery` hook was removed and replaced with a manual implementation of state management and data fetching for more control over the data fetching process.
3. The `Promise.resolve` method was replaced with `Dexie.waitFor` in the `SignalR.ts` and `Table.ts` files to ensure promises are resolved before proceeding with the next operation.
4. The `Shared.Sto.delete` method was updated to be awaited in the `SignalR.ts` file to ensure the delete operation completes before proceeding.
5. The version annotation for the `useSWR` method in the `SignalR.ts` file was updated from `1.3.0` to `1.3.5`.
6. The `Table.ts` file was updated to use `Dexie.waitFor` instead of `Promise.resolve` when setting the value in the table to ensure the value is set before proceeding.
7. The `Table.ts` file was updated to format the object being put into the store in a more readable way to improve code readability.
8. The `Table.ts` file was updated to throw a `RangeError` if the expire time is less than the current time to prevent setting an expire time in the past.
  • Loading branch information
Aloento committed Feb 13, 2024
1 parent 2ba425e commit 14609af
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 54 deletions.
89 changes: 43 additions & 46 deletions src/ShopNet/Order/Get.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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<ICartItem[]>([]);

const { data: meta } = this.useSWR<
this.useSWR<
{
Types: number[];
Quantity: number;
Expand All @@ -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<string, string> = {};
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<string, string> = {};
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;
}
Expand Down
9 changes: 5 additions & 4 deletions src/ShopNet/SignalR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -124,15 +125,15 @@ export abstract class SignalR {

// TODO:导致 LiveQuery 无限刷新
const update = async () => {
const res = await Promise.resolve(this.Invoke<T | true | null>(methodName, key, find?.Version));
const res = await Dexie.waitFor(this.Invoke<T | true | null>(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();
}

Expand All @@ -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<T>(
Expand Down
10 changes: 6 additions & 4 deletions src/ShopNet/Table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -63,7 +63,7 @@ export class Table {
): Promise<T> {
const res = await this.Get<T>(key, expire);
if (res) return res;
return this.Set<T>(key, await Promise.resolve(fac()), exp);
return this.Set<T>(key, await Dexie.waitFor(fac()), exp);
}

/**
Expand All @@ -78,7 +78,8 @@ export class Table {

if (exp === null) {
await this.Sto.put({
Id: id, Exp: exp,
Id: id,
Exp: exp,
Val: val
});

Expand All @@ -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
});

Expand Down

0 comments on commit 14609af

Please sign in to comment.