|
1 | | -// useSelector.ts |
2 | 1 | 'use client'; |
3 | | -import * as React from 'react'; |
4 | | -import { useSyncExternalStore } from 'react'; |
| 2 | +/* eslint-disable no-bitwise */ |
| 3 | +import { useEffect, useRef, useState } from 'react'; |
| 4 | +import { flushSync } from 'react-dom'; |
| 5 | +import type { AllPathsKeys } from 'skyroc-type-utils'; |
| 6 | + |
| 7 | +import type { ChangeMask } from '../../form-core/event'; |
| 8 | +import { ChangeTag } from '../../form-core/event'; |
5 | 9 |
|
6 | 10 | import { useFieldContext } from './FieldContext'; |
7 | | -import type { ChangeMask } from './events'; |
8 | | -import { ChangeTag } from './events'; |
9 | | -import type { NamePath } from './types'; |
| 11 | +import type { FormInstance, InternalFormInstance } from './FieldContext'; |
10 | 12 |
|
11 | 13 | type Eq<T> = (a: T, b: T) => boolean; |
12 | 14 |
|
13 | | -export function useSelector<T>( |
14 | | - selector: (get: (n: NamePath) => any, all: any) => T, |
15 | | - eq: Eq<T> = Object.is, |
16 | | - opt?: { mask?: ChangeMask; names?: NamePath[] } |
17 | | -): T { |
18 | | - const form = useFieldContext(); |
19 | | - const mask = opt?.mask ?? ChangeTag.Value | ChangeTag.Errors | ChangeTag.Validating; |
20 | | - const names = opt?.names || []; |
21 | | - |
22 | | - const getSel = React.useCallback( |
23 | | - () => selector(form.getFieldValue.bind(form), form.getFieldsValue()), |
24 | | - [form, selector] |
25 | | - ); |
26 | | - |
27 | | - const subscribe = React.useCallback( |
28 | | - (on: () => void) => { |
29 | | - if (!names.length) { |
30 | | - // 全局订阅:任意字段改变都试着比较 |
31 | | - return form.__store.subscribeField([], () => on(), { includeChildren: true, mask }); |
| 15 | +type UseSelectorOpts<Values, R> = { |
| 16 | + /** 订阅字段,空则订阅全部 */ |
| 17 | + deps?: AllPathsKeys<Values>[]; |
| 18 | + /** 是否相等 */ |
| 19 | + eq?: Eq<R>; |
| 20 | + /** 表单实例 */ |
| 21 | + form?: FormInstance<Values>; |
| 22 | + /** 是否订阅子路径 */ |
| 23 | + includeChildren?: boolean; |
| 24 | + /** 变更掩码 */ |
| 25 | + mask?: ChangeMask; |
| 26 | +}; |
| 27 | + |
| 28 | +/** |
| 29 | + * 从表单中“选择”任意聚合值,只有依赖变化才刷新。 |
| 30 | + */ |
| 31 | +export function useSelector<Values = any, R = unknown>( |
| 32 | + selector: (get: (n: AllPathsKeys<Values>) => any, all: Values) => R, |
| 33 | + opts?: UseSelectorOpts<Values, R> |
| 34 | +): R { |
| 35 | + const ctxForm = useFieldContext<Values>(); |
| 36 | + const form = opts?.form ?? ctxForm; |
| 37 | + |
| 38 | + const eq = opts?.eq ?? Object.is; |
| 39 | + |
| 40 | + if (!form) { |
| 41 | + throw new Error('Can not find FormContext. Please make sure you wrap Field under Form or provide a form instance.'); |
| 42 | + } |
| 43 | + |
| 44 | + const { getInternalHooks } = form as unknown as InternalFormInstance<Values>; |
| 45 | + const { subscribeField } = getInternalHooks(); |
| 46 | + |
| 47 | + const deps = opts?.deps; |
| 48 | + |
| 49 | + const mask = opts?.mask ?? ChangeTag.Value; |
| 50 | + const includeChildren = opts?.includeChildren; |
| 51 | + |
| 52 | + // 计算当前选择值 |
| 53 | + const compute = () => { |
| 54 | + const getField = form.getFieldValue; |
| 55 | + const all = form.getFieldsValue() as Values; |
| 56 | + return selector(getField, all); |
| 57 | + }; |
| 58 | + |
| 59 | + // state + ref 用于去抖渲染 |
| 60 | + const [val, setVal] = useState<R>(compute); |
| 61 | + |
| 62 | + const prevRef = useRef<R>(val); |
| 63 | + |
| 64 | + useEffect(() => { |
| 65 | + // 订阅器 |
| 66 | + const onChange = () => { |
| 67 | + const next = compute(); |
| 68 | + if (!eq(prevRef.current, next)) { |
| 69 | + prevRef.current = next; |
| 70 | + // 与 useFieldState 一致:同步刷新,减少闪烁 |
| 71 | + flushSync(() => setVal(next)); |
32 | 72 | } |
33 | | - const offs = names.map(n => form.__store.subscribeField(n, () => on(), { includeChildren: true, mask })); |
34 | | - return () => offs.forEach(f => f()); |
35 | | - }, |
36 | | - [form, names.map(String).join('|'), mask] |
37 | | - ); |
38 | | - |
39 | | - const getSnap = React.useRef<T>(getSel()); |
40 | | - const getSnapshot = () => getSnap.current; |
41 | | - const serverSnapshot = getSnapshot; |
42 | | - |
43 | | - useSyncExternalStore( |
44 | | - subscribe, |
45 | | - () => { |
46 | | - const next = getSel(); |
47 | | - const prev = getSnap.current; |
48 | | - if (!eq(prev, next)) getSnap.current = next; |
49 | | - return getSnap.current; |
50 | | - }, |
51 | | - serverSnapshot |
52 | | - ); |
53 | | - |
54 | | - return getSnap.current; |
| 73 | + }; |
| 74 | + |
| 75 | + // 指定字段订阅 |
| 76 | + return subscribeField(deps, onChange, { |
| 77 | + includeChildren, |
| 78 | + mask |
| 79 | + }); |
| 80 | + }, []); |
| 81 | + |
| 82 | + return val; |
55 | 83 | } |
0 commit comments