Skip to content

Commit

Permalink
refactor: removing immer to fix max call stack error with production … (
Browse files Browse the repository at this point in the history
#20)(by @egadstar)

* refactor: removing immer to fix max call stack error with production builds

* refactor: drying the reducer logic
  • Loading branch information
egadstar authored and gorhom committed Sep 21, 2021
1 parent bc55f56 commit 809c3bc
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 64 deletions.
6 changes: 4 additions & 2 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"@react-navigation/stack": "^5.14.5",
"expo": "^41.0.1",
"expo-splash-screen": "~0.10.2",
"immer": "^9.0.3",
"react": "16.13.1",
"react-dom": "16.13.1",
"react-native": "0.63.4",
Expand All @@ -34,7 +33,10 @@
"@babel/core": "~7.9.0",
"@babel/runtime": "^7.12.18",
"@expo/webpack-config": "^0.12.76",
"@types/react": "~16.9.35",
"@types/react-native": "~0.63.2",
"babel-plugin-module-resolver": "^4.1.0",
"babel-preset-expo": "8.3.0"
"babel-preset-expo": "8.3.0",
"typescript": "~4.0.0"
}
}
49 changes: 44 additions & 5 deletions example/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2760,11 +2760,45 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.11.tgz#fc25a4248a5e8d0837019b1d170146d07334abe0"
integrity sha512-BJ97wAUuU3NUiUCp44xzUFquQEvnk1wu7q4CMEUYKJWjdkr0YWYDsm4RFtAvxYsNjLsKcrFt6RvK8r+mnzMbEQ==

"@types/prop-types@*":
version "15.7.4"
resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.4.tgz#fcf7205c25dff795ee79af1e30da2c9790808f11"
integrity sha512-rZ5drC/jWjrArrS8BR6SIr4cWpW09RNTYt9AMZo3Jwwif+iacXAqgVjm0B0Bv/S1jhDXKHqRVNCbACkJ89RAnQ==

"@types/q@^1.5.1":
version "1.5.4"
resolved "https://registry.yarnpkg.com/@types/q/-/q-1.5.4.tgz#15925414e0ad2cd765bfef58842f7e26a7accb24"
integrity sha512-1HcDas8SEj4z1Wc696tH56G8OlRaH/sqZOynNNB+HF0WOeXPaxTtbYzJY2oEfiUxjSKjhCKr+MvR7dCHcEelug==

"@types/react-native@~0.63.2":
version "0.63.53"
resolved "https://registry.yarnpkg.com/@types/react-native/-/react-native-0.63.53.tgz#776dc1b05d649cb41b05fdff06e41ab9066538ca"
integrity sha512-hfUYHlfuy436viNct5uYNh2y40iWl1+iWhAQ18vzoKtyeW4WLNyccZf6OfkJdkqkHyH+bgvHLhbmnocpnQeKXg==
dependencies:
"@types/react" "*"

"@types/react@*":
version "17.0.22"
resolved "https://registry.yarnpkg.com/@types/react/-/react-17.0.22.tgz#c80d1d0e87fe953bae3ab273bef451dea1a6291b"
integrity sha512-kq/BMeaAVLJM6Pynh8C2rnr/drCK+/5ksH0ch9asz+8FW3DscYCIEFtCeYTFeIx/ubvOsMXmRfy7qEJ76gM96A==
dependencies:
"@types/prop-types" "*"
"@types/scheduler" "*"
csstype "^3.0.2"

"@types/react@~16.9.35":
version "16.9.56"
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.9.56.tgz#ea25847b53c5bec064933095fc366b1462e2adf0"
integrity sha512-gIkl4J44G/qxbuC6r2Xh+D3CGZpJ+NdWTItAPmZbR5mUS+JQ8Zvzpl0ea5qT/ZT3ZNTUcDKUVqV3xBE8wv/DyQ==
dependencies:
"@types/prop-types" "*"
csstype "^3.0.2"

"@types/scheduler@*":
version "0.16.2"
resolved "https://registry.yarnpkg.com/@types/scheduler/-/scheduler-0.16.2.tgz#1a62f89525723dde24ba1b01b092bf5df8ad4d39"
integrity sha512-hppQEBDmlwhFAXKJX2KnWLYu5yMfi91yazPb2l+lbJiwW+wdo1gNeRA+3RgNSO39WYX2euey41KEwnqesU2Jew==

