Skip to content

Commit

Permalink
fixed linter
Browse files Browse the repository at this point in the history
  • Loading branch information
chgibb committed Nov 24, 2019
1 parent d30670e commit d13b242
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 68 deletions.
2 changes: 1 addition & 1 deletion src/guiTests/6viewAlignCoverage.ts
Expand Up @@ -8,7 +8,7 @@ import {openCircularGenomeBuilderWindow} from "./req/circularGenomeBuilder/openC
import {toggleFiguresOverlay} from "./req/circularGenomeBuilder/toggleFiguresOverlay";
import {createHPV16Figure} from "./req/circularGenomeBuilder/createHPV16Figure";
import {closeToolBar} from "./req/closeToolBar";
import { expandHPV16FigureList } from './req/circularGenomeBuilder/expandHPV16FigureList';
import {expandHPV16FigureList} from "./req/circularGenomeBuilder/expandHPV16FigureList";

async function runTest() : Promise<void>
{
Expand Down
87 changes: 56 additions & 31 deletions src/req/renderer/containers/circularGenome/cachedPlasmid.ts
@@ -1,53 +1,64 @@
import { Plasmid } from '../../../ngplasmid/lib/plasmid';
import { CircularFigure, buildBaseFigureTemplate, assembleCompilableTemplates, assembleCompilableCoverageTrack, makeMapScope } from '../../circularFigure/circularFigure';
import { Node, loadFromString } from '../../../ngplasmid/lib/html';
import {Plasmid} from "../../../ngplasmid/lib/plasmid";
import {CircularFigure, buildBaseFigureTemplate, assembleCompilableTemplates, assembleCompilableCoverageTrack, makeMapScope} from "../../circularFigure/circularFigure";
import {Node, loadFromString} from "../../../ngplasmid/lib/html";

export interface CachedPlasmid {
uuid: string;
plasmid: Plasmid | undefined;
oldStrScope: string;
}

export class PlasmidCacheMgr {
export class PlasmidCacheMgr
{
protected plasmidCache: Array<CachedPlasmid> = [];

public prunePlasmidCache(figure: CircularFigure): void {
public prunePlasmidCache(figure: CircularFigure): void
{

for (let i = this.plasmidCache.length - 1; i >= 0; --i) {
for (let i = this.plasmidCache.length - 1; i >= 0; --i)
{
let found = false;
for (let k = 0; k != figure.visibleLayers.length; ++k) {
if (figure.visibleLayers[k] == this.plasmidCache[i].uuid) {
for (let k = 0; k != figure.visibleLayers.length; ++k)
{
if (figure.visibleLayers[k] == this.plasmidCache[i].uuid)
{
found = true;
}
}

if (!found) {
if (!found)
{
this.plasmidCache.splice(i, 1);
}
}
}


public findPlasmidInCache(target: string, figure: CircularFigure): CachedPlasmid | undefined {
public findPlasmidInCache(target: string): CachedPlasmid | undefined
{
return this.plasmidCache.find((x) => x.uuid == target);

}

public setOldScope(target: string, figure: CircularFigure): void {
public setOldScope(target: string, figure: CircularFigure): void
{

let entry = this.plasmidCache.find((x) => x.uuid == target);

if (entry) {
if (entry)
{
entry.oldStrScope = JSON.stringify(makeMapScope(figure));
}

}

public async loadPlasmidCacheEntry(target: string, figure: CircularFigure): Promise<CachedPlasmid | undefined> {
let scope = { genome: figure };
public async loadPlasmidCacheEntry(target: string, figure: CircularFigure): Promise<CachedPlasmid | undefined>
{
let scope = {genome: figure};

let oldStrScope = JSON.stringify(scope);
if (target == figure.uuid) {
if (target == figure.uuid)
{
console.log("Loading base figure");
console.log(figure.name);

Expand All @@ -60,10 +71,14 @@ export class PlasmidCacheMgr {
)
);

for (let i = 0; i != nodes.length; ++i) {
if (nodes[i].name == "div") {
for (let k = 0; k != nodes[i].children.length; ++k) {
if (nodes[i].children[k].name == "plasmid") {
for (let i = 0; i != nodes.length; ++i)
{
if (nodes[i].name == "div")
{
for (let k = 0; k != nodes[i].children.length; ++k)
{
if (nodes[i].children[k].name == "plasmid")
{
plasmid.fromNode(nodes[i].children[k]);
return {
uuid: figure.uuid,
Expand All @@ -76,10 +91,12 @@ export class PlasmidCacheMgr {
}
}

else {
else
{
let plasmid = await this.maybeLoadPlasmid(target, figure);

if (plasmid) {
if (plasmid)
{
this.plasmidCache = [
...this.plasmidCache,
{
Expand All @@ -89,32 +106,40 @@ export class PlasmidCacheMgr {
}
];

return { uuid: target, plasmid: plasmid, oldStrScope: oldStrScope };
return {uuid: target, plasmid: plasmid, oldStrScope: oldStrScope};
}
return undefined;
}
return undefined;
}

protected async maybeLoadPlasmid(target: string, figure: CircularFigure): Promise<Plasmid | undefined> {
let scope = { genome: figure };
protected async maybeLoadPlasmid(target: string, figure: CircularFigure): Promise<Plasmid | undefined>
{
let scope = {genome: figure};

let coverageTrack = figure.renderedCoverageTracks.find((x) => x.uuid == target);
if (coverageTrack) {
if (coverageTrack)
{
let plasmid: Plasmid = new Plasmid();
plasmid.$scope = scope;

let nodes: Array<Node> = await loadFromString(assembleCompilableCoverageTrack(figure, coverageTrack));

for (let i = 0; i != nodes.length; ++i) {
if (nodes[i].name == "div") {
for (let k = 0; k != nodes[i].children.length; ++k) {
if (nodes[i].children[k].name == "plasmid") {
for (let i = 0; i != nodes.length; ++i)
{
if (nodes[i].name == "div")
{
for (let k = 0; k != nodes[i].children.length; ++k)
{
if (nodes[i].children[k].name == "plasmid")
{
plasmid.fromNode(nodes[i].children[k]);

setTimeout(() => {
setTimeout(() =>
{
let cacheEntry = this.plasmidCache.find((x) => x.uuid == target);
if (cacheEntry) {
if (cacheEntry)
{
cacheEntry.plasmid = undefined;
console.log("Unloaded ", target);
}
Expand Down
101 changes: 65 additions & 36 deletions src/req/renderer/containers/circularGenome/circularGenome.tsx
@@ -1,7 +1,8 @@
import * as React from "react";

import { CircularFigure, renderSVGToCanvas, CoverageTrackLayer, SNPTrackLayer, MapScope } from "../../circularFigure/circularFigure";
import { PlasmidCacheMgr } from './cachedPlasmid';
import {CircularFigure, renderSVGToCanvas, CoverageTrackLayer, SNPTrackLayer, MapScope} from "../../circularFigure/circularFigure";

import {PlasmidCacheMgr} from "./cachedPlasmid";

export interface CircularGenomeState {

Expand All @@ -20,7 +21,8 @@ export class CircularGenome extends React.Component<CircularGenomeProps, Circula
{
private ref = React.createRef<HTMLDivElement>();
private plasmidCache : PlasmidCacheMgr = new PlasmidCacheMgr();
public constructor(props: CircularGenomeProps) {
public constructor(props: CircularGenomeProps)
{
super(props);

this.state = {
Expand All @@ -32,10 +34,13 @@ export class CircularGenome extends React.Component<CircularGenomeProps, Circula
this.componentDidUpdate = this.componentDidUpdate.bind(this);
}

public async updateCanvas() {
for (let i = 0; i != this.props.figure.visibleLayers.length; ++i) {
public async updateCanvas()
{
for (let i = 0; i != this.props.figure.visibleLayers.length; ++i)
{
let layer = this.props.figure.visibleLayers[i];
if (this.ref.current) {
if (this.ref.current)
{
let canvasArr = this.ref.current.getElementsByTagName("canvas");

let canvas = canvasArr[i];
Expand All @@ -46,45 +51,56 @@ export class CircularGenome extends React.Component<CircularGenomeProps, Circula

layerType = this.props.figure.renderedSNPTracks.find((x) => x.uuid == layer);

if (!layerType) {
if (!layerType)
{
layerType = this.props.figure.renderedCoverageTracks.find((x) => x.uuid == layer);
}

let hasScopeChanged = false;
let oldScope: MapScope | undefined;

if (layerType && cachedPlasmid) {
if (layerType && cachedPlasmid)
{
oldScope = JSON.parse(cachedPlasmid.oldStrScope);
if (oldScope && oldScope.genome) {
switch (layerType.type) {
case "coverageTrackLayer":

console.log(`${oldScope.genome!.radius} ${this.props.figure.radius}`);
if (oldScope.genome.radius != this.props.figure.radius) {
hasScopeChanged = true;
break;
}
if (oldScope && oldScope.genome)
{
switch (layerType.type)
{
case "coverageTrackLayer":

console.log(`${oldScope.genome!.radius} ${this.props.figure.radius}`);
if (oldScope.genome.radius != this.props.figure.radius)
{
hasScopeChanged = true;
break;
}
break;
}

if (oldScope.genome.visibleLayers.length != this.props.figure.visibleLayers.length) {
if (oldScope.genome.visibleLayers.length != this.props.figure.visibleLayers.length)
{
console.log("visible layers changed");
hasScopeChanged = true;
}
}


} else {
}
else
{
hasScopeChanged = true;
}

if (hasScopeChanged || !cachedPlasmid) {
if (hasScopeChanged || !cachedPlasmid)
{
cachedPlasmid = await this.plasmidCache.loadPlasmidCacheEntry(layer, this.props.figure);

if (cachedPlasmid && cachedPlasmid.plasmid && canvas) {
if (cachedPlasmid && cachedPlasmid.plasmid && canvas)
{
let ctx: CanvasRenderingContext2D | null = canvas.getContext("2d");

if (ctx) {
if (ctx)
{
canvas.style.position = "absolute";
canvas.setAttribute("width", `${this.props.width}`);
canvas.setAttribute("height", `${this.props.height}`);
Expand All @@ -93,7 +109,7 @@ export class CircularGenome extends React.Component<CircularGenomeProps, Circula

ctx.clearRect(0, 0, this.props.width, this.props.height);

let scope = { genome: this.props.figure };
let scope = {genome: this.props.figure};
cachedPlasmid.plasmid.$scope = scope;

this.plasmidCache.setOldScope(cachedPlasmid.uuid,this.props.figure);
Expand All @@ -107,46 +123,56 @@ export class CircularGenome extends React.Component<CircularGenomeProps, Circula
}
}

public componentDidMount() {
public componentDidMount()
{
this.updateCanvas();
}

public shouldComponentUpdate(prevProps: Readonly<CircularGenomeProps>, prevState: Readonly<CircularGenomeState>): boolean {
public shouldComponentUpdate(prevProps: Readonly<CircularGenomeProps>, prevState: Readonly<CircularGenomeState>): boolean
{
return true;
}

public async componentDidUpdate(prevProps: Readonly<CircularGenomeProps>, prevState: Readonly<CircularGenomeState>) {
public async componentDidUpdate(prevProps: Readonly<CircularGenomeProps>, prevState: Readonly<CircularGenomeState>)
{
let shouldUpdateCanvasDueToResize = false;

this.plasmidCache.prunePlasmidCache(this.props.figure);

if (this.ref.current) {
if (this.ref.current)
{
let canvasArr = this.ref.current.getElementsByTagName("canvas");

while (canvasArr.length < this.props.figure.visibleLayers.length) {
while (canvasArr.length < this.props.figure.visibleLayers.length)
{
this.ref.current.appendChild(document.createElement("canvas"));
}

while (canvasArr.length > this.props.figure.visibleLayers.length) {
if (this.ref.current.lastChild) {
while (canvasArr.length > this.props.figure.visibleLayers.length)
{
if (this.ref.current.lastChild)
{
this.ref.current.lastChild.remove();
}
canvasArr = this.ref.current.getElementsByTagName("canvas");
}

canvasArr = this.ref.current.getElementsByTagName("canvas");

for (let i = 0; i != canvasArr.length; ++i) {
for (let i = 0; i != canvasArr.length; ++i)
{
let canvas = canvasArr[i];

canvas.style.position = "absolute";

if (prevProps.width != this.props.width || canvas.width != this.props.width) {
if (prevProps.width != this.props.width || canvas.width != this.props.width)
{
shouldUpdateCanvasDueToResize = true;
canvas.setAttribute("width", `${this.props.width}`);
}

if (prevProps.height != this.props.height || canvas.height != this.props.height) {
if (prevProps.height != this.props.height || canvas.height != this.props.height)
{
shouldUpdateCanvasDueToResize = true;
canvas.setAttribute("height", `${this.props.height}`);
}
Expand All @@ -157,11 +183,13 @@ export class CircularGenome extends React.Component<CircularGenomeProps, Circula
if (prevProps.y != this.props.y || canvas.style.top != `${this.props.y}px`)
canvas.style.top = `${this.props.y}px`;
}
if (this.props.shouldUpateCanvas) {
if (this.props.shouldUpateCanvas)
{
shouldUpdateCanvasDueToResize = true;
}

if (shouldUpdateCanvasDueToResize || this.props.shouldUpateCanvas) {
if (shouldUpdateCanvasDueToResize || this.props.shouldUpateCanvas)
{
await this.updateCanvas();
}
}
Expand All @@ -170,7 +198,8 @@ export class CircularGenome extends React.Component<CircularGenomeProps, Circula

}

public render(): JSX.Element {
public render(): JSX.Element
{
return (
<React.Fragment>
<div ref={this.ref} key={this.props.figure.uuid}>
Expand Down

0 comments on commit d13b242

Please sign in to comment.