side-effect.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = SideEffect;
  6. var _interop_require_wildcard = require("@swc/helpers/lib/_interop_require_wildcard.js").default;
  7. var _react = _interop_require_wildcard(require("react"));
  8. function SideEffect(props) {
  9. const { headManager , reduceComponentsToState } = props;
  10. function emitChange() {
  11. if (headManager && headManager.mountedInstances) {
  12. const headElements = _react.Children.toArray(Array.from(headManager.mountedInstances).filter(Boolean));
  13. headManager.updateHead(reduceComponentsToState(headElements, props));
  14. }
  15. }
  16. if (isServer) {
  17. var ref;
  18. headManager == null ? void 0 : (ref = headManager.mountedInstances) == null ? void 0 : ref.add(props.children);
  19. emitChange();
  20. }
  21. useClientOnlyLayoutEffect(()=>{
  22. var ref1;
  23. headManager == null ? void 0 : (ref1 = headManager.mountedInstances) == null ? void 0 : ref1.add(props.children);
  24. return ()=>{
  25. var ref;
  26. headManager == null ? void 0 : (ref = headManager.mountedInstances) == null ? void 0 : ref.delete(props.children);
  27. };
  28. });
  29. // We need to call `updateHead` method whenever the `SideEffect` is trigger in all
  30. // life-cycles: mount, update, unmount. However, if there are multiple `SideEffect`s
  31. // being rendered, we only trigger the method from the last one.
  32. // This is ensured by keeping the last unflushed `updateHead` in the `_pendingUpdate`
  33. // singleton in the layout effect pass, and actually trigger it in the effect pass.
  34. useClientOnlyLayoutEffect(()=>{
  35. if (headManager) {
  36. headManager._pendingUpdate = emitChange;
  37. }
  38. return ()=>{
  39. if (headManager) {
  40. headManager._pendingUpdate = emitChange;
  41. }
  42. };
  43. });
  44. useClientOnlyEffect(()=>{
  45. if (headManager && headManager._pendingUpdate) {
  46. headManager._pendingUpdate();
  47. headManager._pendingUpdate = null;
  48. }
  49. return ()=>{
  50. if (headManager && headManager._pendingUpdate) {
  51. headManager._pendingUpdate();
  52. headManager._pendingUpdate = null;
  53. }
  54. };
  55. });
  56. return null;
  57. }
  58. const isServer = typeof window === 'undefined';
  59. const useClientOnlyLayoutEffect = isServer ? ()=>{} : _react.useLayoutEffect;
  60. const useClientOnlyEffect = isServer ? ()=>{} : _react.useEffect;
  61. //# sourceMappingURL=side-effect.js.map