"@types/source-list-map@*":
version "0.1.2"
resolved "https://registry.yarnpkg.com/@types/source-list-map/-/source-list-map-0.1.2.tgz#0078836063ffaf17412349bba364087e0ac02ec9"
Expand Down Expand Up @@ -4611,6 +4645,11 @@ csso@^4.0.2:
dependencies:
css-tree "^1.1.2"

csstype@^3.0.2:
version "3.0.9"
resolved "https://registry.yarnpkg.com/csstype/-/csstype-3.0.9.tgz#6410af31b26bd0520933d02cbc64fce9ce3fbf0b"
integrity sha512-rpw6JPxK6Rfg1zLOYCSwle2GFOOsnjmDYDaBwEcwoOg4qlsIVCN789VkBZDJAGi4T07gI4YSutR43t9Zz4Lzuw==

cyclist@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/cyclist/-/cyclist-1.0.1.tgz#596e9698fd0c80e12038c2b82d6eb1b35b6224d9"
Expand Down Expand Up @@ -6184,11 +6223,6 @@ immer@8.0.1:
resolved "https://registry.yarnpkg.com/immer/-/immer-8.0.1.tgz#9c73db683e2b3975c424fb0572af5889877ae656"
integrity sha512-aqXhGP7//Gui2+UrEtvxZxSquQVXTpZ7KDxfCcKAF3Vysvw0CViVaW9RZ1j1xlIYqaaaipBoqdqeibkc18PNvA==

immer@^9.0.3:
version "9.0.3"
resolved "https://registry.yarnpkg.com/immer/-/immer-9.0.3.tgz#146e2ba8b84d4b1b15378143c2345559915097f4"
integrity sha512-mONgeNSMuyjIe0lkQPa9YhdmTv8P19IeHV0biYhcXhbd5dhdB9HSK93zBpyKjp6wersSUgT5QyU0skmejUVP2A==

import-fresh@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/import-fresh/-/import-fresh-2.0.0.tgz#d81355c15612d386c61f9ddd3922d4304822a546"
Expand Down Expand Up @@ -10514,6 +10548,11 @@ typedarray@^0.0.6:
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=

typescript@~4.0.0:
version "4.0.8"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.0.8.tgz#5739105541db80a971fdbd0d56511d1a6f17d37f"
integrity sha512-oz1765PN+imfz1MlZzSZPtC/tqcwsCyIYA8L47EkRnRW97ztRk83SzMiWLrnChC0vqoYxSU1fcFUDA5gV/ZiPg==

ua-parser-js@^0.7.18:
version "0.7.22"
resolved "https://registry.yarnpkg.com/ua-parser-js/-/ua-parser-js-0.7.22.tgz#960df60a5f911ea8f1c818f3747b99c6e177eae3"
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"bootstrap": "yarn example && yarn && pod install"
},
"dependencies": {
"immer": "^9.0.3",
"nanoid": "^3.1.23"
},
"devDependencies": {
Expand Down
99 changes: 48 additions & 51 deletions src/state/reducer.ts
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;
}
);
};
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5197,11 +5197,6 @@ image-size@^0.6.0:
resolved "https://registry.yarnpkg.com/image-size/-/image-size-0.6.3.tgz#e7e5c65bb534bd7cdcedd6cb5166272a85f75fb2"
integrity sha512-47xSUiQioGaB96nqtp5/q55m0aBQSQdyIloMOc/x+QVTDZLNmXE892IIDrJ0hM1A5vcNUDD5tDffkSP5lCaIIA==

immer@^9.0.3:
version "9.0.3"
resolved "https://registry.yarnpkg.com/immer/-/immer-9.0.3.tgz#146e2ba8b84d4b1b15378143c2345559915097f4"
integrity sha512-mONgeNSMuyjIe0lkQPa9YhdmTv8P19IeHV0biYhcXhbd5dhdB9HSK93zBpyKjp6wersSUgT5QyU0skmejUVP2A==

import-cwd@3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/import-cwd/-/import-cwd-3.0.0.tgz#20845547718015126ea9b3676b7592fb8bd4cf92"
Expand Down

0 comments on commit 809c3bc

Please sign in to comment.