//Event and optional argument we'll be operating on, default to empty string to avoid NPEs
String event = (args != null && args.length > 0) ? (String) request.getArgumentByName( "event" ) : "";
String eventArg = (args != null && args.length > 1) ? (String) request.getArgumentByName( "arg" ) : "";
SystemEventReturnValue returnValue = null;
//Dispatch the function call or complain that we don't recognize it
try {
if(FUNCTION_REGISTER.equals(op)) {
_eventManager.listenFor(event, eventArg);
returnValue = SystemEventReturnValue.getSuccessForOp(FUNCTION_REGISTER, event);
} else if(FUNCTION_UNREGISTER.equals(op)) {
_eventManager.stopListeningFor(event, eventArg);
returnValue = SystemEventReturnValue.getSuccessForOp(FUNCTION_UNREGISTER, event);
} else if(FUNCTION_POLL.equals(op)) {
returnValue = _eventManager.getNextWaitingEvent();
} else {
returnValue = SystemEventReturnValue.INVALID_METHOD;
}
} catch (RuntimeException e) {
returnValue = SystemEventReturnValue.getErrorForOp(op, event);
}
response.setPostData( returnValue.getJSExtensionReturnValue().getReturnValue().toString().getBytes() );
}