Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Commit 339e504

Browse files
davide-dc-devjasonLaster
authored andcommitted
[Sources] Use Immutable records (#5389)
1 parent 744d62f commit 339e504

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`sources - pretty print returns a pretty source for a minified file 1`] = `
4-
Immutable.Map {
5-
url: "http://localhost:8000/examples/base.js:formatted",
6-
id: "base.js/originalSource-36c718d4bde9a75edb388ff7733efe7f",
7-
isPrettyPrinted: true,
8-
contentType: "text/javascript",
9-
loadedState: "loaded",
10-
text: "undefined
11-
",
4+
Object {
5+
"contentType": "text/javascript",
6+
"error": undefined,
7+
"id": "base.js/originalSource-36c718d4bde9a75edb388ff7733efe7f",
8+
"isBlackBoxed": false,
9+
"isPrettyPrinted": true,
10+
"isWasm": false,
11+
"loadedState": "loaded",
12+
"sourceMapURL": undefined,
13+
"text": "undefined
14+
",
15+
"url": "http://localhost:8000/examples/base.js:formatted",
1216
}
1317
`;

src/reducers/sources.js

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ export function initialSourcesState(): Record<SourcesState> {
4747
)();
4848
}
4949

50+
export const SourceRecordClass = new I.Record({
51+
id: undefined,
52+
url: undefined,
53+
sourceMapURL: undefined,
54+
isBlackBoxed: false,
55+
isPrettyPrinted: false,
56+
isWasm: false,
57+
text: undefined,
58+
contentType: "",
59+
error: undefined,
60+
loadedState: "unloaded"
61+
});
62+
5063
function update(
5164
state: Record<SourcesState> = initialSourcesState(),
5265
action: Action
@@ -60,7 +73,8 @@ function update(
6073
}
6174

6275
case "ADD_SOURCE": {
63-
return updateSource(state, action.source);
76+
const source = action.source;
77+
return updateSource(state, source);
6478
}
6579

6680
case "ADD_SOURCES": {
@@ -133,7 +147,7 @@ function update(
133147

134148
case "NAVIGATE":
135149
const source = getSelectedSource({ sources: state });
136-
const url = source && source.get("url");
150+
const url = source && source.url;
137151

138152
if (!url) {
139153
return initialSourcesState();
@@ -174,8 +188,13 @@ function updateSource(state: Record<SourcesState>, source: Source | Object) {
174188
if (!source.id) {
175189
return state;
176190
}
191+
const existingSource = state.getIn(["sources", source.id]);
177192

178-
return state.mergeIn(["sources", source.id], source);
193+
if (existingSource) {
194+
const updatedSource = existingSource.merge(source);
195+
return state.setIn(["sources", source.id], updatedSource);
196+
}
197+
return state.setIn(["sources", source.id], new SourceRecordClass(source));
179198
}
180199

181200
export function removeSourceFromTabList(tabs: any, url: string) {
@@ -201,7 +220,7 @@ function restoreTabs() {
201220
* @static
202221
*/
203222
function updateTabList(state: OuterState, url: ?string, tabIndex?: number) {
204-
let tabs = state.sources.get("tabs");
223+
let tabs = state.sources.tabs;
205224

206225
const urlIndex = tabs.indexOf(url);
207226
const includesUrl = !!tabs.find(tab => tab == url);
@@ -238,20 +257,18 @@ export function getNewSelectedSourceId(
238257

239258
const selectedTab = state.sources.sources.get(selectedLocation.sourceId);
240259

241-
const selectedTabUrl = selectedTab ? selectedTab.get("url") : "";
260+
const selectedTabUrl = selectedTab ? selectedTab.url : "";
242261

243262
if (availableTabs.includes(selectedTabUrl)) {
244263
const sources = state.sources.sources;
245264
if (!sources) {
246265
return "";
247266
}
248267

249-
const selectedSource = sources.find(
250-
source => source.get("url") == selectedTabUrl
251-
);
268+
const selectedSource = sources.find(source => source.url == selectedTabUrl);
252269

253270
if (selectedSource) {
254-
return selectedSource.get("id");
271+
return selectedSource.id;
255272
}
256273

257274
return "";
@@ -268,7 +285,7 @@ export function getNewSelectedSourceId(
268285
);
269286

270287
if (tabSource) {
271-
return tabSource.get("id");
288+
return tabSource.id;
272289
}
273290

274291
return "";
@@ -312,7 +329,7 @@ export function getPrettySource(state: OuterState, id: string) {
312329
return;
313330
}
314331

315-
return getSourceByURL(state, getPrettySourceURL(source.get("url")));
332+
return getSourceByURL(state, getPrettySourceURL(source.url));
316333
}
317334

318335
export function hasPrettySource(state: OuterState, id: string) {
@@ -324,7 +341,7 @@ function getSourceByUrlInSources(sources: SourcesMap, url: string) {
324341
return null;
325342
}
326343

327-
return sources.find(source => source.get("url") === url);
344+
return sources.find(source => source.url === url);
328345
}
329346

330347
export function getSourceInSources(
@@ -378,7 +395,7 @@ export const getSelectedSourceText = createSelector(
378395
getSelectedSource,
379396
getSourcesState,
380397
(selectedSource, sources) => {
381-
const id = selectedSource.get("id");
398+
const id = selectedSource.id;
382399
return id ? sources.sourcesText.get(id) : null;
383400
}
384401
);

0 commit comments

Comments
 (0)