Skip to content

Commit 726f733

Browse files
authored
Merge pull request #82 from ISISComputingGroup/80_fix_memory_leak
fix memory leak by putting setInterval() in a useeffect
2 parents a2f2886 + 79a304c commit 726f733

16 files changed

+928
-246
lines changed

app/commonVars.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { IfcPVWSRequest, PVWSRequestType } from "@/app/types";
2+
3+
export const instListPV = "CS:INSTLIST";
4+
export const socketURL =
5+
process.env.NEXT_PUBLIC_WS_URL || "ws://localhost:8080/pvws/pv";
6+
7+
export const instListSubscription: IfcPVWSRequest = {
8+
type: PVWSRequestType.subscribe,
9+
pvs: [instListPV],
10+
};

app/components/InstList.ts

Lines changed: 0 additions & 38 deletions
This file was deleted.

app/components/InstrumentPage.test.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { ConfigOutput, IfcBlock, IfcPVWSRequest } from "@/app/types";
1+
import {
2+
ConfigOutput,
3+
IfcBlock,
4+
IfcPVWSRequest,
5+
PVWSRequestType,
6+
} from "@/app/types";
27
import {
38
getGroupsWithBlocksFromConfigOutput,
49
RC_ENABLE,
@@ -14,7 +19,7 @@ test("subscribeToBlockPVs subscribes to all run control PVs", () => {
1419
subscribeToBlockPVs(mockSendJsonMessage, aBlock);
1520
expect(mockSendJsonMessage.mock.calls.length).toBe(1);
1621
const expectedCall: IfcPVWSRequest = {
17-
type: "subscribe",
22+
type: PVWSRequestType.subscribe,
1823
pvs: [aBlock, aBlock + RC_ENABLE, aBlock + RC_INRANGE, aBlock + SP_RBV],
1924
};
2025
expect(JSON.stringify(mockSendJsonMessage.mock.calls[0][0])).toBe(

app/components/InstrumentPage.tsx

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,28 @@ import React, { useEffect, useState } from "react";
33
import TopBar from "./TopBar";
44
import Groups from "./Groups";
55
import useWebSocket from "react-use-websocket";
6-
import { dehex_and_decompress } from "./dehex_and_decompress";
6+
import {
7+
dehex_and_decompress,
8+
instListFromBytes,
9+
} from "./dehex_and_decompress";
710
import { findPVInDashboard, Instrument } from "./Instrument";
811
import { useSearchParams } from "next/navigation";
912
import {
1013
ConfigOutput,
1114
ConfigOutputBlock,
1215
IfcBlock,
1316
IfcGroup,
14-
IfcPV,
1517
IfcPVWSMessage,
1618
IfcPVWSRequest,
19+
instList,
20+
PVWSRequestType,
1721
} from "@/app/types";
1822
import {
19-
findPVByAddress,
2023
ExponentialOnThresholdFormat,
24+
findPVByAddress,
2125
} from "@/app/components/PVutils";
2226
import CheckToggle from "@/app/components/CheckToggle";
27+
import { instListPV, instListSubscription, socketURL } from "@/app/commonVars";
2328

2429
let lastUpdate: string = "";
2530

@@ -46,7 +51,7 @@ export function subscribeToBlockPVs(
4651
* Subscribes to a block and its associated run control PVs
4752
*/
4853
sendJsonMessage({
49-
type: "subscribe",
54+
type: PVWSRequestType.subscribe,
5055
pvs: [
5156
block_address,
5257
block_address + RC_ENABLE,
@@ -101,15 +106,12 @@ export function toPrecision(
101106

102107
function InstrumentData({ instrumentName }: { instrumentName: string }) {
103108
const [showHiddenBlocks, setShowHiddenBlocks] = useState(false);
104-
const [showSetpoints, setShowSetpoints] = useState(false);
105-
const [showTimestamps, setShowTimestamps] = useState(false);
106109
const CONFIG_DETAILS = "CS:BLOCKSERVER:GET_CURR_CONFIG_DETAILS";
107-
const [instlist, setInstlist] = useState<Array<any> | null>(null);
110+
const [instlist, setInstlist] = useState<instList | null>(null);
108111
const [currentInstrument, setCurrentInstrument] = useState<Instrument | null>(
109112
null,
110113
);
111-
const socketURL =
112-
process.env.NEXT_PUBLIC_WS_URL || "ws://localhost:8080/pvws/pv";
114+
113115
const instName = instrumentName;
114116

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

131133
useEffect(() => {
132134
// This is an initial useEffect to subscribe to lots of PVs including the instlist.
133-
sendJsonMessage({
134-
type: "subscribe",
135-
pvs: ["CS:INSTLIST"],
136-
});
135+
sendJsonMessage(instListSubscription);
137136

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

144143
for (const item of instlist) {
145-
if (item["name"] == instName.toUpperCase()) {
146-
prefix = item["pvPrefix"];
144+
if (item.name == instName.toUpperCase()) {
145+
prefix = item.pvPrefix;
147146
}
148147
}
149148
if (!prefix) {
@@ -156,15 +155,18 @@ function InstrumentData({ instrumentName }: { instrumentName: string }) {
156155
setCurrentInstrument(instrument);
157156

158157
sendJsonMessage({
159-
type: "subscribe",
158+
type: PVWSRequestType.subscribe,
160159
pvs: [`${prefix}${CONFIG_DETAILS}`],
161160
});
162161

163162
// subscribe to dashboard and run info PVs
164163
for (const pv of instrument.runInfoPVs.concat(
165164
instrument.dashboard.flat(3),
166165
)) {
167-
sendJsonMessage({ type: "subscribe", pvs: [pv.pvaddress] });
166+
sendJsonMessage({
167+
type: PVWSRequestType.subscribe,
168+
pvs: [pv.pvaddress],
169+
});
168170
}
169171
}
170172
}, [instlist, instName, sendJsonMessage, currentInstrument]);
@@ -178,11 +180,8 @@ function InstrumentData({ instrumentName }: { instrumentName: string }) {
178180
const updatedPVName: string = updatedPV.pv;
179181
const updatedPVbytes: string | null | undefined = updatedPV.b64byt;
180182

181-
if (updatedPVName == "CS:INSTLIST" && updatedPVbytes != null) {
182-
const dehexedInstList = dehex_and_decompress(atob(updatedPVbytes));
183-
if (dehexedInstList != null && typeof dehexedInstList == "string") {
184-
setInstlist(JSON.parse(dehexedInstList));
185-
}
183+
if (updatedPVName == instListPV && updatedPVbytes != null) {
184+
setInstlist(instListFromBytes(updatedPVbytes));
186185
}
187186

188187
if (!currentInstrument) {
@@ -200,9 +199,6 @@ function InstrumentData({ instrumentName }: { instrumentName: string }) {
200199
}
201200
lastUpdate = updatedPVbytes;
202201
const res = dehex_and_decompress(atob(updatedPVbytes));
203-
if (res == null || typeof res != "string") {
204-
return;
205-
}
206202
currentInstrument.groups = getGroupsWithBlocksFromConfigOutput(
207203
JSON.parse(res),
208204
sendJsonMessage,

0 commit comments

Comments
 (0)