/* function on(name, observer: function): Http */ static EjsHttp *http_on(Ejs *ejs, EjsHttp *hp, int argc, EjsObj **argv) { EjsFunction *observer; HttpConn *conn; observer = (EjsFunction*) argv[1]; if (observer->boundThis == 0 || observer->boundThis == ejs->global) { observer->boundThis = hp; } ejsAddObserver(ejs, &hp->emitter, argv[0], observer); conn = hp->conn; if (conn->readq && conn->readq->count > 0) { ejsSendEvent(ejs, hp->emitter, "readable", NULL, hp); } // TODO - don't need to test finalizedConnector if (!conn->tx->finalizedConnector && !conn->error && HTTP_STATE_CONNECTED <= conn->state && conn->state < HTTP_STATE_FINALIZED && conn->writeq->ioCount == 0) { httpNotify(conn, HTTP_EVENT_WRITABLE, 0); } return hp; }
/* function on(name, observer: function): Http */ static EjsWebSocket *ws_on(Ejs *ejs, EjsWebSocket *ws, int argc, EjsObj **argv) { EjsFunction *observer; HttpConn *conn; observer = (EjsFunction*) argv[1]; if (observer->boundThis == 0 || observer->boundThis == ejs->global) { observer->boundThis = ws; } ejsAddObserver(ejs, &ws->emitter, argv[0], observer); conn = ws->conn; if (conn->readq && conn->readq->count > 0) { // MOB - can't have NULL as data onWebSocketEvent(ws, HTTP_EVENT_READABLE, 0, 0); } // MOB - don't need to test finalizedConnector if (!conn->tx->finalizedConnector && !conn->error && HTTP_STATE_CONNECTED <= conn->state && conn->state < HTTP_STATE_FINALIZED && conn->writeq->ioCount == 0) { onWebSocketEvent(ws, HTTP_EVENT_WRITABLE, 0, 0); } return ws; }
/* function on(name: [String|Array], observer: Function): HttpServer */ static EjsHttpServer *hs_on(Ejs *ejs, EjsHttpServer *sp, int argc, EjsAny **argv) { // TODO -- should fire if currently readable / writable (also socket etc) ejsAddObserver(ejs, &sp->emitter, argv[0], argv[1]); return sp; }