Skip to content

Commit

Permalink
events, refactor: support onEventTrigger virtual method.
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed Aug 16, 2017
1 parent 4375999 commit 88a8b02
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
24 changes: 13 additions & 11 deletions fibjs/include/Trigger.h
Expand Up @@ -87,28 +87,30 @@ class JSTrigger {
return esa;
}

inline result_t emitNewOrRemoveEvent(v8::Local<v8::Function> func, exlib::string ev, exlib::string type)
inline result_t onEventChange(v8::Local<v8::Function> func, exlib::string ev, exlib::string type)
{
std::vector<v8::Local<v8::Value>> _args;
_args.resize(2);
v8::Local<v8::Value> _args[2];
bool b;

_args[0] = NewFromUtf8(ev);

v8::Local<v8::Value> _func = func->Get(NewFromUtf8("_func"));

if (_func->IsUndefined() || _func->IsNull())
_args[1] = func->Get(NewFromUtf8("_func"));
if (_args[1]->IsUndefined())
_args[1] = func;
else
_args[1] = _func;
func = v8::Local<v8::Function>::Cast(_args[1]);

obj_ptr<object_base> pThis = object_base::getInstance(o);
if (pThis)
pThis->onEventChange(func, ev, type);

return _emit(type, _args.data(), (int32_t)_args.size(), b);
return _emit(type, _args, 2, b);
}

inline int32_t putFunction(v8::Local<v8::Array> esa, v8::Local<v8::Function> func, exlib::string ev)
{
result_t hr;
hr = emitNewOrRemoveEvent(func, ev, "newListener");
hr = onEventChange(func, ev, "newListener");
if (hr < 0)
return hr;

Expand All @@ -120,7 +122,7 @@ class JSTrigger {
inline int32_t prependPutFunction(v8::Local<v8::Array> esa, v8::Local<v8::Function> func, exlib::string ev)
{
result_t hr;
hr = emitNewOrRemoveEvent(func, ev, "newListener");
hr = onEventChange(func, ev, "newListener");
if (hr < 0)
return hr;

Expand Down Expand Up @@ -159,7 +161,7 @@ class JSTrigger {
if (v->Equals(func)) {
spliceOne(esa, i);
result_t hr;
hr = emitNewOrRemoveEvent(func, ev, "removeListener");
hr = onEventChange(func, ev, "removeListener");
if (hr < 0)
return hr;
return 0;
Expand Down
5 changes: 5 additions & 0 deletions fibjs/include/object.h
Expand Up @@ -218,6 +218,11 @@ class object_base : public obj_base {
result_t _emit(exlib::string ev, v8::Local<v8::Value>* args, int32_t argCount, bool& retVal);
result_t _emit(exlib::string ev, Variant* args, int32_t argCount);

virtual result_t onEventChange(v8::Local<v8::Function> func, exlib::string ev, exlib::string type)
{
return 0;
}

void extMemory(int32_t ext)
{
if (handle_.IsEmpty())
Expand Down

0 comments on commit 88a8b02

Please sign in to comment.