I am implementing auto-save for a react-redux-firebase side project using redux-observable. Currently I have a updateFretboardEpic that is responding to any actions that modify a Fretboard component currently possessing a reference (firebaseKey) to the Firebase database. After 1 second debounce, updateFretboard should save the new state of the component to Firebase.
import {
ADD_STRING,
REMOVE_STRING,
INC_STR_PITCH,
DEC_STR_PITCH,
ADD_FRET,
REMOVE_FRET,
UPDATE_FRETBOARD
} from '../actions/action-types';
import {
updateFretboard
} from '../actions/fretboard-actions';
export const updateFretboardEpic = (action$, store) => {
return action$.ofType(
ADD_STRING,
REMOVE_STRING,
INC_STR_PITCH,
DEC_STR_PITCH,
ADD_FRET,
REMOVE_FRET
)
.filter(() => store.getState().fretboard.firebaseKey !== undefined)
.debounceTime(1000)
.map(() => updateFretboard(
store.getState().fretboard.fretboardData,
store.getState().fretboard.firebaseKey
));
};
However, I am currently getting the following error:
Uncaught Error: Actions must be plain objects. Use custom middleware for async actions.
at Object.performAction (<anonymous>:3:2312)
at liftAction (<anonymous>:2:27846)
at dispatch (<anonymous>:2:31884)
at bundle.ff2509b….js:9896
at SafeSubscriber.dispatch [as _next] (vendor.ff2509b….js:51341)
at SafeSubscriber.__tryOrUnsub (bundle.ff2509b….js:392)
at SafeSubscriber.next (bundle.ff2509b….js:339)
at Subscriber._next (bundle.ff2509b….js:279)
at Subscriber.next (bundle.ff2509b….js:243)
at SwitchMapSubscriber.notifyNext (bundle.ff2509b….js:6579)
Prior to implementing redux-observable, updateFretboard was using Redux-Thunk to dispatch an action:
export function updateFretboard(fretboardData, firebaseKey = undefined) {
return dispatch => { fretboards.child(firebaseKey).update(fretboardData); };
}
Using that as it stands with redux-observable will produce the error without any auto-save. Instead of returning a thunk, I changed it to this:
export function updateFretboard(fretboardData, firebaseKey = undefined) {
return fretboards.child(firebaseKey).update(fretboardData);
}
Interestingly, updateFretboardEpic will auto-save for the first action in the stream, return the error, and will not auto-save for any subsequent actions thereafter. updateFretboard does not currently flow through any of my reducers (it is only responsible for passing new state to Firebase), although I may choose in the future to receive a promise to know when the save occurred and pass it through my reducers.
I am new to RxJS/redux-observable, so I suspect there is a better way of doing this. Thoughts?