Skip to content
Permalink
Browse files

event, feat: wait until all triggers are completed.

  • Loading branch information...
xicilion committed Feb 4, 2019
1 parent e4b8a26 commit 293487bd1f941336ebdb9fa487999ca759a8e88c
Showing with 41 additions and 7 deletions.
  1. +1 −0 fibjs/include/Fiber.h
  2. +5 −2 fibjs/include/Trigger.h
  3. +1 −1 fibjs/include/utils.h
  4. +9 −3 fibjs/src/base/utils.cpp
  5. +2 −1 fibjs/src/coroutine/Fiber.cpp
  6. +23 −0 test/trigger_test.js
@@ -132,6 +132,7 @@ class JSFiber : public Fiber_base,
}

public:
exlib::string m_message;
exlib::Event m_quit;
weak_ptr<Fiber_base> m_caller;

@@ -456,6 +456,7 @@ class JSTrigger {
retVal = false;
QuickArray<obj_ptr<Fiber_base>> evs;
v8::Local<v8::Function> ff;
exlib::string msg;

hr = fireTrigger(GetHiddenList(ev), args, argCount, evs, ff);
if (hr < 0)
@@ -469,13 +470,15 @@ class JSTrigger {
if (evs.size() > 0) {
int32_t i;
METHOD_NAME("EventEmitter.emit");
for (i = 0; i < (int32_t)evs.size(); i++)
for (i = 0; i < (int32_t)evs.size(); i++) {
evs[i]->join();
msg = ((JSFiber*)(Fiber_base*)evs[i])->m_message;
}

retVal = true;
}

return 0;
return !msg.empty() ? CHECK_ERROR(Runtime::setError(msg)) : 0;
}

result_t emit(exlib::string ev, OptArgs args, bool& retVal)
@@ -1184,7 +1184,7 @@ inline result_t SocketError()

exlib::string getResultMessage(result_t hr);
v8::Local<v8::Value> ThrowResult(result_t hr);
void ReportException(TryCatch& try_catch, result_t hr, bool repl = false);
exlib::string ReportException(TryCatch& try_catch, result_t hr, bool repl = false);
exlib::string GetException(TryCatch& try_catch, result_t hr, bool repl = false);
result_t throwSyntaxError(TryCatch& try_catch);

@@ -224,10 +224,16 @@ result_t throwSyntaxError(TryCatch& try_catch)
return CALL_E_JAVASCRIPT;
}

void ReportException(TryCatch& try_catch, result_t hr, bool repl)
exlib::string ReportException(TryCatch& try_catch, result_t hr, bool repl)
{
if (try_catch.HasCaught() || hr < 0)
errorLog(GetException(try_catch, hr, repl));
exlib::string msg;

if (try_catch.HasCaught() || hr < 0) {
msg = GetException(try_catch, hr, repl);
errorLog(msg);
}

return msg;
}

result_t CheckConfig(v8::Local<v8::Object> opts, const char** keys)
@@ -228,9 +228,10 @@ JSFiber::scope::~scope()
{
m_pFiber->holder()->m_isolate->RunMicrotasks();

m_pFiber->m_message = ReportException(try_catch, m_hr);

m_pFiber->m_quit.set();

ReportException(try_catch, m_hr);
m_pFiber->holder()->m_fibers.remove(m_pFiber);
exlib::Fiber::tlsPut(g_tlsCurrent, 0);
}
@@ -174,6 +174,29 @@ function evevt_test(name, e) {
e.off('order');
});

it("on error", () => {
e.on('error', () => {});
e.emit('error');
e.off('error');

e.on('error', () => {
throw new Error('error');
});
assert.throws(() => {
e.emit('error');
});
e.off('error');

e.on('error', () => {});
e.on('error', () => {
throw new Error('error');
});
assert.throws(() => {
e.emit('error');
});
e.off('error');
});

it("listeners(name)", () => {
assert.equal(e.on('test1', t1), e);
assert.equal(e.on('test1', t2), e);

0 comments on commit 293487b

Please sign in to comment.
You can’t perform that action at this time.