diff --git a/src/plugins/kaios-ram/index.tsx b/src/plugins/kaios-ram/index.tsx index 79bcafbae..b03eff3b5 100644 --- a/src/plugins/kaios-ram/index.tsx +++ b/src/plugins/kaios-ram/index.tsx @@ -9,7 +9,7 @@ import React from 'react'; -import {FlipperDevicePlugin, Device, KaiOSDevice} from 'flipper'; +import {FlipperDevicePlugin, Device, KaiOSDevice, sleep} from 'flipper'; import {FlexColumn, Button, Toolbar, Panel} from 'flipper'; @@ -74,33 +74,36 @@ export default class KaiOSGraphs extends FlipperDevicePlugin { monitoring: false, }; - intervalID: NodeJS.Timer | null = null; - static supportsDevice(device: Device) { return device instanceof KaiOSDevice; } + teardown() { + this.onStopMonitor(); + } + onStartMonitor = () => { - if (this.intervalID) { - return; - } - - this.intervalID = setInterval(this.updateFreeMem, 1000); - - this.setState({ - monitoring: true, - }); + this.setState( + { + monitoring: true, + }, + () => { + // no await because monitoring runs in the background + this.monitorInBackground(); + }, + ); }; onStopMonitor = () => { - if (!this.intervalID) { - return; - } else { - clearInterval(this.intervalID); - this.intervalID = null; - this.setState({ - monitoring: false, - }); + this.setState({ + monitoring: false, + }); + }; + + monitorInBackground = async () => { + while (this.state.monitoring) { + await this.updateFreeMem(); + await sleep(1000); } };