diff --git a/src/index.js b/src/index.js index f38485b..9acdc78 100644 --- a/src/index.js +++ b/src/index.js @@ -104,8 +104,9 @@ export const makeMidiEnhancer = ({midiOptions, stateKey = 'midi', requestMIDIAcc const enhancedStoreMethods = { dispatch (action) { - const {payload} = action; + action = store.dispatch(action); if (action.type === SEND_MIDI_MESSAGE) { + const {payload} = action; const {timestamp, data, device} = payload; if (midiAccess) { const {outputs} = midiAccess; @@ -114,7 +115,7 @@ export const makeMidiEnhancer = ({midiOptions, stateKey = 'midi', requestMIDIAcc } } } - return store.dispatch(action); + return action; } }; diff --git a/test/specs/midi-io.js b/test/specs/midi-io.js index dd09420..cb2c4b7 100644 --- a/test/specs/midi-io.js +++ b/test/specs/midi-io.js @@ -5,14 +5,21 @@ import MidiApi from 'web-midi-test-api'; import createMockStore from '../utils/mockStore'; import unique from 'lodash.uniq'; import sinon from 'sinon'; +import { applyMiddleware, compose } from 'redux'; describe('MIDI I/O', () => { let store, api; beforeEach(() => { api = new MidiApi(); - store = createMockStore(undefined, makeMidiEnhancer({ + const middleware = () => next => action => { + if (action.type === 'NOT_A_MIDI_MESSAGE') { + return next({...action, type: SEND_MIDI_MESSAGE}); + } + return next(action); + }; + store = createMockStore(undefined, compose(makeMidiEnhancer({ requestMIDIAccess: api.requestMIDIAccess - })); + }), applyMiddleware(middleware))); return new Promise(resolve => setImmediate(resolve)); }); it('should see no devices to begin with', () => { @@ -93,5 +100,14 @@ describe('MIDI I/O', () => { store.dispatch({ type: SEND_MIDI_MESSAGE, payload: {data: [0x80, 0x7f, 0x7f], timestamp: 1234, device: outputIds[0]} }); device.inputs[0].onmidimessage.should.have.been.calledWith({data: new Uint8Array([0x80, 0x7f, 0x7f]), receivedTime: 1234}); }); + it('should pick up on actions created by later middleware', () => { + const devices = store.getState().midi.devices; + const outputs = devices.filter(device => device.type === 'output'); + console.log(store.getState()); + const outputIds = outputs.map(device => device.id); + device.inputs[0].onmidimessage = sinon.spy(); + store.dispatch({ type: 'NOT_A_MIDI_MESSAGE', payload: {data: [0x80, 0x7f, 0x7f], timestamp: 1234, device: outputIds[0]} }); + device.inputs[0].onmidimessage.should.have.been.called; + }); }); });