Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not crash when any server is offline #94

Merged
merged 2 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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