fix: Typescript (Fix throttle issue and update stats logs) (#671)

* Typescript - Fix throttle issue and update stats logs

* Typescript - Fix unit tests
This commit is contained in:
Kiran Niranjan 2019-06-10 22:13:59 +05:30 committed by Vishwas Shashidhar
parent 74a2d7fb37
commit 10d3d49c44
5 changed files with 48 additions and 33 deletions

View File

@ -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);
});
});
});

View File

@ -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);
}
/**

View File

@ -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();

View File

@ -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}`);

View File

@ -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));
}
};
};