StateTransitionMachine.loadGraph reports success (#757)

Step one towards #586. This will enable us to chain runPagerank after
loadGraph only if the load went through successfully.

Test plan: Unit tests included.
This commit is contained in:
Dandelion Mané 2018-09-03 14:33:25 -07:00 committed by GitHub
parent b92d6138d3
commit 241905f6d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 5 deletions

View File

@ -79,7 +79,7 @@ export function initialState(): AppState {
export interface StateTransitionMachineInterface {
+setRepo: (Repo) => void;
+setEdgeEvaluator: (EdgeEvaluator) => void;
+loadGraph: (Assets) => Promise<void>;
+loadGraph: (Assets) => Promise<boolean>;
+runPagerank: (NodeAddressT) => Promise<void>;
}
/* In production, instantiate via createStateTransitionMachine; the constructor
@ -167,7 +167,8 @@ export class StateTransitionMachine implements StateTransitionMachineInterface {
}
}
async loadGraph(assets: Assets) {
/** Loads the graph, reports whether it was successful */
async loadGraph(assets: Assets): Promise<boolean> {
const state = this.getState();
if (
state.type !== "INITIALIZED" ||
@ -182,6 +183,7 @@ export class StateTransitionMachine implements StateTransitionMachineInterface {
};
this.setState(loadingState);
let newState: ?AppState;
let success = true;
try {
const graphWithAdapters = await this.loadGraphWithAdapters(assets, repo);
newState = {
@ -195,10 +197,13 @@ export class StateTransitionMachine implements StateTransitionMachineInterface {
} catch (e) {
console.error(e);
newState = {...state, substate: {...substate, loading: "FAILED"}};
success = false;
}
if (deepEqual(this.getState(), loadingState)) {
this.setState(newState);
return success;
}
return false;
}
async runPagerank(totalScoreNodePrefix: NodeAddressT) {

View File

@ -226,7 +226,8 @@ describe("app/credExplorer/state", () => {
const {getState, stm, loadGraphMock} = example(readyToLoadGraph());
const gwa = graphWithAdapters();
loadGraphMock.mockResolvedValue(gwa);
await stm.loadGraph(new Assets("/my/gateway/"));
const succeeded = await stm.loadGraph(new Assets("/my/gateway/"));
expect(succeeded).toBe(true);
const state = getState();
const substate = getSubstate(state);
expect(loading(state)).toBe("NOT_LOADING");
@ -246,7 +247,8 @@ describe("app/credExplorer/state", () => {
resolve(graphWithAdapters());
})
);
await stm.loadGraph(new Assets("/my/gateway/"));
const succeeded = await stm.loadGraph(new Assets("/my/gateway/"));
expect(succeeded).toBe(false);
const state = getState();
const substate = getSubstate(state);
expect(loading(state)).toBe("NOT_LOADING");
@ -259,7 +261,8 @@ describe("app/credExplorer/state", () => {
// $ExpectFlowError
console.error = jest.fn();
loadGraphMock.mockRejectedValue(error);
await stm.loadGraph(new Assets("/my/gateway/"));
const succeeded = await stm.loadGraph(new Assets("/my/gateway/"));
expect(succeeded).toBe(false);
const state = getState();
const substate = getSubstate(state);
expect(loading(state)).toBe("FAILED");