mirror of
https://github.com/zoriya/v10.git
synced 2026-08-16 02:45:09 +00:00
45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
import type { AnyStore, InferStoreRequests } from '../../core/store';
|
|
|
|
import { isUndefined } from '@videojs/utils/predicate';
|
|
|
|
/**
|
|
* Access the store's request methods.
|
|
*
|
|
* Returns either the full request map or a specific request function by name.
|
|
*
|
|
* The request map is stable across renders (same reference).
|
|
*
|
|
* @example
|
|
* ```tsx
|
|
* // Get all requests
|
|
* function Controls() {
|
|
* const request = useRequest(store);
|
|
* return <button onClick={() => request.play()}>Play</button>;
|
|
* }
|
|
*
|
|
* // Get a specific request by name
|
|
* function PlayButton() {
|
|
* const play = useRequest(store, 'play');
|
|
* return <button onClick={() => play()}>Play</button>;
|
|
* }
|
|
* ```
|
|
*/
|
|
export function useRequest<S extends AnyStore>(store: S): InferStoreRequests<S>;
|
|
export function useRequest<S extends AnyStore, Name extends keyof InferStoreRequests<S>>(
|
|
store: S,
|
|
name: Name,
|
|
): InferStoreRequests<S>[Name];
|
|
// eslint-disable-next-line react/no-unnecessary-use-prefix
|
|
export function useRequest<S extends AnyStore, Name extends keyof InferStoreRequests<S>>(
|
|
store: S,
|
|
name?: Name,
|
|
): InferStoreRequests<S> | InferStoreRequests<S>[Name] {
|
|
const request = store.request as InferStoreRequests<S>;
|
|
|
|
if (isUndefined(name)) {
|
|
return request;
|
|
}
|
|
|
|
return request[name];
|
|
}
|