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

Commit 23ed947

Browse files
रोहन मल्होत्राjasonLaster
authored andcommitted
refactored loading states & added loading in outline (#5651)
refactored symbol actions & reducers to show proper loading flag
1 parent 7b00f36 commit 23ed947

File tree

7 files changed

+49
-27
lines changed

7 files changed

+49
-27
lines changed

src/actions/ast.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
findOutOfScopeLocations,
1919
getFramework
2020
} from "../workers/parser";
21+
import { PROMISE } from "./utils/middleware/promise";
2122

2223
import type { SourceId } from "../types";
2324
import type { ThunkArgs } from "./types";
@@ -57,8 +58,12 @@ export function setSymbols(sourceId: SourceId) {
5758
return;
5859
}
5960

60-
const symbols = await getSymbols(source.id);
61-
dispatch({ type: "SET_SYMBOLS", source, symbols });
61+
await dispatch({
62+
type: "SET_SYMBOLS",
63+
source,
64+
[PROMISE]: getSymbols(source.id)
65+
});
66+
6267
dispatch(setEmptyLines(sourceId));
6368
dispatch(setSourceMetaData(sourceId));
6469
};

src/actions/preview.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export function updatePreview(target: HTMLElement, editor: any) {
125125
const symbols = getSymbols(getState(), source.toJS());
126126

127127
let match;
128-
if (!symbols || symbols.identifiers.length > 0) {
128+
if (!symbols || symbols.loading) {
129129
match = findBestMatchExpression(symbols, tokenPos, tokenText);
130130
} else {
131131
match = getExpressionFromCoords(editor.codeMirror, tokenPos);

src/actions/tests/ast.spec.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ const {
1616
getEmptyLines,
1717
getOutOfScopeLocations,
1818
getSourceMetaData,
19-
getInScopeLines
19+
getInScopeLines,
20+
isSymbolsLoading
2021
} = selectors;
2122

2223
import I from "immutable";
@@ -114,32 +115,29 @@ describe("ast", () => {
114115
await dispatch(actions.newSource(base));
115116
await dispatch(actions.loadSourceText(I.Map({ id: "base.js" })));
116117
await dispatch(actions.setSymbols("base.js"));
117-
await waitForState(
118-
store,
119-
state => getSymbols(state, base).functions.length > 0
120-
);
118+
await waitForState(store, state => !isSymbolsLoading(state, base));
121119

122120
const baseSymbols = getSymbols(getState(), base);
123121
expect(baseSymbols).toMatchSnapshot();
124122
});
125123
});
126124

127125
describe("when the source is not loaded", () => {
128-
it("should return an empty set", async () => {
126+
it("should return null", async () => {
129127
const { getState, dispatch } = createStore(threadClient);
130128
const base = makeSource("base.js");
131129
await dispatch(actions.newSource(base));
132130

133131
const baseSymbols = getSymbols(getState(), base);
134-
expect(baseSymbols).toEqual({ variables: [], functions: [] });
132+
expect(baseSymbols).toEqual(null);
135133
});
136134
});
137135

138136
describe("when there is no source", () => {
139-
it("should return an empty set", async () => {
137+
it("should return null", async () => {
140138
const { getState } = createStore(threadClient);
141139
const baseSymbols = getSymbols(getState());
142-
expect(baseSymbols).toEqual({ variables: [], functions: [] });
140+
expect(baseSymbols).toEqual(null);
143141
});
144142
});
145143
});

src/actions/types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ type ASTAction =
315315
| {
316316
type: "SET_SYMBOLS",
317317
source: Source,
318-
symbols: SymbolDeclaration[]
318+
value: SymbolDeclaration[]
319319
}
320320
| {
321321
type: "SET_EMPTY_LINES",

src/components/PrimaryPanes/Outline.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ export class Outline extends Component<Props> {
4747
return <div className="outline-pane-info">{placeholderMessage}</div>;
4848
}
4949

50+
renderLoading() {
51+
return (
52+
<div className="outline-pane-info">{L10N.getStr("loadingText")}</div>
53+
);
54+
}
55+
5056
renderFunction(func: SymbolDeclaration) {
5157
const { name, location, parameterNames } = func;
5258

@@ -132,11 +138,12 @@ export class Outline extends Component<Props> {
132138

133139
render() {
134140
const { symbols } = this.props;
135-
141+
if (!symbols || symbols.loading) {
142+
return this.renderLoading();
143+
}
136144
const symbolsToDisplay = symbols.functions.filter(
137145
func => func.name != "anonymous"
138146
);
139-
140147
return (
141148
<div className="outline">
142149
{symbolsToDisplay.length > 0

src/reducers/ast.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import type { Action } from "../actions/types";
2020
import type { Record } from "../utils/makeRecord";
2121

2222
type EmptyLinesType = number[];
23-
export type SymbolsMap = Map<string, SymbolDeclarations>;
23+
export type Symbols = SymbolDeclarations | { loading: true };
24+
export type SymbolsMap = Map<string, Symbols>;
2425
export type EmptyLinesMap = Map<string, EmptyLinesType>;
2526

2627
export type SourceMetaDataType = {
@@ -70,10 +71,12 @@ function update(
7071
): Record<ASTState> {
7172
switch (action.type) {
7273
case "SET_SYMBOLS": {
73-
const { source, symbols } = action;
74-
return state.setIn(["symbols", source.id], symbols);
74+
const { source } = action;
75+
if (action.status === "start") {
76+
return state.setIn(["symbols", source.id], { loading: true });
77+
}
78+
return state.setIn(["symbols", source.id], action.value);
7579
}
76-
7780
case "SET_EMPTY_LINES": {
7881
const { source, emptyLines } = action;
7982
return state.setIn(["emptyLines", source.id], emptyLines);
@@ -131,25 +134,34 @@ function update(
131134
// https://github.com/devtools-html/debugger.html/blob/master/src/reducers/sources.js#L179-L185
132135
type OuterState = { ast: Record<ASTState> };
133136

134-
const emptySymbols = { variables: [], functions: [] };
135137
export function getSymbols(
136138
state: OuterState,
137139
source: Source
138-
): SymbolDeclarations {
140+
): ?SymbolDeclarations {
139141
if (!source) {
140-
return emptySymbols;
142+
return null;
141143
}
142144

143-
const symbols = state.ast.getIn(["symbols", source.id]);
144-
return symbols || emptySymbols;
145+
return state.ast.getIn(["symbols", source.id]) || null;
145146
}
146147

147148
export function hasSymbols(state: OuterState, source: Source): boolean {
148-
if (!source) {
149+
const symbols = getSymbols(state, source);
150+
151+
if (!symbols) {
152+
return false;
153+
}
154+
155+
return !symbols.loading;
156+
}
157+
158+
export function isSymbolsLoading(state: OuterState, source: Source): boolean {
159+
const symbols = getSymbols(state, source);
160+
if (!symbols) {
149161
return false;
150162
}
151163

152-
return !!state.ast.getIn(["symbols", source.id]);
164+
return !!symbols.loading;
153165
}
154166

155167
export function isEmptyLineInSource(

src/utils/breakpoint/astBreakpointLocation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export function getASTLocation(
5252
symbols: SymbolDeclarations,
5353
location: Location
5454
): ASTLocation {
55-
if (source.isWasm) {
55+
if (source.isWasm || !symbols || symbols.loading) {
5656
return { name: undefined, offset: location };
5757
}
5858

0 commit comments

Comments
 (0)