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

调整 server.before 触发点以及添加 process.exit 方法 #9

Merged
merged 2 commits into from
Dec 21, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions How-To-Write-A-Dora-Plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ Methods:
- `middleware.after`
- `server.before`
- `server.after`
- `process.exit`

22 changes: 21 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const defaultArgs = {
resolveDir: [defaultCwd],
};

let notDestroy = true;

export default function createServer(_args) {
const args = assign({}, defaultArgs, _args);
log.config(args);
Expand All @@ -37,8 +39,10 @@ export default function createServer(_args) {
}));
_applyPlugins('middleware.after');

_applyPlugins('server.before');
const server = http.createServer(app.callback());
pluginArgs.server = server; // pass server to plugin
_applyPlugins('server.before');

server.listen(port, () => {
// Fix log, #8
const stream = process.stderr;
Expand All @@ -50,4 +54,20 @@ export default function createServer(_args) {
log.info('dora', `listened on ${port}`);
_applyPlugins('server.after');
});


Copy link
Member

Choose a reason for hiding this comment

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

多了个空行?

// exit 事件和 SIGINT 事件是否一定只会发生一次呢? 如果不是, 需要防止多次触发 process.exit
process.on('exit', () => {
if (notDestroy) {
_applyPlugins('process.exit');
}
notDestroy = false;
});

process.on('SIGINT', () => {
if (notDestroy) {
_applyPlugins('process.exit');
}
notDestroy = false;
});
Copy link
Member

Choose a reason for hiding this comment

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

  • process.exit 换成 exit ?
  • process.on 可以改成 process.once,这样不用担心注册多次

}