generated from amosproj/amos202Xss0Y-projname
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #401 from amosproj/fix-graph-updates
Fix graph updates
- Loading branch information
Showing
7 changed files
with
157 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import 'reflect-metadata'; | ||
import { inject, injectable } from 'inversify'; | ||
import { Subscription } from 'rxjs'; | ||
import { GraphData } from 'react-graph-vis'; | ||
import { uuid } from 'uuidv4'; | ||
import { deepEqual } from 'fast-equals'; | ||
import SimpleStore from '../SimpleStore'; | ||
import QueryResultStore, { QueryResultMeta } from '../QueryResultStore'; | ||
import EntityStyleStore from '../colors/EntityStyleStore'; | ||
import convertQueryResult from '../../visualization/shared-ops/convertQueryResult'; | ||
import { EntityStyleProvider } from '../colors'; | ||
import { QueryResult } from '../../shared/queries'; | ||
|
||
export type UUID = string; | ||
|
||
export interface GraphState { | ||
graph: GraphData; | ||
key: UUID; | ||
/** | ||
* If undefined: no shortest path queried. | ||
* If false: shortest path queried but is not in result | ||
* If true: shortest path queried and is in result. | ||
*/ | ||
containsShortestPath?: boolean; | ||
} | ||
|
||
@injectable() | ||
export class GraphStateStore extends SimpleStore<GraphState> { | ||
protected getInitialValue(): GraphState { | ||
return { | ||
graph: { | ||
nodes: [], | ||
edges: [], | ||
}, | ||
key: uuid(), | ||
containsShortestPath: false, | ||
}; | ||
} | ||
|
||
private queryResultStoreSubscription?: Subscription; | ||
private entityStyleStoreSubscription?: Subscription; | ||
|
||
@inject(QueryResultStore) | ||
private readonly queryResultStore!: QueryResultStore; | ||
|
||
@inject(EntityStyleStore) | ||
private readonly entityStyleStore!: EntityStyleStore; | ||
|
||
protected ensureInit(): void { | ||
if (this.queryResultStoreSubscription == null) { | ||
this.queryResultStoreSubscription = this.subscribeQueryResultStore(); | ||
} | ||
|
||
if (this.entityStyleStoreSubscription == null) { | ||
this.entityStyleStoreSubscription = this.subscribeEntityStyleStore(); | ||
} | ||
} | ||
|
||
subscribeQueryResultStore(): Subscription { | ||
return this.queryResultStore.getState().subscribe({ | ||
next: (queryResult) => | ||
this.update(queryResult, this.entityStyleStore.getValue()), | ||
}); | ||
} | ||
|
||
subscribeEntityStyleStore(): Subscription { | ||
return this.entityStyleStore.getState().subscribe({ | ||
next: (styleProvider) => | ||
this.update(this.queryResultStore.getValue(), styleProvider), | ||
}); | ||
} | ||
|
||
update( | ||
queryResult: QueryResult & QueryResultMeta, | ||
styleProvider: EntityStyleProvider | ||
): void { | ||
const currentState = this.getValue(); | ||
const graph = convertQueryResult(queryResult, styleProvider); | ||
const { containsShortestPath } = queryResult; | ||
const updatedState = { | ||
graph, | ||
containsShortestPath, | ||
key: uuid(), | ||
}; | ||
|
||
// TODO: Do we need to update the render-token if containsShortestPath changed? | ||
if (deepEqual(graph, currentState.graph)) { | ||
// Make sure that the object are the same instance such that | ||
// the graph-vis library does not update the graph component. | ||
updatedState.graph = currentState.graph; | ||
updatedState.key = currentState.key; | ||
} | ||
|
||
this.setState(updatedState); | ||
} | ||
} | ||
|
||
export default GraphStateStore; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters