Skip to content

Commit

Permalink
selective store
Browse files Browse the repository at this point in the history
  • Loading branch information
liady committed Apr 30, 2024
1 parent 78b8566 commit b968b0e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ interface NewCanvasControlsProps {

export const NewCanvasControls = React.memo((props: NewCanvasControlsProps) => {
const canvasControlProps = useEditorState(
Substores.fullStore,
Substores.canvasControlsSubstate,
(store) => ({
keysPressed: store.editor.keysPressed,
editorMode: store.editor.mode,
Expand Down
20 changes: 20 additions & 0 deletions editor/src/components/editor/store/store-hook-substore-types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { HigherOrderControl } from '../../../components/canvas/canvas-types'
import { uniq } from '../../../core/shared/array-utils'
import { omit, pick } from '../../../core/shared/object-utils'
import type { EditorDispatch } from '../action-types'
Expand Down Expand Up @@ -30,6 +31,7 @@ export type Substates = {
projectServerState: ProjectServerStateSubstate
variablesInScope: VariablesInScopeSubstate
propertyControlsInfo: PropertyControlsInfoSubstate
canvasControlsSubstate: CanvasControlsSubstate
}

export type StoreKey = keyof Substates
Expand Down Expand Up @@ -125,6 +127,24 @@ const emptyVariablesInScopeSubstate = {
} as const
export type VariablesInScopeSubstate = typeof emptyVariablesInScopeSubstate

export const canvasControlsEditorSubstateKeys = [
'keysPressed',
'mode',
'focusedPanel',
'selectedViews',
'highlightedViews',
] as const
export const canvasControlsCanvasSubstateKeys = ['scale', 'scrollAnimation'] as const
export const canvasControlsDerivedSubstateKeys = ['controls'] as const
const emptyCanvasControlsSubstate = {
editor: {
...pick(canvasControlsEditorSubstateKeys, EmptyEditorStateForKeysOnly),
canvas: pick(canvasControlsCanvasSubstateKeys, EmptyEditorStateForKeysOnly.canvas),
},
derived: { controls: [] } as { controls: Array<HigherOrderControl> },
} as const
export type CanvasControlsSubstate = typeof emptyCanvasControlsSubstate

// MultiplayerSubstate
export const multiplayerSubstateKeys = ['collaborators'] as const
const emptyMultiplayerSubstate = {
Expand Down
11 changes: 11 additions & 0 deletions editor/src/components/editor/store/store-hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
import type {
BuiltInDependenciesSubstate,
CanvasAndMetadataSubstate,
CanvasControlsSubstate,
CanvasOffsetSubstate,
CanvasSubstate,
FocusedElementPathSubstate,
Expand All @@ -43,6 +44,9 @@ import type {
VariablesInScopeSubstate,
} from './store-hook-substore-types'
import {
canvasControlsCanvasSubstateKeys,
canvasControlsDerivedSubstateKeys,
canvasControlsEditorSubstateKeys,
canvasOffsetSubstateKeys,
canvasSubstateKeys,
focusedElementPathSubstateKeys,
Expand Down Expand Up @@ -316,6 +320,13 @@ export const Substores = {
variablesInScope: (a: VariablesInScopeSubstate, b: VariablesInScopeSubstate) => {
return keysEquality(variablesInScopeSubstateKeys, a.editor, b.editor)
},
canvasControlsSubstate: (a: CanvasControlsSubstate, b: CanvasControlsSubstate) => {
return (
keysEquality(canvasControlsEditorSubstateKeys, a.editor, b.editor) &&
keysEquality(canvasControlsCanvasSubstateKeys, a.editor.canvas, b.editor.canvas) &&
keysEquality(canvasControlsDerivedSubstateKeys, a.derived, b.derived)
)
},
multiplayer: (a: MultiplayerSubstate, b: MultiplayerSubstate) => {
return MultiplayerSubstateKeepDeepEquality(a, b).areEqual
},
Expand Down

0 comments on commit b968b0e

Please sign in to comment.