Skip to content

Commit

Permalink
fix: check template content empty before cached
Browse files Browse the repository at this point in the history
  • Loading branch information
welefen committed Feb 3, 2016
1 parent 4b7b033 commit 2cce1d7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
9 changes: 7 additions & 2 deletions src/adapter/template/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ export default class extends think.adapter.base {
* get template file content
* @return {} []
*/
getContent(file){
let mTime = fs.statSync(file).mtime.getTime();
async getContent(file){
let stat = await think.promisify(fs.stat, fs)(file);
let mTime = stat.mtime.getTime();
let fileCache = thinkCache(thinkCache.VIEW_CONTENT, file);
if(fileCache && fileCache[0] >= mTime){
return fileCache[1];
Expand All @@ -47,6 +48,10 @@ export default class extends think.adapter.base {
let fn = think.promisify(fs.readFile, fs);
return fn(file, 'utf8');
}).then(content => {
//if content is empty, not cached
if(!content){
return content;
}
thinkCache(thinkCache.VIEW_CONTENT, file, [mTime, content]);
return content;
});
Expand Down
7 changes: 4 additions & 3 deletions test/adapter/template/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ describe('adapter/template/base.js', function(){
})
it('run', function(done){
var instance = new baseTemplate();
var data = instance.run(__filename);
assert.equal(data.indexOf("describe('adapter/template/base.js'") > -1, true);
done();
instance.run(__filename).then(function(data){
assert.equal(data.indexOf("describe('adapter/template/base.js'") > -1, true);
done();
});
})
})

0 comments on commit 2cce1d7

Please sign in to comment.