From 809c3bc0e5d9e8a18a7ef15874e68b7c4c68b09a Mon Sep 17 00:00:00 2001 From: Peter Washburn Date: Tue, 21 Sep 2021 03:31:52 -0400 Subject: [PATCH] =?UTF-8?q?refactor:=20removing=20immer=20to=20fix=20max?= =?UTF-8?q?=20call=20stack=20error=20with=20production=20=E2=80=A6=20(#20)?= =?UTF-8?q?(by=20@egadstar)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: removing immer to fix max call stack error with production builds * refactor: drying the reducer logic --- example/package.json | 6 ++- example/yarn.lock | 49 +++++++++++++++++++--- package.json | 1 - src/state/reducer.ts | 99 +++++++++++++++++++++----------------------- yarn.lock | 5 --- 5 files changed, 96 insertions(+), 64 deletions(-) diff --git a/example/package.json b/example/package.json index 0112baf..5a6a2c0 100644 --- a/example/package.json +++ b/example/package.json @@ -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", @@ -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" } } diff --git a/example/yarn.lock b/example/yarn.lock index e3ed422..cb11706 100644 --- a/example/yarn.lock +++ b/example/yarn.lock @@ -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" @@ -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" @@ -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" @@ -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" diff --git a/package.json b/package.json index c76d9f5..db53f4a 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,6 @@ "bootstrap": "yarn example && yarn && pod install" }, "dependencies": { - "immer": "^9.0.3", "nanoid": "^3.1.23" }, "devDependencies": { diff --git a/src/state/reducer.ts b/src/state/reducer.ts index c827500..a4c6f90 100644 --- a/src/state/reducer.ts +++ b/src/state/reducer.ts @@ -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>, + state: Record>, hostName: string ) => { - if (!(hostName in draft)) { - draft[hostName] = []; + if (!(hostName in state)) { + state[hostName] = []; } + return state; }; const deregisterHost = ( - draft: Record>, + state: Record>, hostName: string ) => { - delete draft[hostName]; + delete state[hostName]; + return state; }; const addOrUpdatePortal = ( - draft: Record>, + state: Record>, 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>, + state: Record>, 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>, 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>, + 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; } -); +}; diff --git a/yarn.lock b/yarn.lock index 3fa9c3b..b7a128a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"