Skip to content
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

Merged
merged 10 commits into from Mar 14, 2017
Merged

feat: refactor #2

merged 10 commits into from Mar 14, 2017

Conversation

ngot
Copy link
Member

@ngot ngot commented Mar 13, 2017

Checklist
  • npm test passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)
Description of change

@codecov
Copy link

codecov bot commented Mar 13, 2017

Codecov Report

Merging #2 into master will increase coverage by 8%.
The diff coverage is 100%.

@@          Coverage Diff          @@
##           master     #2   +/-   ##
=====================================
+ Coverage      92%   100%   +8%     
=====================================
  Files           5      7    +2     
  Lines         100     71   -29     
=====================================
- Hits           92     71   -21     
+ Misses          8      0    -8
Impacted Files Coverage Δ
lib/view.js 100% <100%> (+5.26%)
lib/xtpl.js 100% <100%> (ø)
app/extend/application.js 100% <100%> (ø)
config/config.default.js 100% <100%> (ø)
lib/engine.js 100% <100%> (+9.58%)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e5dfd62...23bcaa8. Read the comment docs.

@ngot
Copy link
Member Author

ngot commented Mar 13, 2017

重构了一版

@popomore @atian25 看下
loadpath 本来的版本就没有效果的,根本走不到那个逻辑。

@@ -3,5 +3,6 @@
module.exports = function* () {
this.body = yield this.renderString('foo {{ name }}', {
name: 'ngot',
viewEngine: 'xtpl',
Copy link
Member

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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

用 mz 吧

'use strict';

const XTPL = Symbol('app#XTPL');
const ENGINE = require('../../lib/engine');
Copy link
Member

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');

Copy link
Member Author

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');

@@ -0,0 +1,18 @@
'use strict';

const XTPL = Symbol('app#XTPL');
Copy link
Member

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') 感觉合适点


/**
* xtpl environment
* @member {XTPLEnvironment} Application#xtpl
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

XTPLEnvironment 这个是 xtpl 那边的 class name 拼写么?

Copy link
Member Author

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');
Copy link
Member

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) {
Copy link
Member

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({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

xtpl 实际业务中会有很多 instance?

Copy link
Member Author

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) {
Copy link
Member

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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -0,0 +1 @@
< ��� {{ name }}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

���

Copy link
Member Author

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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

要支持个 cleanCache() 么?

Copy link
Member Author

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))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为啥用 self?

Copy link
Member Author

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() {
Copy link
Member

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

非递归的用 Object.assign 就好了?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里默认值,复制利用 copy比较方便一些

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

和 Object.assign 一个意思吧?

Copy link
Member Author

@ngot ngot Mar 14, 2017

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个会有异常么

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

会有的,比如语法错误,传入的扩展 commands 抛异常

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那要 try/catch

Copy link
Member

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) => {
Copy link
Member

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)) {
Copy link
Member

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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为啥用 Symbol.for

Copy link
Member Author

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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有个疑问,模板为什么要用 lru

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果按照现在这种使用方式,用 map也就够了。不过,lru的话,可以比较方便后续扩展设置缓存超时时间,实际使用上也没什么副作用。比较方便。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

只是疑问为什么需要缓存超时,模板除非是文件变更,不太会变化的。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

目前设置默认配置是不超时的,也没开放设置的

@ngot
Copy link
Member Author

ngot commented Mar 14, 2017

改了,大家再看看 @atian25 @popomore

@@ -0,0 +1,18 @@
'use strict';

const XTPLSYMBOL = Symbol('app#xtpl');
Copy link
Member

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);
Copy link
Member

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 });
Copy link
Member

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 这边也需要?

Copy link
Member Author

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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加一个 egg-view

Copy link
Member

@atian25 atian25 left a 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

多了一个 :

@atian25
Copy link
Member

atian25 commented Mar 14, 2017

windows 用例挂了

@ngot
Copy link
Member Author

ngot commented Mar 14, 2017

@atian25 xtemplate自己在windows下,挂了...我提个issue吧,不影响插件自己

@atian25
Copy link
Member

atian25 commented Mar 14, 2017

+1

@ngot
Copy link
Member Author

ngot commented Mar 14, 2017

xtemplate/xtemplate#94

@atian25
Copy link
Member

atian25 commented Mar 14, 2017

@popomore 看看有没有问题,没的话可以先发个 minor 了

lib/xtpl.js Outdated

const defaultOptions = {
catchError: process.env.NODE_ENV !== 'production',
encoding: 'utf-8',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个不能通过 config.xtpl 配置?

Copy link
Member Author

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);
Copy link
Member

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个要用 yield 吧

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这样可以的。yield的话,这个方法得改成 generator。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个调整为generator了

@popomore
Copy link
Member

其他 +1

@ngot
Copy link
Member Author

ngot commented Mar 14, 2017

我合并,发个minor

@ngot ngot merged commit e428f29 into master Mar 14, 2017
@ngot ngot deleted the refactor branch March 14, 2017 08:47
@ngot
Copy link
Member Author

ngot commented Mar 14, 2017

  • egg-view-xtpl@1.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants