xen-service
Advanced tools
Comparing version 0.1.3 to 0.1.4
@@ -65,3 +65,3 @@ const { Observable } = require('rxjs/Observable'); | ||
return next().then(() => service | ||
.dispatch(action) | ||
.dispatch(action, true) | ||
.do(responseAction => { | ||
@@ -68,0 +68,0 @@ ctx.body = responseAction; |
{ | ||
"name": "xen-service", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "Simple and flexible microservice library", | ||
@@ -5,0 +5,0 @@ "main": "source/XenService.js", |
@@ -78,3 +78,6 @@ const { isFSA } = require('flux-standard-action'); | ||
} | ||
return function interserviceDispatchWithScopedState(action = {}) { | ||
return function interserviceDispatchWithScopedState( | ||
action = {}, | ||
forceLocal = false | ||
) { | ||
if (!isFSA(action)) { | ||
@@ -87,19 +90,12 @@ throw new Error( | ||
if ( | ||
!(dispatchMap[action.type] && typeof dispatchMap[action.type].endpoint === 'string') && | ||
typeof state.procedures[action.type] !== 'function' | ||
) { | ||
throw new Error( | ||
`Procedure "${action.type}" not implemented. Check your dispatchMap object.` | ||
); | ||
} | ||
// In some cases, it is necessary to dispatch an action locally, such as | ||
// when dispatching manually from a service subscription | ||
if (forceLocal) { | ||
if (typeof state.procedures[action.type] !== 'function') { | ||
throw new Error( | ||
`Local procedure "${action.type}" not implemented.` | ||
); | ||
} | ||
// !TODO: The logic to decide if something's a local proc is messy | ||
// !TODO: Decision: should local procs be invoked directly, or be treated as remotes? | ||
// debugger; | ||
if ( | ||
dispatchMap[action.type].service === state.name && | ||
typeof state.procedures[action.type] === 'function' | ||
) { | ||
// Local procedures | ||
// Execute local procedures | ||
return toObservable( | ||
@@ -113,3 +109,12 @@ state.procedures[action.type]( | ||
} else { | ||
// Remote procedures | ||
if ( | ||
!dispatchMap[action.type] || | ||
typeof dispatchMap[action.type].endpoint !== 'string' | ||
) { | ||
throw new Error( | ||
`Procedure "${action.type}" not implemented. Check your dispatchMap object.` | ||
); | ||
} | ||
// Dispatch remote action | ||
return fetchAction( | ||
@@ -116,0 +121,0 @@ dispatchMap[action.type].endpoint, |
@@ -102,3 +102,9 @@ /* eslint-env mocha */ | ||
it('should throw when passed procedure is not implemented', () => { | ||
it('should throw when passed local procedure is not implemented', () => { | ||
expect(() => | ||
interserviceDispatchWithDispatchMap({ procedures: {}, models: {}})({ type: 'test' }, true)) | ||
.to.throw(/Local procedure \"test\" not implemented/); | ||
}); | ||
it('should throw when passed remote procedure is not implemented', () => { | ||
expect( | ||
@@ -119,3 +125,3 @@ () => interserviceDispatchWithDispatchMap({ procedures: {}, models: {}})({ type: 'test' }) | ||
dispatch({ type: 'test' }).subscribe(() => { | ||
dispatch({ type: 'test' }, true).subscribe(() => { | ||
expect(procedureSpy.calledOnce).to.be.true; | ||
@@ -134,3 +140,3 @@ done(); | ||
}, testDispatchMap); | ||
dispatch({ type: 'test' }).subscribe(responseAction => { | ||
dispatch({ type: 'test' }, true).subscribe(responseAction => { | ||
expect(isFSA(responseAction)).to.be.true; | ||
@@ -153,5 +159,5 @@ expect(responseAction.payload).to.equal('G-man'); | ||
}, testDispatchMap); | ||
expect(dispatch({ type: 'test' })).to.be.an.instanceof(Observable); | ||
expect(dispatch({ type: 'testPromise' })).to.be.an.instanceof(Observable); | ||
expect(dispatch({ type: 'testObservable' })).to.be.an.instanceof(Observable); | ||
expect(dispatch({ type: 'test' }, true)).to.be.an.instanceof(Observable); | ||
expect(dispatch({ type: 'testPromise' }, true)).to.be.an.instanceof(Observable); | ||
expect(dispatch({ type: 'testObservable' }, true)).to.be.an.instanceof(Observable); | ||
}); | ||
@@ -158,0 +164,0 @@ |
56818
1632