Skip to content
This repository has been archived by the owner on Dec 1, 2019. It is now read-only.

Commit

Permalink
feat(*): debounce running checker
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita Korotkih committed Jan 24, 2016
1 parent fa71235 commit 938fc96
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 6 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"colors": "^1.1.2",
"enhanced-resolve": "^0.9.1",
"loader-utils": "^0.2.6",
"lodash": "^3.10.0",
"lodash": "^3.10.1",
"object-assign": "^2.1.1",
"parse-json": "^2.2.0",
"strip-bom": "^2.0.0",
Expand All @@ -59,8 +59,10 @@
"load-grunt-tasks": "^0.6.0",
"mkdirp": "^0.5.1",
"mocha": "^2.3.4",
"ps-node": "0.0.5",
"rimraf": "^2.5.0",
"tslint": "^3.2.2-dev.1",
"typescript": "^1.8.0-dev.20151202"
"typescript": "^1.8.0-dev.20151202",
"webpack-dev-server": "^1.14.1"
}
}
14 changes: 10 additions & 4 deletions src/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,15 @@ function setupWatchRun(compiler, instanceName: string) {
});
}

let runChecker = (instance, payload) => {
instance.checker.send({
messageType: 'compile',
payload
});
};

runChecker = _.debounce(runChecker, 200);

function setupAfterCompile(compiler, instanceName, forkChecker = false) {
compiler.plugin('after-compile', function(compilation, callback) {
let instance: ICompilerInstance = resolveInstance(compilation.compiler, instanceName);
Expand All @@ -269,10 +278,7 @@ function setupAfterCompile(compiler, instanceName, forkChecker = false) {
resolutionCache: state.host.moduleResolutionHost.resolutionCache
};

instance.checker.send({
messageType: 'compile',
payload
});
runChecker(instance, payload);
} else {
if (!state.program) {
// program may be undefined here, if all files
Expand Down
102 changes: 102 additions & 0 deletions test/checker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
var webpack = require('webpack');
var Promise = require('bluebird');
var ps = Promise.promisifyAll(require('ps-node'));
var fs = Promise.promisifyAll(require('fs'));
var expect = require('expect');

var path = require('path');
var loader = path.resolve(__dirname, '../dist.babel');
var ForkCheckerPlugin = require(loader).ForkCheckerPlugin;
var globalConfig = {
watch: true,
module: {
loaders: [
{
test: /\.ts?/,
loader: loader + '?doTypeCheck&+forkChecker',
},
],
},
plugins: [
new ForkCheckerPlugin(),
],
};

var outputDir = path.resolve(__dirname, './output/');

describe('checker test', function() {
var toCheckFilename = './test/fixtures/to-check.ts';

this.timeout(5000);

var outputFile = path.resolve(outputDir, toCheckFilename);
var config = {
output: {
path: outputDir,
filename: toCheckFilename,
},
entry: toCheckFilename,
};
var getCheckerRuntimeProcess = () => {
return ps.lookupAsync({
command: new RegExp('node'),
arguments: new RegExp('checker-runtime'),
psargs: 'aux'
}).then(res => {
return res[0];
});
};

var utilizeProcess = (p) => {
expect(p).toExist();

return ps.killAsync(p.pid);
};

it('should fork checker in separate process', function(done) {
// assert that checker starts on startup
new Promise((resolve, reject) => {
webpack(Object.assign(globalConfig, config), function(err, stat) {
resolve();
});
})
.then(getCheckerRuntimeProcess)
.then(p => {
expect(p).toExist();
return utilizeProcess(p);
})
.then(() => {
done();
})
});

it('should fork only one checker after multiple changes', function(done) {
// I didn't get how to test it more precise, so it's more like a proof of work
var fileContent = fs.readFileSync(toCheckFilename);
var updateFile = () => fs.writeFileAsync(toCheckFilename, Date.now());
var timeoutAsync = (ms) => {
return new Promise((resolve, reject) => {
setTimeout(resolve, ms);
});
};

new Promise((resolve, reject) => {
webpack(Object.assign(globalConfig, config), function(err, stat) {
resolve();
});
})
.then(getCheckerRuntimeProcess)
.then(p => {
expect(p).toExist();
var times = [];
var i = 10;
while(i--) { times.push(i); }

return Promise.each(times, () => {
return updateFile().then(() => timeoutAsync(50));
}).then(() => fs.writeFileAsync(toCheckFilename, fileContent));
})
.then(() => timeoutAsync(2000))
.then(() => { done(); });
});
});
1 change: 1 addition & 0 deletions test/fixtures/to-check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1453065049802

0 comments on commit 938fc96

Please sign in to comment.