123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.useReducerWithReduxDevtools = useReducerWithReduxDevtools;
- var _react = require("react");
- function normalizeRouterState(val) {
- if (val instanceof Map) {
- const obj = {};
- for (const [key, value] of val.entries()){
- if (typeof value === 'function') {
- obj[key] = 'fn()';
- continue;
- }
- if (typeof value === 'object' && value !== null) {
- if (value.$$typeof) {
- obj[key] = value.$$typeof.toString();
- continue;
- }
- if (value._bundlerConfig) {
- obj[key] = 'FlightData';
- continue;
- }
- }
- obj[key] = normalizeRouterState(value);
- }
- return obj;
- }
- if (typeof val === 'object' && val !== null) {
- const obj = {};
- for(const key in val){
- const value = val[key];
- if (typeof value === 'function') {
- obj[key] = 'fn()';
- continue;
- }
- if (typeof value === 'object' && value !== null) {
- if (value.$$typeof) {
- obj[key] = value.$$typeof.toString();
- continue;
- }
- if (value.hasOwnProperty('_bundlerConfig')) {
- obj[key] = 'FlightData';
- continue;
- }
- }
- obj[key] = normalizeRouterState(value);
- }
- return obj;
- }
- if (Array.isArray(val)) {
- return val.map(normalizeRouterState);
- }
- return val;
- }
- function devToolReducer(fn, ref) {
- return (state, action)=>{
- const res = fn(state, action);
- if (ref.current) {
- ref.current.send(action, normalizeRouterState(res));
- }
- return res;
- };
- }
- function useReducerWithReduxDevtools(fn, initialState) {
- const devtoolsConnectionRef = (0, _react).useRef();
- const enabledRef = (0, _react).useRef();
- (0, _react).useEffect(()=>{
- if (devtoolsConnectionRef.current || enabledRef.current === false) {
- return;
- }
- if (enabledRef.current === undefined && typeof window.__REDUX_DEVTOOLS_EXTENSION__ === 'undefined') {
- enabledRef.current = false;
- return;
- }
- devtoolsConnectionRef.current = window.__REDUX_DEVTOOLS_EXTENSION__.connect({
- instanceId: 1,
- name: 'next-router'
- });
- if (devtoolsConnectionRef.current) {
- devtoolsConnectionRef.current.init(normalizeRouterState(initialState));
- }
- return ()=>{
- devtoolsConnectionRef.current = undefined;
- };
- }, [
- initialState
- ]);
- const [state, dispatch] = (0, _react).useReducer(devToolReducer( fn , devtoolsConnectionRef), initialState);
- const sync = (0, _react).useCallback(()=>{
- if (devtoolsConnectionRef.current) {
- devtoolsConnectionRef.current.send({
- type: 'RENDER_SYNC'
- }, normalizeRouterState(state));
- }
- }, [
- state
- ]);
- return [
- state,
- dispatch,
- sync
- ];
- }
- if ((typeof exports.default === 'function' || (typeof exports.default === 'object' && exports.default !== null)) && typeof exports.default.__esModule === 'undefined') {
- Object.defineProperty(exports.default, '__esModule', { value: true });
- Object.assign(exports.default, exports);
- module.exports = exports.default;
- }
|