Commit 146f5ddb authored by Kyle Keating's avatar Kyle Keating

Namespace the activity module

parent fee80c6d
...@@ -9,69 +9,55 @@ const { actionTypes } = require('../util'); ...@@ -9,69 +9,55 @@ const { actionTypes } = require('../util');
function init() { function init() {
return { return {
activity: { /**
/** * The number of API requests that have started and not yet completed.
* The number of API requests that have started and not yet completed. */
*/ activeApiRequests: 0,
activeApiRequests: 0, /**
/** * The number of annotation fetches that have started and not yet completed.
* The number of annotation fetches that have started and not yet completed. */
*/ activeAnnotationFetches: 0,
activeAnnotationFetches: 0,
},
}; };
} }
const update = { const update = {
API_REQUEST_STARTED(state) { API_REQUEST_STARTED(state) {
const { activity } = state;
return { return {
activity: { ...state,
...activity, activeApiRequests: state.activeApiRequests + 1,
activeApiRequests: activity.activeApiRequests + 1,
},
}; };
}, },
API_REQUEST_FINISHED(state) { API_REQUEST_FINISHED(state) {
const { activity } = state; if (state.activeApiRequests === 0) {
if (activity.activeApiRequests === 0) {
throw new Error( throw new Error(
'API_REQUEST_FINISHED action when no requests were active' 'API_REQUEST_FINISHED action when no requests were active'
); );
} }
return { return {
activity: { ...state,
...activity, activeApiRequests: state.activeApiRequests - 1,
activeApiRequests: activity.activeApiRequests - 1,
},
}; };
}, },
ANNOTATION_FETCH_STARTED(state) { ANNOTATION_FETCH_STARTED(state) {
const { activity } = state;
return { return {
activity: { ...state,
...activity, activeAnnotationFetches: state.activeAnnotationFetches + 1,
activeAnnotationFetches: activity.activeAnnotationFetches + 1,
},
}; };
}, },
ANNOTATION_FETCH_FINISHED(state) { ANNOTATION_FETCH_FINISHED(state) {
const { activity } = state; if (state.activeAnnotationFetches === 0) {
if (activity.activeAnnotationFetches === 0) {
throw new Error( throw new Error(
'ANNOTATION_FETCH_FINISHED action when no annotation fetches were active' 'ANNOTATION_FETCH_FINISHED action when no annotation fetches were active'
); );
} }
return { return {
activity: { ...state,
...activity, activeAnnotationFetches: state.activeAnnotationFetches - 1,
activeAnnotationFetches: activity.activeAnnotationFetches - 1,
},
}; };
}, },
}; };
...@@ -112,6 +98,7 @@ function isFetchingAnnotations(state) { ...@@ -112,6 +98,7 @@ function isFetchingAnnotations(state) {
module.exports = { module.exports = {
init, init,
update, update,
namespace: 'activity',
actions: { actions: {
apiRequestStarted, apiRequestStarted,
......
...@@ -57,7 +57,7 @@ describe('sidebar/store/modules/activity', () => { ...@@ -57,7 +57,7 @@ describe('sidebar/store/modules/activity', () => {
}); });
it('defaults `activeAnnotationFetches` counter to zero', () => { it('defaults `activeAnnotationFetches` counter to zero', () => {
assert.equal(store.getState().activity.activeAnnotationFetches, 0); assert.equal(store.getRootState().activity.activeAnnotationFetches, 0);
}); });
describe('annotationFetchFinished', () => { describe('annotationFetchFinished', () => {
...@@ -70,7 +70,7 @@ describe('sidebar/store/modules/activity', () => { ...@@ -70,7 +70,7 @@ describe('sidebar/store/modules/activity', () => {
it('increments `activeAnnotationFetches` counter when a new annotation fetch is started', () => { it('increments `activeAnnotationFetches` counter when a new annotation fetch is started', () => {
store.annotationFetchStarted(); store.annotationFetchStarted();
assert.equal(store.getState().activity.activeAnnotationFetches, 1); assert.equal(store.getRootState().activity.activeAnnotationFetches, 1);
}); });
}); });
...@@ -86,7 +86,7 @@ describe('sidebar/store/modules/activity', () => { ...@@ -86,7 +86,7 @@ describe('sidebar/store/modules/activity', () => {
store.annotationFetchFinished(); store.annotationFetchFinished();
assert.equal(store.getState().activity.activeAnnotationFetches, 0); assert.equal(store.getRootState().activity.activeAnnotationFetches, 0);
}); });
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment