use-subscription.development.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /**
  2. * @license React
  3. * use-subscription.development.js
  4. *
  5. * Copyright (c) Facebook, Inc. and its affiliates.
  6. *
  7. * This source code is licensed under the MIT license found in the
  8. * LICENSE file in the root directory of this source tree.
  9. */
  10. 'use strict';
  11. if (process.env.NODE_ENV !== "production") {
  12. (function() {
  13. 'use strict';
  14. /* global __REACT_DEVTOOLS_GLOBAL_HOOK__ */
  15. if (
  16. typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== 'undefined' &&
  17. typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart ===
  18. 'function'
  19. ) {
  20. __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(new Error());
  21. }
  22. var shim = require('use-sync-external-store/shim');
  23. //
  24. // In order to avoid removing and re-adding subscriptions each time this hook is called,
  25. // the parameters passed to this hook should be memoized in some way–
  26. // either by wrapping the entire params object with useMemo()
  27. // or by wrapping the individual callbacks with useCallback().
  28. function useSubscription(_ref) {
  29. var getCurrentValue = _ref.getCurrentValue,
  30. subscribe = _ref.subscribe;
  31. return shim.useSyncExternalStore(subscribe, getCurrentValue);
  32. }
  33. exports.useSubscription = useSubscription;
  34. /* global __REACT_DEVTOOLS_GLOBAL_HOOK__ */
  35. if (
  36. typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== 'undefined' &&
  37. typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop ===
  38. 'function'
  39. ) {
  40. __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(new Error());
  41. }
  42. })();
  43. }