Skip to content

Commit

Permalink
async import slow loading dependencies to decrease startup time
Browse files Browse the repository at this point in the history
  • Loading branch information
alanhamlett committed May 13, 2018
1 parent 879ccf9 commit 5bbe0fe
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,11 @@
},
"dependencies": {
"adm-zip": "0.4.7",
"ini": "1.3.5",
"request": "2.85.0",
"rimraf": "2.6.2"
},
"devDependencies": {
"@types/node": "^9.4.7",
"ini": "1.3.5",
"request": "2.85.0",
"rimraf": "2.6.2",
"typescript": "^2.7.2",
Expand Down
31 changes: 14 additions & 17 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';

import fs = require('fs');
import os = require('os');
import path = require('path');
import child_process = require('child_process');

var AdmZip = require('adm-zip');
var ini = require('ini');
var request = require('request');
var rimraf = require('rimraf');
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as child_process from 'child_process';

var logger: Logger;
var options: Options;
Expand All @@ -21,7 +16,6 @@ export function activate(ctx: vscode.ExtensionContext) {
options = new Options();
logger = new Logger('info');

// initialize WakaTime
let wakatime = new WakaTime();

ctx.subscriptions.push(
Expand Down Expand Up @@ -54,8 +48,7 @@ export function activate(ctx: vscode.ExtensionContext) {
}),
);

// add to a list of disposables which are disposed when this extension
// is deactivated again.
// dispose WakaTime instance when this extension is deactivated
ctx.subscriptions.push(wakatime);

options.getSetting('settings', 'debug', function(error, debug) {
Expand Down Expand Up @@ -555,8 +548,9 @@ class Dependencies {
});
}

private getLatestCoreVersion(callback: (string) => void): void {
private async getLatestCoreVersion(callback: (string) => void): Promise<void> {
let url = 'https://raw.githubusercontent.com/wakatime/wakatime/master/wakatime/__about__.py';
const request = await import('request');
this.options.getSetting('settings', 'proxy', function(err, proxy) {
let options = { url: url };
if (proxy && proxy.trim()) options['proxy'] = proxy.trim();
Expand Down Expand Up @@ -596,9 +590,10 @@ class Dependencies {
});
}

private removeCore(callback: () => void): void {
private async removeCore(callback: () => void): Promise<void> {
if (fs.existsSync(this.dirname + path.sep + 'wakatime-master')) {
try {
const rimraf = await import('rimraf');
rimraf(this.dirname + path.sep + 'wakatime-master', () => {
if (callback != null) {
return callback();
Expand All @@ -614,7 +609,8 @@ class Dependencies {
}
}

private downloadFile(url: string, outputFile: string, callback: () => void): void {
private async downloadFile(url: string, outputFile: string, callback: () => void): Promise<void> {
const request = await import('request');
this.options.getSetting('settings', 'proxy', function(err, proxy) {
let options = { url: url };
if (proxy && proxy.trim()) options['proxy'] = proxy.trim();
Expand All @@ -631,9 +627,10 @@ class Dependencies {
});
}

private unzip(file: string, outputDir: string, callback: () => void = null) {
private async unzip(file: string, outputDir: string, callback: () => void = null): Promise<void> {
if (fs.existsSync(file)) {
try {
const AdmZip = await import('adm-zip');
let zip = new AdmZip(file);
zip.extractAllTo(outputDir, true);
} catch (e) {
Expand Down Expand Up @@ -863,4 +860,4 @@ class Logger {
public error(msg: string): void {
this.log('error', msg);
}
}
}

0 comments on commit 5bbe0fe

Please sign in to comment.