-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: removing immer to fix max call stack error with production … (
#20)(by @egadstar) * refactor: removing immer to fix max call stack error with production builds * refactor: drying the reducer logic
- Loading branch information
Showing
5 changed files
with
96 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,96 @@ | ||
import produce, { enableES5, setAutoFreeze } from 'immer'; | ||
import { ACTIONS } from './constants'; | ||
import { print } from '../utilities/logger'; | ||
import type { PortalType } from '../types'; | ||
import type { ActionTypes, AddPortalAction } from './types'; | ||
|
||
enableES5(); | ||
setAutoFreeze(false); | ||
|
||
const registerHost = ( | ||
draft: Record<string, Array<PortalType>>, | ||
state: Record<string, Array<PortalType>>, | ||
hostName: string | ||
) => { | ||
if (!(hostName in draft)) { | ||
draft[hostName] = []; | ||
if (!(hostName in state)) { | ||
state[hostName] = []; | ||
} | ||
return state; | ||
}; | ||
|
||
const deregisterHost = ( | ||
draft: Record<string, Array<PortalType>>, | ||
state: Record<string, Array<PortalType>>, | ||
hostName: string | ||
) => { | ||
delete draft[hostName]; | ||
delete state[hostName]; | ||
return state; | ||
}; | ||
|
||
const addOrUpdatePortal = ( | ||
draft: Record<string, Array<PortalType>>, | ||
state: Record<string, Array<PortalType>>, | ||
hostName: string, | ||
portalName: string, | ||
node: any | ||
) => { | ||
if (!(hostName in draft)) { | ||
registerHost(draft, hostName); | ||
if (!(hostName in state)) { | ||
state = registerHost(state, hostName); | ||
} | ||
|
||
/** | ||
* updated portal, if it was already added. | ||
*/ | ||
const index = draft[hostName].findIndex(item => item.name === portalName); | ||
const index = state[hostName].findIndex(item => item.name === portalName); | ||
if (index !== -1) { | ||
draft[hostName][index].node = node; | ||
state[hostName][index].node = node; | ||
} else { | ||
draft[hostName].push({ | ||
state[hostName].push({ | ||
name: portalName, | ||
node, | ||
}); | ||
} | ||
return state; | ||
}; | ||
|
||
const removePortal = ( | ||
draft: Record<string, Array<PortalType>>, | ||
state: Record<string, Array<PortalType>>, | ||
hostName: string, | ||
portalName: string | ||
) => { | ||
if (!(hostName in draft)) { | ||
if (!(hostName in state)) { | ||
print({ | ||
component: reducer.name, | ||
method: removePortal.name, | ||
params: `Failed to remove portal '${portalName}', '${hostName}' was not registered!`, | ||
}); | ||
return; | ||
return state; | ||
} | ||
|
||
const index = draft[hostName].findIndex(item => item.name === portalName); | ||
if (index !== -1) draft[hostName].splice(index, 1); | ||
const index = state[hostName].findIndex(item => item.name === portalName); | ||
if (index !== -1) state[hostName].splice(index, 1); | ||
return state; | ||
}; | ||
|
||
export const reducer = produce( | ||
(draft: Record<string, Array<PortalType>>, action: ActionTypes) => { | ||
const { type } = action; | ||
switch (type) { | ||
case ACTIONS.REGISTER_HOST: | ||
registerHost(draft, action.hostName); | ||
break; | ||
|
||
case ACTIONS.DEREGISTER_HOST: | ||
deregisterHost(draft, action.hostName); | ||
break; | ||
|
||
case ACTIONS.ADD_PORTAL: | ||
case ACTIONS.UPDATE_PORTAL: | ||
addOrUpdatePortal( | ||
draft, | ||
action.hostName, | ||
(action as AddPortalAction).portalName, | ||
(action as AddPortalAction).node | ||
); | ||
break; | ||
|
||
case ACTIONS.REMOVE_PORTAL: | ||
removePortal( | ||
draft, | ||
action.hostName, | ||
(action as AddPortalAction).portalName | ||
); | ||
break; | ||
} | ||
export const reducer = ( | ||
state: Record<string, Array<PortalType>>, | ||
action: ActionTypes | ||
) => { | ||
const { type } = action; | ||
let clonedState = { ...state }; | ||
switch (type) { | ||
case ACTIONS.REGISTER_HOST: | ||
return registerHost(clonedState, action.hostName); | ||
case ACTIONS.DEREGISTER_HOST: | ||
return deregisterHost(clonedState, action.hostName); | ||
case ACTIONS.ADD_PORTAL: | ||
case ACTIONS.UPDATE_PORTAL: | ||
return addOrUpdatePortal( | ||
clonedState, | ||
action.hostName, | ||
(action as AddPortalAction).portalName, | ||
(action as AddPortalAction).node | ||
); | ||
case ACTIONS.REMOVE_PORTAL: | ||
return removePortal( | ||
clonedState, | ||
action.hostName, | ||
(action as AddPortalAction).portalName | ||
); | ||
default: | ||
return state; | ||
} | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters