use-reducer-with-devtools.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.useReducerWithReduxDevtools = useReducerWithReduxDevtools;
  6. var _react = require("react");
  7. function normalizeRouterState(val) {
  8. if (val instanceof Map) {
  9. const obj = {};
  10. for (const [key, value] of val.entries()){
  11. if (typeof value === 'function') {
  12. obj[key] = 'fn()';
  13. continue;
  14. }
  15. if (typeof value === 'object' && value !== null) {
  16. if (value.$$typeof) {
  17. obj[key] = value.$$typeof.toString();
  18. continue;
  19. }
  20. if (value._bundlerConfig) {
  21. obj[key] = 'FlightData';
  22. continue;
  23. }
  24. }
  25. obj[key] = normalizeRouterState(value);
  26. }
  27. return obj;
  28. }
  29. if (typeof val === 'object' && val !== null) {
  30. const obj = {};
  31. for(const key in val){
  32. const value = val[key];
  33. if (typeof value === 'function') {
  34. obj[key] = 'fn()';
  35. continue;
  36. }
  37. if (typeof value === 'object' && value !== null) {
  38. if (value.$$typeof) {
  39. obj[key] = value.$$typeof.toString();
  40. continue;
  41. }
  42. if (value.hasOwnProperty('_bundlerConfig')) {
  43. obj[key] = 'FlightData';
  44. continue;
  45. }
  46. }
  47. obj[key] = normalizeRouterState(value);
  48. }
  49. return obj;
  50. }
  51. if (Array.isArray(val)) {
  52. return val.map(normalizeRouterState);
  53. }
  54. return val;
  55. }
  56. function devToolReducer(fn, ref) {
  57. return (state, action)=>{
  58. const res = fn(state, action);
  59. if (ref.current) {
  60. ref.current.send(action, normalizeRouterState(res));
  61. }
  62. return res;
  63. };
  64. }
  65. function useReducerWithReduxDevtools(fn, initialState) {
  66. const devtoolsConnectionRef = (0, _react).useRef();
  67. const enabledRef = (0, _react).useRef();
  68. (0, _react).useEffect(()=>{
  69. if (devtoolsConnectionRef.current || enabledRef.current === false) {
  70. return;
  71. }
  72. if (enabledRef.current === undefined && typeof window.__REDUX_DEVTOOLS_EXTENSION__ === 'undefined') {
  73. enabledRef.current = false;
  74. return;
  75. }
  76. devtoolsConnectionRef.current = window.__REDUX_DEVTOOLS_EXTENSION__.connect({
  77. instanceId: 1,
  78. name: 'next-router'
  79. });
  80. if (devtoolsConnectionRef.current) {
  81. devtoolsConnectionRef.current.init(normalizeRouterState(initialState));
  82. }
  83. return ()=>{
  84. devtoolsConnectionRef.current = undefined;
  85. };
  86. }, [
  87. initialState
  88. ]);
  89. const [state, dispatch] = (0, _react).useReducer(devToolReducer(/* logReducer( */ fn /*)*/ , devtoolsConnectionRef), initialState);
  90. const sync = (0, _react).useCallback(()=>{
  91. if (devtoolsConnectionRef.current) {
  92. devtoolsConnectionRef.current.send({
  93. type: 'RENDER_SYNC'
  94. }, normalizeRouterState(state));
  95. }
  96. }, [
  97. state
  98. ]);
  99. return [
  100. state,
  101. dispatch,
  102. sync
  103. ];
  104. }
  105. if ((typeof exports.default === 'function' || (typeof exports.default === 'object' && exports.default !== null)) && typeof exports.default.__esModule === 'undefined') {
  106. Object.defineProperty(exports.default, '__esModule', { value: true });
  107. Object.assign(exports.default, exports);
  108. module.exports = exports.default;
  109. }
  110. //# sourceMappingURL=use-reducer-with-devtools.js.map