From 294a400428e088ce645c650dc01cb0c0c83f8fab Mon Sep 17 00:00:00 2001 From: Alexander Putilin Date: Mon, 13 Jan 2020 03:41:22 -0800 Subject: [PATCH] Properly wait 1 second between runs of `adb b2g-info` Summary: This slows down the phone less. In the future, it'd be nice to get timestamp on X axis. Reviewed By: passy Differential Revision: D19097154 fbshipit-source-id: e145f1b911dec368a6bb7de32790d6280c696eda --- src/plugins/kaios-ram/index.tsx | 43 ++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 20 deletions(-) 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); } };