Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 33 additions & 21 deletions src/reducers/sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,16 @@ function update(
switch (action.type) {
case "UPDATE_SOURCE": {
const source = action.source;
return updateSource(state, source);
return updateSources(state, [source]);
}

case "ADD_SOURCE": {
const source = action.source;
return updateSource(state, source);
return updateSources(state, [source]);
}

case "ADD_SOURCES": {
return action.sources.reduce(
(newState, source) => updateSource(newState, source),
state
);
return updateSources(state, action.sources);
}

case "SET_SELECTED_LOCATION":
Expand Down Expand Up @@ -127,7 +124,7 @@ function update(
const { id, url } = action.source;
const { isBlackBoxed } = ((action: any): DonePromiseAction).value;
updateBlackBoxList(url, isBlackBoxed);
return updateSource(state, { id, isBlackBoxed });
return updateSources(state, [{ id, isBlackBoxed }]);
}
break;

Expand Down Expand Up @@ -161,8 +158,8 @@ function getTextPropsFromAction(action) {
}

return {
text: action.value.text,
id: sourceId,
text: action.value.text,
contentType: action.value.contentType,
loadedState: "loaded"
};
Expand All @@ -173,8 +170,22 @@ function getTextPropsFromAction(action) {
// "start" and "error" states but we don't type it like that. We need
// to rethink how we type async actions.
function setSourceTextProps(state, action: LoadSourceAction): SourcesState {
const text = getTextPropsFromAction(action);
return updateSource(state, text);
const source = getTextPropsFromAction(action);
return updateSources(state, [source]);
}

function updateSources(state, sources) {
state = {
...state,
sources: { ...state.sources },
relativeSources: { ...state.relativeSources },
urls: { ...state.urls }
};

return sources.reduce(
(newState, source) => updateSource(newState, source),
state
);
}

function updateSource(state: SourcesState, source: Object) {
Expand All @@ -187,19 +198,20 @@ function updateSource(state: SourcesState, source: Object) {
? { ...existingSource, ...source }
: createSource(source);

state.sources[source.id] = updatedSource;

const existingUrls = state.urls[source.url];
const urls = existingUrls ? [...existingUrls, source.id] : [source.id];
state.urls[source.url] = existingUrls
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to take into account origin vs. generated here? Since a sourcemap can have the same URL?

? [...existingUrls, source.id]
: [source.id];

updateRelativeSource(
state.relativeSources,
updatedSource,
state.projectDirectoryRoot
);

return {
...state,
relativeSources: updateRelativeSource(
{ ...state.relativeSources },
updatedSource,
state.projectDirectoryRoot
),
sources: { ...state.sources, [source.id]: updatedSource },
urls: { ...state.urls, [source.url]: urls }
};
return state;
}

function updateRelativeSource(
Expand Down