Using Redux Subspace with redux-observable
is pretty similar to using it with Redux itself, except rather than subspacing the store, you need to subspace the epics.
In order for redux-observable
middleware to work, it must only be applied to the root store and not to every subspace. You can use the applyToRoot
utility yourself, but redux-subspace-observable
provides a middleware that does that for you:
import { createStore } from 'redux'
import { applyMiddleware } from 'redux-subspace'
import { createEpicMiddleware } from 'redux-subspace-observable'
import { reducer, epic } from 'some-dependency'
const epicMiddleware = createEpicMiddleware(epic)
const store = createStore(reducer, applyMiddleware(epicMiddleware))
The subspaced
higher-order epic is used to subspace epics:
import { subspaced } from 'react-redux-observable'
import epic from 'some-dependency'
const subspacedEpic = subspaced((state) => state.subApp, 'subApp')(epic)
Now the epic will only recieve actions that match the subspace's namespace and any actions it emits will be automatically namespaced as well.
The store
parameter will also be a subspace created by the provided selector and namespace.
You can nest subspaces epics by combining them together like regular epics. By doing this, the standard nesting behavior of subspaces will still occur.