New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: refactor #2
Conversation
Codecov Report
@@ Coverage Diff @@
## master #2 +/- ##
=====================================
+ Coverage 92% 100% +8%
=====================================
Files 5 7 +2
Lines 100 71 -29
=====================================
- Hits 92 71 -21
+ Misses 8 0 -8
Continue to review full report at Codecov.
|
@@ -3,5 +3,6 @@ | |||
module.exports = function* () { | |||
this.body = yield this.renderString('foo {{ name }}', { | |||
name: 'ngot', | |||
viewEngine: 'xtpl', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
第三个参数
lib/xtpl.js
Outdated
|
||
const Xtemplate = require('xtemplate'); | ||
const debug = require('debug')('egg-view-xtpl:xtpl'); | ||
const fs = require('fs-then'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
用 mz 吧
app/extend/application.js
Outdated
'use strict'; | ||
|
||
const XTPL = Symbol('app#XTPL'); | ||
const ENGINE = require('../../lib/engine'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个不是静态变量,不用全大写吧?
const engine = require('../../lib/engine');
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我改成
const XTPLSYMBOL = Symbol('app#xtpl');
const engine = require('../../lib/engine');
吧
app/extend/application.js
Outdated
@@ -0,0 +1,18 @@ | |||
'use strict'; | |||
|
|||
const XTPL = Symbol('app#XTPL'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个用 const ENGINE = Symbol('app#xtpl')
感觉合适点
app/extend/application.js
Outdated
|
||
/** | ||
* xtpl environment | ||
* @member {XTPLEnvironment} Application#xtpl |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
XTPLEnvironment
这个是 xtpl 那边的 class name 拼写么?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个不是,xtpl没有这个概念。我改成自定义的 xtpl 吧
lib/xtpl.js
Outdated
const LRU = require('lru-cache'); | ||
const copy = require('copy-to'); | ||
|
||
const INSTANCECACHESYMBOL = Symbol('XTPL#INSTANCECACHE'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这几个全大写看起来好累.. SYMBOL 后缀没必要加了吧
const INSTANCE_CACHE = Symbol('XtplEngine#instanceCache');
下面几个类似
lib/xtpl.js
Outdated
return cached; | ||
} | ||
|
||
readFile(pathname, config) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perfer pathName
or fileName
lib/xtpl.js
Outdated
} | ||
|
||
return new Promise((resolve, reject) => { | ||
const instance = this.getInstance({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
xtpl 实际业务中会有很多 instance?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
一个request 一个 instance
lib/xtpl.js
Outdated
copy(self.defaultOptions).to(options); | ||
pathname = path.normalize(pathname); | ||
|
||
function load(tpl, callback) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这函数能否变为私有成员函数? 不用每次都 new ?
package.json
Outdated
@@ -19,6 +19,7 @@ | |||
"dependencies": { | |||
"copy-to": "^2.0.1", | |||
"debug": "^2.6.1", | |||
"fs-then": "^0.2.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -0,0 +1 @@ | |||
< ��� {{ name }} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
���
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个是 gbk
yield this.render('home.xtpl', { | ||
name: 'ngot', | ||
}); | ||
this.app.xtpl.fnCache.reset(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
要支持个 cleanCache()
么?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
暂时,貌似还没有什么用。以后支持吧
lib/xtpl.js
Outdated
const cache = config.cache; | ||
const pathname = config.name; | ||
let cached; | ||
if (cache && (cached = self.instanceCache.peek(pathname))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为啥用 self?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
跟着其他几处,顺手写的,我改掉吧
lib/xtpl.js
Outdated
return this[INSTANCECACHESYMBOL]; | ||
} | ||
|
||
get fileCache() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这几个为啥不再构造函数里面
lib/xtpl.js
Outdated
} | ||
|
||
renderString(str, data, options) { | ||
copy(this.defaultOptions).to(options); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
非递归的用 Object.assign 就好了?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里默认值,复制利用 copy比较方便一些
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
和 Object.assign 一个意思吧?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
等价于 Object.assign({}, defaultOptions, options);
lib/xtpl.js
Outdated
copy(this.defaultOptions).to(options); | ||
return new Promise((resolve, reject) => { | ||
const fn = new Xtemplate(str); | ||
fn.render(data, { commands: options.commands }, renderTpl); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个会有异常么
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
会有的,比如语法错误,传入的扩展 commands 抛异常
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
那要 try/catch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
按上面说的,改成 generator 吧
lib/xtpl.js
Outdated
|
||
self.readFile(pathname, rootConfig) | ||
.then(tpl => { | ||
return new Promise((resolve, reject) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
嵌套这么深还是用 generator 吧
lib/xtpl.js
Outdated
const cache = rootConfig.cache; | ||
debug('rootConfig:', rootConfig); | ||
|
||
if (cache && self.fnCache.has(pathname)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
readFile 里有一层缓存了
lib/xtpl.js
Outdated
const INSTANCECACHESYMBOL = Symbol('XTPL#INSTANCECACHE'); | ||
const FNCACHESYMBOL = Symbol('XTPL#FNCACHE'); | ||
const FILECACHESYMBOL = Symbol('XTPL#FILECACHE'); | ||
const INSTANCECACHESYMBOL = Symbol.for('XTPL#INSTANCECACHE'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为啥用 Symbol.for
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
考虑给这个测试用的
this.app.xtpl[FNCACHESYMBOL].reset(); |
fncache和filecache确实有些重复了。我把filecache去掉吧,只缓存编译后的结果。这个测试就不要了。
lib/xtpl.js
Outdated
|
||
class XtplEngine { | ||
constructor() { | ||
this[INSTANCECACHESYMBOL] = new LRU(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
有个疑问,模板为什么要用 lru
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果按照现在这种使用方式,用 map也就够了。不过,lru的话,可以比较方便后续扩展设置缓存超时时间,实际使用上也没什么副作用。比较方便。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
只是疑问为什么需要缓存超时,模板除非是文件变更,不太会变化的。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
目前设置默认配置是不超时的,也没开放设置的
app/extend/application.js
Outdated
@@ -0,0 +1,18 @@ | |||
'use strict'; | |||
|
|||
const XTPLSYMBOL = Symbol('app#xtpl'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
XTPL
就好了吧,不用加 SYMBOL
lib/view.js
Outdated
filename = path.join(this.config.loadpath, filename + '.xtpl'); | ||
} | ||
return engine.render(filename, locals, config); | ||
debug('filename:', filename); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
debug('render filename:'
lib/view.js
Outdated
} | ||
|
||
renderString(str, locals, viewOptions) { | ||
assert(str, 'view string must be supplied!'); | ||
assert(locals, 'locals must be supplied!'); | ||
const config = Object.assign({}, this.config, viewOptions, { cache: null }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ejs 那边加 cache: null
是因为 renderString 无法缓存,不这么做会报错, xtpl 这边也需要?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个是没用的
package.json
Outdated
@@ -17,15 +17,15 @@ | |||
"egg-plugin" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
加一个 egg-view
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
其他 +1。 @popomore 再看看。
PS: 话说, xtpl 怎么设计得每一个 request 一个 instance,浪费啊。
lib/view.js
Outdated
@@ -12,12 +12,12 @@ class XTPLView { | |||
|
|||
render(filename, locals, viewOptions) { | |||
const config = Object.assign({}, this.config, viewOptions, { filename }); | |||
debug('filename:', filename); | |||
debug('render filename::', filename); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
多了一个 :
windows 用例挂了 |
@atian25 xtemplate自己在windows下,挂了...我提个issue吧,不影响插件自己 |
+1 |
@popomore 看看有没有问题,没的话可以先发个 minor 了 |
lib/xtpl.js
Outdated
|
||
const defaultOptions = { | ||
catchError: process.env.NODE_ENV !== 'production', | ||
encoding: 'utf-8', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个不能通过 config.xtpl 配置?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我写的多余了。我删除了。
lib/view.js
Outdated
} | ||
return engine.render(filename, locals, config); | ||
debug('filename:', filename); | ||
return this.app.xtpl.render(filename, locals, config); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个需要用 yield 了吧
lib/view.js
Outdated
} | ||
return engine.render(filename, locals, config); | ||
debug('render filename:', filename); | ||
return this.app.xtpl.render(filename, locals, config); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个要用 yield 吧
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这样可以的。yield的话,这个方法得改成 generator。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个调整为generator了
其他 +1 |
我合并,发个minor |
|
Checklist
npm test
passesAffected core subsystem(s)
Description of change