/
githubmirror
/
client
Обзор
Документация
Войти
/
githubmirror
/
client
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
shared/stores/waiting.tsx
94 строки
3 KB
chrisnojima-zoom
Version 670 - clean2 (#29122)
08 июн 2026, 19:31
Не верифицирован
08 июн 2026, 19:31
1943197
Код
Авторство
О чём код?
import type {RPCError} from '@/util/errors' import type * as T from '@/constants/types' import * as Z from '@/util/zustand' // This store has no dependencies on other stores and is safe to import directly from other stores. const initialStore: T.Waiting.State = { counts: new Map(), errors: new Map(), } export type State = T.Waiting.State & { dispatch: { resetState: () => void clear: (keys: string | ReadonlyArray<string>) => void increment: (keys: string | ReadonlyArray<string>) => void decrement: (keys: string | ReadonlyArray<string>, error?: RPCError) => void batch: ( changes: ReadonlyArray<{key: string | ReadonlyArray<string>; increment: boolean; error?: RPCError}> ) => void } } const getKeys = (k?: string | ReadonlyArray<string>) => { if (k === undefined) return [] if (typeof k === 'string') return [k] return k } export const useWaitingState = Z.createZustand<State>('waiting', (set, get) => { const changeHelper = (keys: string | ReadonlyArray<string>, diff: 1 | -1, error?: RPCError) => { set(s => { getKeys(keys).forEach(k => { const oldCount = s.counts.get(k) || 0 // going from 0 => 1, clear errors if (oldCount === 0 && diff === 1) { s.errors.delete(k) } else { if (error) { s.errors.set(k, error) } } const newCount = oldCount + diff if (newCount === 0) { s.counts.delete(k) } else { s.counts.set(k, newCount) } }) }) } const dispatch: State['dispatch'] = { batch: changes => { changes.forEach(c => { if (c.increment) { get().dispatch.increment(c.key) } else { get().dispatch.decrement(c.key, c.error) } }) }, clear: keys => { set(s => { getKeys(keys).forEach(k => { s.counts.delete(k) s.errors.delete(k) }) }) }, decrement: (keys, error) => { changeHelper(keys, -1, error) }, increment: keys => { changeHelper(keys, 1) }, resetState: Z.defaultReset, } return { ...initialStore, dispatch, } }) export const useAnyWaiting = (k?: string | Array<string>) => useWaitingState(s => !!getKeys(k).some(k => (s.counts.get(k) ?? 0) > 0)) export const useAnyErrors = (k: string | Array<string>) => useWaitingState(s => { const errorKey = getKeys(k).find(k => s.errors.get(k)) return errorKey ? s.errors.get(errorKey) : undefined }) export const useDispatchClearWaiting = () => useWaitingState(s => s.dispatch.clear)