110 lines
3.5 KiB
JavaScript
110 lines
3.5 KiB
JavaScript
import { useRef, useReducer, useEffect, useCallback } from "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 useReducerWithReduxDevtoolsNoop(fn, initialState) {
|
|
const [state, dispatch] = useReducer(fn, initialState);
|
|
return [
|
|
state,
|
|
dispatch,
|
|
()=>{}
|
|
];
|
|
}
|
|
function useReducerWithReduxDevtoolsImpl(fn, initialState) {
|
|
const devtoolsConnectionRef = useRef();
|
|
const enabledRef = useRef();
|
|
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: 8000,
|
|
name: "next-router"
|
|
});
|
|
if (devtoolsConnectionRef.current) {
|
|
devtoolsConnectionRef.current.init(normalizeRouterState(initialState));
|
|
}
|
|
return ()=>{
|
|
devtoolsConnectionRef.current = undefined;
|
|
};
|
|
}, [
|
|
initialState
|
|
]);
|
|
const [state, dispatch] = useReducer(devToolReducer(/* logReducer( */ fn /*)*/ , devtoolsConnectionRef), initialState);
|
|
const sync = useCallback(()=>{
|
|
if (devtoolsConnectionRef.current) {
|
|
devtoolsConnectionRef.current.send({
|
|
type: "RENDER_SYNC"
|
|
}, normalizeRouterState(state));
|
|
}
|
|
}, [
|
|
state
|
|
]);
|
|
return [
|
|
state,
|
|
dispatch,
|
|
sync
|
|
];
|
|
}
|
|
export const useReducerWithReduxDevtools = typeof window !== "undefined" ? useReducerWithReduxDevtoolsImpl : useReducerWithReduxDevtoolsNoop;
|
|
|
|
//# sourceMappingURL=use-reducer-with-devtools.js.map
|