Skip to content

Commit 31090f0

Browse files
committed
Bug fixes and re-factoring: now service host has new methods to tell if files or typings have been changed since last interaction
New unit test Urigo/angular2-meteor#102
1 parent c4b62ae commit 31090f0

File tree

6 files changed

+73
-45
lines changed

6 files changed

+73
-45
lines changed

cache.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ CCp.get = function(filePath, options, compileFn, force) {
155155
return compileResult;
156156
};
157157

158-
Cp.resultChanged = function(filePath, options) {
158+
CCp.resultChanged = function(filePath, options) {
159159
var source = sourceHost.get(filePath);
160160
var cacheKey = utils.deepHash(pkgVersion, source, options);
161161
var compileResult = this._cache.get(cacheKey);
@@ -181,19 +181,16 @@ exports.FileCache = FileCache;
181181
var FCp = FileCache.prototype = new Cache();
182182

183183
FCp.save = function(filePath, content) {
184-
var content = content === undefined ?
184+
content = content === undefined ?
185185
sourceHost.get(filePath) : content;
186-
187186
var cacheKey = utils.deepHash(filePath);
188187
var contentHash = utils.deepHash(content);
189188
this._save(cacheKey, contentHash);
190189
};
191190

192191
FCp.isChanged = function(filePath, content) {
193-
var content = content === undefined ?
194-
sourceHost.get(filePath) : content;
195-
if (! content) return true;
196-
192+
content = content === undefined ?
193+
sourceHost.get(filePath) : content;
197194
var cacheKey = utils.deepHash(filePath);
198195
var contentHash = utils.deepHash(content);
199196
return this._get(cacheKey) != contentHash;

compile-service-host.js

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ var _ = require("underscore");
66
var sourceHost = require("./files-source-host").sourceHost;
77
var tsu = require("./ts-utils").ts;
88

9-
function CompileServiceHost(compileCache, typingsCache) {
10-
this.compileCache = compileCache;
11-
this.typingsCache = typingsCache;
12-
9+
function CompileServiceHost(fileCache) {
1310
this.files = {};
11+
this.fileCache = fileCache;
12+
this.typingsChanged = false;
13+
this.appId = ts.sys.getCurrentDirectory();
1414
this.webArchExp = new RegExp("^web\.");
1515
}
1616

@@ -20,40 +20,56 @@ var SH = CompileServiceHost.prototype;
2020

2121
SH.setFiles = function(filePaths, options) {
2222
this.options = options;
23+
var typingsChanged = false;
24+
var typings = {};
2325

2426
_.each(filePaths, function(filePath) {
25-
if (! this.files[filePath]) {
26-
this.files[filePath] = { version: 1 };
27-
return;
28-
}
27+
var isTypings = tsu.isTypings(filePath);
28+
if (isTypings) typings[filePath] = true;
2929

30-
if (tsu.isTypings(filePath)) {
31-
if (this.typingsCache.isChanged(filePath)) {
32-
this.files[filePath].version++;
33-
}
34-
return;
30+
if (! this.files[filePath]) {
31+
this.files[filePath] = { version: 0 };
3532
}
3633

37-
if (this.compileCache.resultChanged(filePath, options)) {
34+
if (this.fileCache.isChanged(filePath)) {
3835
this.files[filePath].version++;
36+
this.files[filePath].changed = true;
37+
typingsChanged = typingsChanged || isTypings;
38+
this.fileCache.save(filePath);
3939
}
4040
}, this);
41+
42+
this.typingsChanged = typingsChanged ||
43+
this.fileCache.isChanged(this.appId, typings);
44+
this.fileCache.save(this.appId, typings);
45+
};
46+
47+
SH.isFileChanged = function(filePath) {
48+
return this.files[filePath].changed;
49+
};
50+
51+
SH.isTypingsChanged = function() {
52+
return this.typingsChanged;
4153
};
4254

4355
SH.getScriptFileNames = function() {
44-
var rootFilePaths = [];
56+
var rootFilePaths = {};
4557
for (var filePath in this.files) {
46-
rootFilePaths.push(filePath);
58+
rootFilePaths[filePath] = true;
4759
}
60+
61+
// Add in options.typings, which is used
62+
// to set up typings that should be read from disk.
4863
var typings = this.options.typings;
4964
if (typings) {
5065
_.each(typings, function(filePath) {
51-
if (! this.files[filePath]) {
52-
rootFilePaths.push(filePath);
66+
if (! rootFilePaths[filePath]) {
67+
rootFilePaths[filePath] = true;
5368
}
54-
}, this);
69+
});
5570
}
56-
return rootFilePaths;
71+
72+
return _.keys(rootFilePaths);
5773
};
5874

5975
SH.getScriptVersion = function(filePath) {

index.js

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ var utils = require("./utils");
1313
var _ = require("underscore");
1414

1515
var compileCache;
16-
var typingsCache;
16+
var fileCache;
1717
function setCacheDir(cacheDir) {
1818
if (compileCache && compileCache.cacheDir === cacheDir) {
1919
return;
2020
}
2121

2222
compileCache = new CompileCache(cacheDir);
23-
typingsCache = new FileCache(cacheDir);
23+
fileCache = new FileCache(cacheDir);
2424
};
2525

2626
exports.setCacheDir = setCacheDir;
@@ -44,7 +44,7 @@ function lazyInit() {
4444
}
4545

4646
if (! serviceHost) {
47-
serviceHost = new ServiceHost(compileCache, typingsCache);
47+
serviceHost = new ServiceHost(fileCache);
4848
}
4949

5050
if (! compileService) {
@@ -72,28 +72,22 @@ function TSBuild(filePaths, getFileContent, options) {
7272
this.rebuildMap = getRebuildMap(filePaths, resOptions);
7373
}
7474

75-
function rebuildWithNewTypings(filePath, typings) {
76-
typings = typings || [];
77-
var rebuild = typingsCache.isChanged(filePath, typings);
78-
typingsCache.save(filePath, typings);
75+
function rebuildWithNewTypings(typings) {
76+
if (! typings) return false;
7977

8078
var tLen = typings.length;
8179
for (var i = 0; i < tLen; i++) {
8280
var path = typings[i];
83-
if (typingsCache.isChanged(path)) {
84-
typingsCache.save(path);
85-
rebuild = true;
86-
}
81+
if (serviceHost.isFileChanged(path)) return true;
8782
}
8883

89-
return rebuild;
84+
return false;
9085
}
9186

9287
function getRebuildMap(filePaths, options) {
9388
var files = {};
9489

95-
var appPath = ts.sys.getCurrentDirectory();
96-
if (rebuildWithNewTypings(appPath, options.typings)) {
90+
if (serviceHost.isTypingsChanged()) {
9791
_.each(filePaths, function(filePath) {
9892
files[filePath] = true;
9993
});
@@ -105,7 +99,7 @@ function getRebuildMap(filePaths, options) {
10599
var result = compileCache.get(filePath, options);
106100
var refs = result.references;
107101
if (refs) {
108-
files[filePath] = rebuildWithNewTypings(filePath, refs.typings);
102+
files[filePath] = rebuildWithNewTypings(refs.typings);
109103
if (files[filePath]) return;
110104

111105
var modules = refs.modules;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "meteor-typescript",
33
"author": "@barbatus",
4-
"version": "0.6.0-beta.2",
4+
"version": "0.6.0-beta.3",
55
"license": "MIT",
66
"description": "TypeScript wrapper package for use with Meteor",
77
"keywords": [

tests/ts.spec.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ describe("meteor-typescript -> ", function() {
249249

250250
it("should update diagnostics when file's references has changed", function() {
251251
var foo9 = "module foo9 { export var foo = 'foo' }";
252-
var foo10 = "/// <reference path='foo9.ts'> \n" +
252+
var foo10 = "/// <reference path='foo9.ts' /> \n " +
253253
"module foo10 { export var foo = foo9.foo }";
254254

255255
var build1 = new TSBuild(["foo9.ts", "foo10.ts"], function(filePath) {
@@ -270,6 +270,7 @@ describe("meteor-typescript -> ", function() {
270270
var result2 = build2.emit("foo10.ts");
271271

272272
expect(result2.diagnostics.semanticErrors.length).toEqual(1);
273+
expect(result2.diagnostics.semanticErrors[0].message).toContain("'foo' does not exist");
273274
});
274275

275276
it("should handle ambient typings properly", function() {
@@ -285,5 +286,25 @@ describe("meteor-typescript -> ", function() {
285286

286287
expect(result1.diagnostics.semanticErrors.length).toEqual(0);
287288
});
289+
290+
it("should take result from cache for once compiled code", function() {
291+
var build1 = new TSBuild(["foo13.ts"], function(filePath) {
292+
if (filePath === "foo13.ts") return testCodeLine;
293+
});
294+
var result1 = build1.emit("foo13.ts");
295+
296+
var changedCode = "export const foo1 = 'foo'";
297+
var build2 = new TSBuild(["foo13.ts"], function(filePath) {
298+
if (filePath === "foo13.ts") return changedCode;
299+
});
300+
var result2 = build2.emit("foo13.ts");
301+
302+
var build3 = new TSBuild(["foo13.ts"], function(filePath) {
303+
if (filePath === "foo13.ts") return testCodeLine;
304+
});
305+
var result3 = build3.emit("foo13.ts");
306+
307+
expect(result3).toEqual(result1);
308+
});
288309
});
289310
});

ts-utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function isSourceMap(fileName) {
8585
}
8686

8787
function isTypings(fileName) {
88-
return /\.d\.ts$/.test(fileName);
88+
return ts.fileExtensionIs(fileName, '.d.ts');
8989
}
9090

9191
exports.ts = {

0 commit comments

Comments
 (0)