- Notifications
You must be signed in to change notification settings - Fork524
Description
Hi All, given the following class that inherits fromStoreSubscriber, I'm trying to subscribe only to a sub-state. But even if another sub-state is updated, then this subscriber is notified about an update.
classPortWindowService:StoreSubscriber{typealiasStoreSubscriberStateType=ModelStatefunc start(){ store.subscribe(self){ $0.select{ $0.modelState}}}deinit{ store.unsubscribe(self)}func newState(state:ModelState){print(state.ports.count)print("ports changed")}}
My app state is:
structAppState{varmodelState:ModelState=ModelState()varvolumeState:VolumeState=VolumeState()}structModelState{varmodel:StoredModel?=nilvarports:[Port]=[]}structVolumeState{varvolumes:[VolumeInfo]=[]}
And my reducers are:
func appReducer(action:Action, state:AppState?)->AppState{returnAppState( modelState:modelReducer(action: action, state: state?.modelState), volumeState:volumeReducer(action: action, state: state?.volumeState))}func modelReducer(action:Action, state:ModelState?)->ModelState{varstate= state??ModelState()switch action{caseletaction asSetModel: state.model= action.model state.model?.usbPortsLeft.enumerated().forEach{(index, portNumber)in state.ports.append(Port(portNumber: portNumber, state:.unknown, area:.left))} state.model?.usbPortsRight.enumerated().forEach{(index, portNumber)in state.ports.append(Port(portNumber: portNumber, state:.unknown, area:.right))} state.model?.usbPortsBack.enumerated().forEach{(index, portNumber)in state.ports.append(Port(portNumber: portNumber, state:.unknown, area:.back))}default:break}return state}func volumeReducer(action:Action, state:VolumeState?)->VolumeState{varstate= state??VolumeState()switch action{caseletaction asAddVolume:iflet volumeInfo= action.volume{ state.volumes.append(volumeInfo)}caseletaction asRemoveVolume:iflet basePath= action.basePath{ state.volumes.removeAll(where:{ $0.volumePath== basePath})}default:break}return state}
TheSetModel action is only called twice during startup. So I assume that "ports changed" is only printed twice (seefunc newState in thePortWindowService class). But even when the actionsAddVolume orRemoveVolume are called in a different reducer, in a different sub-state, which is not selected during subscription in theStoreSubscriber initialization, I'll get an event andnewState is called.
What am I missing here? How can I make sure that withstore.subscribe(self) { $0.select { $0.modelState } }newState is only called when anything inmodelState changes, but not involumeState?