Reduce state updates when selecting screens

This commit is contained in:
Andrew Kingston 2022-07-14 15:19:42 +01:00
parent e0496f604e
commit 75ed8feb92
3 changed files with 24 additions and 7 deletions

View File

@ -61,6 +61,10 @@ const INITIAL_FRONTEND_STATE = {
export const getFrontendStore = () => { export const getFrontendStore = () => {
const store = writable({ ...INITIAL_FRONTEND_STATE }) const store = writable({ ...INITIAL_FRONTEND_STATE })
store.subscribe(state => {
console.log("new state")
})
store.actions = { store.actions = {
reset: () => { reset: () => {
store.set({ ...INITIAL_FRONTEND_STATE }) store.set({ ...INITIAL_FRONTEND_STATE })
@ -184,12 +188,24 @@ export const getFrontendStore = () => {
}, },
screens: { screens: {
select: screenId => { select: screenId => {
store.update(state => { // Check this screen exists
let screens = state.screens const state = get(store)
let screen = const screen = state.screens.find(screen => screen._id === screenId)
screens.find(screen => screen._id === screenId) || screens[0] console.log(screen)
if (!screen) return state if (!screen) {
return
}
// Check screen isn't already selected
if (
state.selectedScreenId === screen._id &&
state.selectedComponentId === screen.props?._id
) {
return
}
// Select new screen
store.update(state => {
state.selectedScreenId = screen._id state.selectedScreenId = screen._id
state.selectedComponentId = screen.props?._id state.selectedComponentId = screen.props?._id
return state return state

View File

@ -17,7 +17,8 @@
getOptionValue={x => x._id} getOptionValue={x => x._id}
getOptionIcon={x => (x.routing.homeScreen ? "Home" : "WebPage")} getOptionIcon={x => (x.routing.homeScreen ? "Home" : "WebPage")}
getOptionColour={x => RoleUtils.getRoleColour(x.routing.roleId)} getOptionColour={x => RoleUtils.getRoleColour(x.routing.roleId)}
bind:value={$store.selectedScreenId} value={$store.selectedScreenId}
on:change={e => store.actions.screens.select(e.detail)}
/> />
</div> </div>
<div class="header-right"> <div class="header-right">

View File

@ -60,7 +60,7 @@
indentLevel={0} indentLevel={0}
selected={$store.selectedScreenId === screen._id} selected={$store.selectedScreenId === screen._id}
text={screen.routing.route} text={screen.routing.route}
on:click={() => ($store.selectedScreenId = screen._id)} on:click={() => store.actions.screens.select(screen._id)}
color={RoleUtils.getRoleColour(screen.routing.roleId)} color={RoleUtils.getRoleColour(screen.routing.roleId)}
> >
<ScreenDropdownMenu screenId={screen._id} /> <ScreenDropdownMenu screenId={screen._id} />