From f4fc96756af75a3de6c4629707b00489a77a2955 Mon Sep 17 00:00:00 2001 From: Ivelin Rachev Date: Mon, 2 Sep 2019 11:40:35 -0700 Subject: [PATCH] Convert Flipper plugin "DeviceCPU" to TypeScript Summary: Migrate CPU plugin to use TypeScript Reviewed By: danielbuechele Differential Revision: D17072709 fbshipit-source-id: d9acf172af08d764216e7b79b79994199dc83f5c --- ...mperatureTable.js => TemperatureTable.tsx} | 3 +- src/plugins/cpu/{index.js => index.tsx} | 89 ++++++++++--------- src/plugins/cpu/package.json | 2 +- types/adbkit-fb.d.tsx | 12 +++ 4 files changed, 64 insertions(+), 42 deletions(-) rename src/plugins/cpu/{TemperatureTable.js => TemperatureTable.tsx} (96%) rename src/plugins/cpu/{index.js => index.tsx} (92%) create mode 100644 types/adbkit-fb.d.tsx diff --git a/src/plugins/cpu/TemperatureTable.js b/src/plugins/cpu/TemperatureTable.tsx similarity index 96% rename from src/plugins/cpu/TemperatureTable.js rename to src/plugins/cpu/TemperatureTable.tsx index 64120f25d..7072f4dbc 100644 --- a/src/plugins/cpu/TemperatureTable.js +++ b/src/plugins/cpu/TemperatureTable.tsx @@ -6,6 +6,7 @@ */ import {Component, Text, SearchableTable} from 'flipper'; +import React from 'react'; const ColumnSizes = { thermal_zone: 'flex', @@ -29,7 +30,7 @@ const Columns = { }; type TemperatureTableProps = { - temperatureMap: any, + temperatureMap: any; }; export default class TemperatureTable extends Component { diff --git a/src/plugins/cpu/index.js b/src/plugins/cpu/index.tsx similarity index 92% rename from src/plugins/cpu/index.js rename to src/plugins/cpu/index.tsx index 58597659c..aad166fca 100644 --- a/src/plugins/cpu/index.js +++ b/src/plugins/cpu/index.tsx @@ -6,8 +6,8 @@ */ import {FlipperDevicePlugin, Device} from 'flipper'; -const adb = require('adbkit-fb'); -import TemperatureTable from './TemperatureTable.js'; +import adb from 'adbkit-fb'; +import TemperatureTable from './TemperatureTable'; import { FlexColumn, @@ -21,37 +21,39 @@ import { DetailSidebar, ToggleButton, } from 'flipper'; +import React from 'react'; type ADBClient = any; type AndroidDevice = { - adb: ADBClient, + adb: ADBClient; }; type TableRows = any; // we keep vairable name with underline for to physical path mappings on device -type CPUFrequency = {| - cpu_id: number, - scaling_cur_freq: number, - scaling_min_freq: number, - scaling_max_freq: number, - scaling_available_freqs: Array, - scaling_governor: string, - scaling_available_governors: Array, - cpuinfo_max_freq: number, - cpuinfo_min_freq: number, -|}; +type CPUFrequency = { + [index: string]: number | Array | string | Array; + cpu_id: number; + scaling_cur_freq: number; + scaling_min_freq: number; + scaling_max_freq: number; + scaling_available_freqs: Array; + scaling_governor: string; + scaling_available_governors: Array; + cpuinfo_max_freq: number; + cpuinfo_min_freq: number; +}; -type CPUState = {| - cpuFreq: Array, - cpuCount: number, - monitoring: boolean, - hardwareInfo: string, - selectedIds: Array, - temperatureMap: any, - thermalAccessible: boolean, - displayThermalInfo: boolean, - displayCPUDetail: boolean, -|}; +type CPUState = { + cpuFreq: Array; + cpuCount: number; + monitoring: boolean; + hardwareInfo: string; + selectedIds: Array; + temperatureMap: any; + thermalAccessible: boolean; + displayThermalInfo: boolean; + displayCPUDetail: boolean; +}; type ShellCallBack = (output: string) => any; @@ -106,13 +108,13 @@ const Heading = styled('div')({ }); // check if str is a number -function isNormalInteger(str) { +function isNormalInteger(str: string) { const n = Math.floor(Number(str)); return String(n) === str && n >= 0; } // format frequency to MHz, GHz -function formatFrequency(freq) { +function formatFrequency(freq: number) { if (freq == -1) { return 'N/A'; } else if (freq == -2) { @@ -124,13 +126,16 @@ function formatFrequency(freq) { } } -export default class CPUFrequencyTable extends FlipperDevicePlugin { +export default class CPUFrequencyTable extends FlipperDevicePlugin< + CPUState, + any, + any +> { adbClient: ADBClient; - intervalID: ?IntervalID; - - state = { - cpuFreq: [], + intervalID: NodeJS.Timer | null = null; + state: CPUState = { cpuCount: 0, + cpuFreq: [], monitoring: false, hardwareInfo: '', selectedIds: [], @@ -145,7 +150,7 @@ export default class CPUFrequencyTable extends FlipperDevicePlugin { } init() { - const device = ((this.device: any): AndroidDevice); + const device = (this.device as any) as AndroidDevice; this.adbClient = device.adb; this.updateHardwareInfo(); @@ -172,15 +177,21 @@ export default class CPUFrequencyTable extends FlipperDevicePlugin { this.setState({ cpuCount: count, cpuFreq: cpuFreq, + monitoring: false, + hardwareInfo: '', + selectedIds: [], + temperatureMap: {}, + thermalAccessible: true, + displayThermalInfo: false, + displayCPUDetail: true, }); }, 'cat /sys/devices/system/cpu/possible'); } - executeShell = (callback: ShellCallBack, command: string) => { return this.adbClient .shell(this.device.serial, command) .then(adb.util.readAll) - .then(function(output) { + .then(function(output: {toString: () => {trim: () => string}}) { return callback(output.toString().trim()); }); }; @@ -189,7 +200,6 @@ export default class CPUFrequencyTable extends FlipperDevicePlugin { this.executeShell((output: string) => { const cpuFreq = this.state.cpuFreq; const newFreq = isNormalInteger(output) ? parseInt(output, 10) : -1; - // update table only if frequency changed if (cpuFreq[core][type] != newFreq) { cpuFreq[core][type] = newFreq; @@ -327,7 +337,7 @@ export default class CPUFrequencyTable extends FlipperDevicePlugin { return; } return this.executeShell((temp: string) => { - if (isNaN(temp)) { + if (Number.isNaN(Number(temp))) { return; } map[type] = { @@ -470,7 +480,7 @@ export default class CPUFrequencyTable extends FlipperDevicePlugin { return ( {freq.scaling_available_freqs.map((freq, idx) => { - const style = {}; + const style: React.CSSProperties = {}; if ( freq == info.scaling_cur_freq || freq == info.scaling_min_freq || @@ -637,7 +647,6 @@ export default class CPUFrequencyTable extends FlipperDevicePlugin { )}   {this.state.hardwareInfo} @@ -663,7 +672,7 @@ export default class CPUFrequencyTable extends FlipperDevicePlugin { rows={this.frequencyRows(this.state.cpuFreq)} onRowHighlighted={selectedIds => { this.setState({ - selectedIds: selectedIds, + selectedIds: selectedIds.map(parseInt), }); }} /> diff --git a/src/plugins/cpu/package.json b/src/plugins/cpu/package.json index 049e5277e..eb0cb8a3c 100644 --- a/src/plugins/cpu/package.json +++ b/src/plugins/cpu/package.json @@ -1,7 +1,7 @@ { "name": "DeviceCPU", "version": "1.0.0", - "main": "index.js", + "main": "index.tsx", "license": "MIT", "dependencies": { "adbkit-fb": "2.10.1" diff --git a/types/adbkit-fb.d.tsx b/types/adbkit-fb.d.tsx new file mode 100644 index 000000000..e7fc98f12 --- /dev/null +++ b/types/adbkit-fb.d.tsx @@ -0,0 +1,12 @@ +/** + * Copyright 2018-present Facebook. + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * @format + */ + +declare module 'adbkit-fb' { + const util: any; + const adbkit: any; + export function createClient({port: number, host: string}): any; +}