Commit 148aa3c6 authored by Robert Knight's avatar Robert Knight

Convert state of drafts and frames store modules to objects

Change the state of the drafts and frames store modules from arrays to objects
containing a single array-valued field. This will allow additional fields to be
added to these modules in future. It also means that store modules are now more
uniform, as all modules now have an object, rather than array, for their
`initialState`.
parent 992c4cb1
...@@ -13,8 +13,10 @@ import { removeAnnotations } from './annotations'; ...@@ -13,8 +13,10 @@ import { removeAnnotations } from './annotations';
* existing annotations. * existing annotations.
*/ */
/** @type {Draft[]} */ const initialState = {
const initialState = []; /** @type {Draft[]} */
drafts: [],
};
/** @typedef {typeof initialState} State */ /** @typedef {typeof initialState} State */
...@@ -72,7 +74,7 @@ export class Draft { ...@@ -72,7 +74,7 @@ export class Draft {
const reducers = { const reducers = {
DISCARD_ALL_DRAFTS() { DISCARD_ALL_DRAFTS() {
return []; return { drafts: [] };
}, },
/** /**
...@@ -80,10 +82,10 @@ const reducers = { ...@@ -80,10 +82,10 @@ const reducers = {
* @param {{ annotation: AnnotationID }} action * @param {{ annotation: AnnotationID }} action
*/ */
REMOVE_DRAFT(state, action) { REMOVE_DRAFT(state, action) {
const drafts = state.filter(draft => { const drafts = state.drafts.filter(draft => {
return !draft.match(action.annotation); return !draft.match(action.annotation);
}); });
return drafts; return { drafts };
}, },
/** /**
...@@ -92,11 +94,11 @@ const reducers = { ...@@ -92,11 +94,11 @@ const reducers = {
*/ */
UPDATE_DRAFT(state, action) { UPDATE_DRAFT(state, action) {
// removes a matching existing draft, then adds // removes a matching existing draft, then adds
const drafts = state.filter(draft => { const drafts = state.drafts.filter(draft => {
return !draft.match(action.draft.annotation); return !draft.match(action.draft.annotation);
}); });
drafts.push(action.draft); // push ok since its a copy drafts.push(action.draft); // push ok since its a copy
return drafts; return { drafts };
}, },
}; };
...@@ -124,7 +126,7 @@ function deleteNewAndEmptyDrafts() { ...@@ -124,7 +126,7 @@ function deleteNewAndEmptyDrafts() {
* @param {() => { drafts: State }} getState * @param {() => { drafts: State }} getState
*/ */
return (dispatch, getState) => { return (dispatch, getState) => {
const newDrafts = getState().drafts.filter(draft => { const newDrafts = getState().drafts.drafts.filter(draft => {
return ( return (
!draft.annotation.id && !draft.annotation.id &&
!getDraftIfNotEmpty(getState().drafts, draft.annotation) !getDraftIfNotEmpty(getState().drafts, draft.annotation)
...@@ -161,7 +163,7 @@ function removeDraft(annotation) { ...@@ -161,7 +163,7 @@ function removeDraft(annotation) {
* @param {State} state * @param {State} state
*/ */
function countDrafts(state) { function countDrafts(state) {
return state.length; return state.drafts.length;
} }
/** /**
...@@ -171,7 +173,7 @@ function countDrafts(state) { ...@@ -171,7 +173,7 @@ function countDrafts(state) {
* @param {AnnotationID} annotation * @param {AnnotationID} annotation
*/ */
function getDraft(state, annotation) { function getDraft(state, annotation) {
const drafts = state; const drafts = state.drafts;
for (let i = 0; i < drafts.length; i++) { for (let i = 0; i < drafts.length; i++) {
const draft = drafts[i]; const draft = drafts[i];
if (draft.match(annotation)) { if (draft.match(annotation)) {
...@@ -202,7 +204,7 @@ function getDraftIfNotEmpty(state, annotation) { ...@@ -202,7 +204,7 @@ function getDraftIfNotEmpty(state, annotation) {
*/ */
const unsavedAnnotations = createSelector( const unsavedAnnotations = createSelector(
/** @param {State} state */ /** @param {State} state */
state => state, state => state.drafts,
drafts => drafts.filter(d => !d.annotation.id).map(d => d.annotation) drafts => drafts.filter(d => !d.annotation.id).map(d => d.annotation)
); );
......
...@@ -20,8 +20,10 @@ import { createStoreModule, makeAction } from '../create-store'; ...@@ -20,8 +20,10 @@ import { createStoreModule, makeAction } from '../create-store';
* @prop {boolean} [isAnnotationFetchComplete] * @prop {boolean} [isAnnotationFetchComplete]
*/ */
/** @type {Frame[]} */ const initialState = {
const initialState = []; /** @type {Frame[]} */
frames: [],
};
/** @typedef {typeof initialState} State */ /** @typedef {typeof initialState} State */
...@@ -31,14 +33,16 @@ const reducers = { ...@@ -31,14 +33,16 @@ const reducers = {
* @param {{ frame: Frame }} action * @param {{ frame: Frame }} action
*/ */
CONNECT_FRAME(state, action) { CONNECT_FRAME(state, action) {
const frameIndex = state.findIndex(frame => frame.id === action.frame.id); const frameIndex = state.frames.findIndex(
const newFrames = [...state]; frame => frame.id === action.frame.id
);
const newFrames = [...state.frames];
if (frameIndex !== -1) { if (frameIndex !== -1) {
newFrames[frameIndex] = action.frame; newFrames[frameIndex] = action.frame;
} else { } else {
newFrames.push(action.frame); newFrames.push(action.frame);
} }
return newFrames; return { frames: newFrames };
}, },
/** /**
...@@ -46,7 +50,8 @@ const reducers = { ...@@ -46,7 +50,8 @@ const reducers = {
* @param {{ frame: Frame }} action * @param {{ frame: Frame }} action
*/ */
DESTROY_FRAME(state, action) { DESTROY_FRAME(state, action) {
return state.filter(f => f !== action.frame); const frames = state.frames.filter(f => f !== action.frame);
return { frames };
}, },
/** /**
...@@ -54,7 +59,7 @@ const reducers = { ...@@ -54,7 +59,7 @@ const reducers = {
* @param {{ uri: string, isAnnotationFetchComplete: boolean }} action * @param {{ uri: string, isAnnotationFetchComplete: boolean }} action
*/ */
UPDATE_FRAME_ANNOTATION_FETCH_STATUS(state, action) { UPDATE_FRAME_ANNOTATION_FETCH_STATUS(state, action) {
const frames = state.map(frame => { const frames = state.frames.map(frame => {
const match = frame.uri && frame.uri === action.uri; const match = frame.uri && frame.uri === action.uri;
if (match) { if (match) {
return Object.assign({}, frame, { return Object.assign({}, frame, {
...@@ -64,7 +69,7 @@ const reducers = { ...@@ -64,7 +69,7 @@ const reducers = {
return frame; return frame;
} }
}); });
return frames; return { frames };
}, },
}; };
...@@ -108,7 +113,7 @@ function updateFrameAnnotationFetchStatus(uri, isFetchComplete) { ...@@ -108,7 +113,7 @@ function updateFrameAnnotationFetchStatus(uri, isFetchComplete) {
* @param {State} state * @param {State} state
*/ */
function frames(state) { function frames(state) {
return state; return state.frames;
} }
/** /**
...@@ -123,7 +128,7 @@ function frames(state) { ...@@ -123,7 +128,7 @@ function frames(state) {
*/ */
const mainFrame = createSelector( const mainFrame = createSelector(
/** @param {State} state */ /** @param {State} state */
state => state, state => state.frames,
// Sub-frames will all have a "frame identifier" set. The main frame is the // Sub-frames will all have a "frame identifier" set. The main frame is the
// one with a `null` id. // one with a `null` id.
...@@ -164,9 +169,9 @@ const createShallowEqualSelector = createSelectorCreator( ...@@ -164,9 +169,9 @@ const createShallowEqualSelector = createSelectorCreator(
* values of the array change (are not shallow-equal). * values of the array change (are not shallow-equal).
*/ */
const searchUris = createShallowEqualSelector( const searchUris = createShallowEqualSelector(
/** @param {State} frames */ /** @param {State} state */
frames => state =>
frames.reduce( state.frames.reduce(
(uris, frame) => uris.concat(searchUrisForFrame(frame)), (uris, frame) => uris.concat(searchUrisForFrame(frame)),
/** @type {string[]} */ ([]) /** @type {string[]} */ ([])
), ),
......
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