From 10d3d49c4409f9bca92fa9062b47d4bf1d8542c5 Mon Sep 17 00:00:00 2001 From: Kiran Niranjan Date: Mon, 10 Jun 2019 22:13:59 +0530 Subject: [PATCH] fix: Typescript (Fix throttle issue and update stats logs) (#671) * Typescript - Fix throttle issue and update stats logs * Typescript - Fix unit tests --- spec/utils.spec.ts | 27 +++++++++++++++++++++++---- src/app/config-handler.ts | 4 ++-- src/app/init.ts | 10 ++++++---- src/app/stats.ts | 22 ++++------------------ src/common/utils.ts | 18 +++++++++++++----- 5 files changed, 48 insertions(+), 33 deletions(-) diff --git a/spec/utils.spec.ts b/spec/utils.spec.ts index 654765de..89fc2713 100644 --- a/spec/utils.spec.ts +++ b/spec/utils.spec.ts @@ -110,8 +110,20 @@ describe('utils', () => { }); describe('`throttle`', () => { + let origNow; + let now; beforeEach(() => { - jest.useFakeTimers(); + origNow = Date.now; + // mock date func + Date.now = () => { + return now; + }; + now = 10000; + }); + + afterEach(() => { + // restore original + Date.now = origNow; }); it('should fail when wait is invalid', () => { @@ -124,10 +136,17 @@ describe('utils', () => { }); it('should call `throttle` correctly', () => { - const validTime = 3; - const tempFn = throttle(jest.fn(), validTime); + jest.useFakeTimers(); + const validTime = 1000; + const functionMock = jest.fn(); + const tempFn = throttle(functionMock, validTime); + for (let i = 0; i < 3; i++) { + tempFn(); + } + now += 1000; + jest.runTimersToTime(1000); tempFn(); - expect(setTimeout).toBeCalledWith(expect.any(Function), validTime); + expect(functionMock).toBeCalledTimes(2); }); }); }); diff --git a/src/app/config-handler.ts b/src/app/config-handler.ts index 59d06b42..2746f1a9 100644 --- a/src/app/config-handler.ts +++ b/src/app/config-handler.ts @@ -206,7 +206,7 @@ class Config { await this.updateUserConfig({ configVersion: app.getVersion().toString(), buildNumber } as IConfig); } this.userConfig = this.parseConfigData(fs.readFileSync(this.userConfigPath, 'utf8')); - logger.info(`config-handler: user config exists with data ${JSON.stringify(this.userConfig)}`); + logger.info(`config-handler: User configuration: `, this.userConfig); } /** @@ -214,7 +214,7 @@ class Config { */ private readGlobalConfig() { this.globalConfig = this.parseConfigData(fs.readFileSync(this.globalConfigPath, 'utf8')); - logger.info(`config-handler: global config exists with data ${JSON.stringify(this.globalConfig)}`); + logger.info(`config-handler: Global configuration: `, this.globalConfig); } /** diff --git a/src/app/init.ts b/src/app/init.ts index 157c73f9..a1b64ac6 100644 --- a/src/app/init.ts +++ b/src/app/init.ts @@ -13,16 +13,18 @@ const userDataPath = userDataPathArg && userDataPathArg.substring(userDataPathAr // Set user data path before app ready event if (isDevEnv) { - const appDataPath = app.getPath('appData'); - logger.info(`init: Setting app data path to ${appDataPath}`); - app.setPath('userData', path.join(appDataPath, 'Symphony-dev')); + const devDataPath = path.join(app.getPath('appData'), 'Symphony-dev'); + logger.info(`init: Setting user data path to`, devDataPath); + app.setPath('userData', devDataPath); } if (userDataPath) { - logger.info(`init: Setting user data path to ${userDataPath}`); + logger.info(`init: Setting user data path to`, userDataPath); app.setPath('userData', userDataPath); } +logger.info(`init: Fetch user data path`, app.getPath('userData')); + // Log app statistics appStats.logStats(); diff --git a/src/app/stats.ts b/src/app/stats.ts index 577a2b95..b3739522 100644 --- a/src/app/stats.ts +++ b/src/app/stats.ts @@ -1,7 +1,6 @@ import { app } from 'electron'; import * as os from 'os'; import { logger } from '../common/logger'; -import { config } from './config-handler'; export class AppStats { @@ -23,7 +22,7 @@ export class AppStats { * Logs system related statistics */ private logSystemStats() { - logger.info(`stats: -----------------Gathering system information-----------------`); + logger.info(`-----------------Gathering system information-----------------`); logger.info( `Network Info -> `, os.networkInterfaces()); logger.info( `CPU Info -> `, os.cpus()); logger.info( `Operating System -> `, os.type()); @@ -51,29 +50,15 @@ export class AppStats { * Logs Configuration Data */ private logConfigurationData() { - const configItems = [ - 'url', 'minimizeOnClose', 'launchOnStartup', 'alwaysOnTop', 'bringToFront', 'whitelistUrl', - 'isCustomTitleBar', 'memoryRefresh', 'devToolsEnabled', 'ctWhitelist', 'configVersion', - 'buildNumber', 'autoLaunchPath', 'notificationSettings', 'permissions', 'customFlags', - 'crashReporter', 'mainWinPos', - ]; - - logger.info(`stats: -----------------App Configuration Information-----------------`); + logger.info(`-----------------App Configuration Information-----------------`); logger.info(`stats: Is app packaged? ${app.isPackaged}`); - - const globalConfiguration = config.getGlobalConfigFields(configItems); - logger.info(`stats: Global configuration: `, globalConfiguration); - - const userConfiguration = config.getUserConfigFields(configItems); - logger.info(`stats: -----------------Gathering User Configuration Information-----------------`); - logger.info(`stats: User configuration: `, userConfiguration); } /** * Logs App metrics */ private logAppMetrics() { - logger.info(`stats: -----------------Gathering App Metrics-----------------`); + logger.info(`-----------------Gathering App Metrics-----------------`); const metrics = app.getAppMetrics(); metrics.forEach((metric) => { logger.info(`stats: PID -> ${metric.pid}, Type -> ${metric.type}, CPU Usage -> `, metric.cpu); @@ -102,6 +87,7 @@ export class AppStats { * Logs process info */ private logProcessInfo() { + logger.info(`-----------------Gathering Process Info-----------------`); logger.info(`stats: Is default app? ${process.defaultApp}`); logger.info(`stats: Is Mac Store app? ${process.mas}`); logger.info(`stats: Is Windows Store app? ${process.windowsStore}`); diff --git a/src/common/utils.ts b/src/common/utils.ts index 8d3b1803..105a373d 100644 --- a/src/common/utils.ts +++ b/src/common/utils.ts @@ -165,13 +165,21 @@ export const throttle = (func: (...args) => void, wait: number): (...args) => vo throw Error('throttle: invalid throttleTime arg, must be a number: ' + wait); } - let isCalled: boolean = false; + let timer: NodeJS.Timer; + let lastRan = 0; return (...args) => { - if (!isCalled) { - func(...args); - isCalled = true; - setTimeout(() => isCalled = false, wait); + if (!lastRan) { + func.apply(null, args); + lastRan = Date.now(); + } else { + clearTimeout(timer); + timer = setTimeout(() => { + if ((Date.now() - lastRan) >= wait) { + func.apply(null, args); + lastRan = Date.now(); + } + }, wait - (Date.now() - lastRan)); } }; };