mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
9cdf8a99ed
* Facebook -> Meta in copyright rg --files | xargs sed -i 's#Copyright (c) Facebook, Inc. and its affiliates.#Copyright (c) Meta Platforms, Inc. and affiliates.#g' * Manual tweaks
31 lines
1.1 KiB
JavaScript
31 lines
1.1 KiB
JavaScript
/**
|
||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||
*
|
||
* This source code is licensed under the MIT license found in the
|
||
* LICENSE file in the root directory of this source tree.
|
||
*
|
||
* @flow
|
||
*/
|
||
|
||
import {useSyncExternalStore} from 'use-sync-external-store/shim';
|
||
|
||
// Hook used for safely managing subscriptions in concurrent mode.
|
||
//
|
||
// In order to avoid removing and re-adding subscriptions each time this hook is called,
|
||
// the parameters passed to this hook should be memoized in some way–
|
||
// either by wrapping the entire params object with useMemo()
|
||
// or by wrapping the individual callbacks with useCallback().
|
||
export function useSubscription<Value>({
|
||
// (Synchronously) returns the current value of our subscription.
|
||
getCurrentValue,
|
||
|
||
// This function is passed an event handler to attach to the subscription.
|
||
// It should return an unsubscribe function that removes the handler.
|
||
subscribe,
|
||
}: {
|
||
getCurrentValue: () => Value,
|
||
subscribe: (callback: Function) => () => void,
|
||
}): Value {
|
||
return useSyncExternalStore(subscribe, getCurrentValue);
|
||
}
|