Skip to content

Commit

Permalink
fix: do not crash when any server is offline (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
PhotonQuantum authored Jul 3, 2024
1 parent f518fe4 commit a5361db
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
25 changes: 24 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,34 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Use Node.js 20.x

- uses: pnpm/action-setup@v4
name: Install pnpm
with:
version: 9
run_install: false

- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: 'pnpm'

- name: Get pnpm store directory
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
- uses: actions/cache@v4
name: Setup pnpm cache
with:
path: ${{ env.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Install dependencies
run: pnpm install

- name: Build
run: pnpm build
12 changes: 9 additions & 3 deletions src/rpcs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,14 @@ const castFetcher = <T, >(fetcher: Fetcher<T, string>) => fetcher;
type FetcherArray<T extends any[], U extends Key> = { [K in keyof T]: Fetcher<T[K], U> };
type ResultArray<T extends any[]> = { [K in keyof T]: T[K] };

const liftFetchers = <T extends any[], U extends Key>(fetchers: FetcherArray<T, U>): Fetcher<ResultArray<T>, U[]> => {
const liftFetchers = <T extends any[], U extends Key>(fetchers: FetcherArray<T, U>, ignoreError: boolean): Fetcher<ResultArray<T>, U[]> => {
return async (inputs: U[]) => {
if (ignoreError) {
// @ts-ignore
const tryResults = await Promise.all(fetchers.map((fetcher, index) => fetcher(inputs[index]).catch(e => e)));
const results = tryResults.filter((x) => !(x instanceof Error));
return results as ResultArray<T>;
}
// @ts-ignore
const results = await Promise.all(fetchers.map((fetcher, index) => fetcher(inputs[index])));
return results as ResultArray<T>;
Expand All @@ -57,7 +63,7 @@ export const useLugReport = () => {
error,
isLoading,
isValidating
} = useSWR(LUG_SERVERS, liftFetchers(LUG_SERVERS.map(() => lugFetcher)))
} = useSWR(LUG_SERVERS, liftFetchers(LUG_SERVERS.map(() => lugFetcher), true))
const mergedData = useMemo(() => {
const merged = data?.map((data) => data.WorkerStatus).reduce((prev, x) => ({...prev, ...x}))
if (merged !== undefined) {
Expand All @@ -78,7 +84,7 @@ export const useError = (error: any) => {
color: "red",
title: "出错了",
icon: <IconX/>,
message: error,
message: error.toString(),
})
}
}, [error]);
Expand Down

0 comments on commit a5361db

Please sign in to comment.