(
GateComponent: Gate<Props>,
props: Props = {} as any,
scope?: Scope,
)
| 368 | } |
| 369 | |
| 370 | export function useGateBase<Props>( |
| 371 | GateComponent: Gate<Props>, |
| 372 | props: Props = {} as any, |
| 373 | scope?: Scope, |
| 374 | ) { |
| 375 | const {open, close, set} = useUnitBase( |
| 376 | { |
| 377 | open: GateComponent.open, |
| 378 | close: GateComponent.close, |
| 379 | set: GateComponent.set, |
| 380 | }, |
| 381 | scope, |
| 382 | ) |
| 383 | const ForkedGate = React.useMemo( |
| 384 | () => |
| 385 | ({ |
| 386 | open, |
| 387 | close, |
| 388 | set, |
| 389 | } as Gate<Props>), |
| 390 | [GateComponent, open], |
| 391 | ) |
| 392 | |
| 393 | const propsRef = React.useRef<{value: any; count: number}>({ |
| 394 | value: null, |
| 395 | count: 0, |
| 396 | }) |
| 397 | useIsomorphicLayoutEffect(() => { |
| 398 | ForkedGate.open(propsRef.current.value) |
| 399 | return () => ForkedGate.close(propsRef.current.value) as any |
| 400 | }, [ForkedGate]) |
| 401 | if (!shallowCompare(propsRef.current.value, props)) { |
| 402 | propsRef.current.value = props |
| 403 | propsRef.current.count += 1 |
| 404 | } |
| 405 | useIsomorphicLayoutEffect(() => { |
| 406 | ForkedGate.set(propsRef.current.value) |
| 407 | }, [propsRef.current.count]) |
| 408 | } |
| 409 | |
| 410 | function shallowCompare(a: any, b: any) { |
| 411 | if (a === b) return true |
no test coverage detected
searching dependent graphs…