Permalink
Browse files

process, feat: support Event exit.

  • Loading branch information...
xicilion committed Oct 16, 2017
1 parent 08cf706 commit dd0c0611df68b320fc188355531daf7d356bd1b2
Showing with 69 additions and 3 deletions.
  1. +12 −2 fibjs/src/process/process.cpp
  2. +8 −1 idl/zh-cn/process.idl
  3. +20 −0 test/process/exec16.js
  4. +18 −0 test/process/exec17.js
  5. +11 −0 test/process_test.js
@@ -12,6 +12,7 @@
#include "ifs/global.h"
#include "ifs/util.h"
#include "Fiber.h"
#include "Trigger.h"
#include "File.h"
#include "BufferedStream.h"
#include "SubProcess.h"
@@ -252,10 +253,19 @@ result_t process_base::get_stderr(obj_ptr<File_base>& retVal)
result_t process_base::exit(int32_t code)
{
Isolate* isolate = Isolate::current();
if (code != 0)
set_exitCode(code);
isolate->m_exitCode = code;
else
get_exitCode(code);
code = isolate->m_exitCode;
JSTrigger t(isolate->m_isolate, class_info().getModule(isolate));
v8::Local<v8::Value> v = v8::Number::New(isolate->m_isolate, code);
bool r;
isolate->m_pendding.inc();
t._emit("exit", &v, 1, r);
flushLog();
View
@@ -15,7 +15,14 @@
```
正常情况下,如果没有额外的工作被添加到任务队列,fibjs 进程会结束。但是如果 `beforeExit` 事件绑定的监听器的回调函数中,启动了一个新的任务,比如开启一个 fiber,那么 fibjs 进程会继续运行。
`process.exitCode` 作为唯一的参数值传递给 `beforeExit` 事件监听器的回调函数。如果进程由于显式的原因而将要终止,例如直接调用 `process.exit` 或抛出未捕获的异常,`beforeExit`事件不会被触发。
process.exitCode 作为唯一的参数值传递给 `beforeExit` 事件监听器的回调函数。如果进程由于显式的原因而将要终止,例如直接调用 process.exit 或抛出未捕获的异常,`beforeExit`事件不会被触发。
### exit
**当 fibjs 退出时,事件 `exit` 会被触发,一旦所有与 `exit` 事件绑定的监听器执行完成,进程会终止**
```JavaScript
process.on('exit', exitCode => {});
```
`exit` 事件监听器的回调函数,只有一个入参,这个参数的值可以是 process.exitCode 的属性值,或者是调用 process.exit 方法时传入的 `exitCode` 值。
*/
module process : EventEmitter
{
View
@@ -0,0 +1,20 @@
process.exitCode = 101;
process.on('beforeExit', exitCode => {
console.log('beforeExit', exitCode);
});
var once = true;
process.on('beforeExit', exitCode => {
console.log('other beforeExit', exitCode);
if (once) {
once = false;
setTimeout(() => {
console.log("new work", exitCode);
}, 10);
}
});
process.exit();
View
@@ -0,0 +1,18 @@
process.exitCode = 101;
process.on('exit', exitCode => {
console.log('exit', exitCode);
});
var once = true;
process.on('exit', exitCode => {
console.log('other exit', exitCode);
if (once) {
once = false;
setTimeout(() => {
console.log("new work", exitCode);
}, 10);
}
});
View
@@ -228,6 +228,17 @@ describe('process', () => {
"beforeExit 101",
"other beforeExit 101"
]);
var bs = process.open(cmd, [path.join(__dirname, 'process', 'exec16.js')]);
assert.deepEqual(bs.readLines(), []);
});
it("exit", () => {
var bs = process.open(cmd, [path.join(__dirname, 'process', 'exec17.js')]);
assert.deepEqual(bs.readLines(), [
"exit 101",
"other exit 101"
]);
});
});
});

0 comments on commit dd0c061

Please sign in to comment.