Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix memory leak by putting setInterval() in a useeffect #82

Merged
merged 5 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/commonVars.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { IfcPVWSRequest, PVWSRequestType } from "@/app/types";

export const instListPV = "CS:INSTLIST";
export const socketURL =
process.env.NEXT_PUBLIC_WS_URL || "ws://localhost:8080/pvws/pv";

export const instListSubscription: IfcPVWSRequest = {
type: PVWSRequestType.subscribe,
pvs: [instListPV],
};
38 changes: 0 additions & 38 deletions app/components/InstList.ts

This file was deleted.

9 changes: 7 additions & 2 deletions app/components/InstrumentPage.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { ConfigOutput, IfcBlock, IfcPVWSRequest } from "@/app/types";
import {
ConfigOutput,
IfcBlock,
IfcPVWSRequest,
PVWSRequestType,
} from "@/app/types";
import {
getGroupsWithBlocksFromConfigOutput,
RC_ENABLE,
Expand All @@ -14,7 +19,7 @@ test("subscribeToBlockPVs subscribes to all run control PVs", () => {
subscribeToBlockPVs(mockSendJsonMessage, aBlock);
expect(mockSendJsonMessage.mock.calls.length).toBe(1);
const expectedCall: IfcPVWSRequest = {
type: "subscribe",
type: PVWSRequestType.subscribe,
pvs: [aBlock, aBlock + RC_ENABLE, aBlock + RC_INRANGE, aBlock + SP_RBV],
};
expect(JSON.stringify(mockSendJsonMessage.mock.calls[0][0])).toBe(
Expand Down
46 changes: 21 additions & 25 deletions app/components/InstrumentPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,28 @@ import React, { useEffect, useState } from "react";
import TopBar from "./TopBar";
import Groups from "./Groups";
import useWebSocket from "react-use-websocket";
import { dehex_and_decompress } from "./dehex_and_decompress";
import {
dehex_and_decompress,
instListFromBytes,
} from "./dehex_and_decompress";
import { findPVInDashboard, Instrument } from "./Instrument";
import { useSearchParams } from "next/navigation";
import {
ConfigOutput,
ConfigOutputBlock,
IfcBlock,
IfcGroup,
IfcPV,
IfcPVWSMessage,
IfcPVWSRequest,
instList,
PVWSRequestType,
} from "@/app/types";
import {
findPVByAddress,
ExponentialOnThresholdFormat,
findPVByAddress,
} from "@/app/components/PVutils";
import CheckToggle from "@/app/components/CheckToggle";
import { instListPV, instListSubscription, socketURL } from "@/app/commonVars";

let lastUpdate: string = "";

Expand All @@ -46,7 +51,7 @@ export function subscribeToBlockPVs(
* Subscribes to a block and its associated run control PVs
*/
sendJsonMessage({
type: "subscribe",
type: PVWSRequestType.subscribe,
pvs: [
block_address,
block_address + RC_ENABLE,
Expand Down Expand Up @@ -101,15 +106,12 @@ export function toPrecision(

function InstrumentData({ instrumentName }: { instrumentName: string }) {
const [showHiddenBlocks, setShowHiddenBlocks] = useState(false);
const [showSetpoints, setShowSetpoints] = useState(false);
const [showTimestamps, setShowTimestamps] = useState(false);
const CONFIG_DETAILS = "CS:BLOCKSERVER:GET_CURR_CONFIG_DETAILS";
const [instlist, setInstlist] = useState<Array<any> | null>(null);
const [instlist, setInstlist] = useState<instList | null>(null);
const [currentInstrument, setCurrentInstrument] = useState<Instrument | null>(
null,
);
const socketURL =
process.env.NEXT_PUBLIC_WS_URL || "ws://localhost:8080/pvws/pv";

const instName = instrumentName;

useEffect(() => {
Expand All @@ -130,10 +132,7 @@ function InstrumentData({ instrumentName }: { instrumentName: string }) {

useEffect(() => {
// This is an initial useEffect to subscribe to lots of PVs including the instlist.
sendJsonMessage({
type: "subscribe",
pvs: ["CS:INSTLIST"],
});
sendJsonMessage(instListSubscription);

if (instName == "" || instName == null || instlist == null) {
return;
Expand All @@ -142,8 +141,8 @@ function InstrumentData({ instrumentName }: { instrumentName: string }) {
let prefix = "";

for (const item of instlist) {
if (item["name"] == instName.toUpperCase()) {
prefix = item["pvPrefix"];
if (item.name == instName.toUpperCase()) {
prefix = item.pvPrefix;
}
}
if (!prefix) {
Expand All @@ -156,15 +155,18 @@ function InstrumentData({ instrumentName }: { instrumentName: string }) {
setCurrentInstrument(instrument);

sendJsonMessage({
type: "subscribe",
type: PVWSRequestType.subscribe,
pvs: [`${prefix}${CONFIG_DETAILS}`],
});

// subscribe to dashboard and run info PVs
for (const pv of instrument.runInfoPVs.concat(
instrument.dashboard.flat(3),
)) {
sendJsonMessage({ type: "subscribe", pvs: [pv.pvaddress] });
sendJsonMessage({
type: PVWSRequestType.subscribe,
pvs: [pv.pvaddress],
});
}
}
}, [instlist, instName, sendJsonMessage, currentInstrument]);
Expand All @@ -178,11 +180,8 @@ function InstrumentData({ instrumentName }: { instrumentName: string }) {
const updatedPVName: string = updatedPV.pv;
const updatedPVbytes: string | null | undefined = updatedPV.b64byt;

if (updatedPVName == "CS:INSTLIST" && updatedPVbytes != null) {
const dehexedInstList = dehex_and_decompress(atob(updatedPVbytes));
if (dehexedInstList != null && typeof dehexedInstList == "string") {
setInstlist(JSON.parse(dehexedInstList));
}
if (updatedPVName == instListPV && updatedPVbytes != null) {
setInstlist(instListFromBytes(updatedPVbytes));
}

if (!currentInstrument) {
Expand All @@ -200,9 +199,6 @@ function InstrumentData({ instrumentName }: { instrumentName: string }) {
}
lastUpdate = updatedPVbytes;
const res = dehex_and_decompress(atob(updatedPVbytes));
if (res == null || typeof res != "string") {
return;
}
currentInstrument.groups = getGroupsWithBlocksFromConfigOutput(
JSON.parse(res),
sendJsonMessage,
Expand Down
Loading