@MusiCode אמר בAPI לימות המשיח:
איך עושים?
עשיתי קצת שינויים בקוד שלך והמרתי אותו לקלאס של EMS6, יתכן שזה מספיק בלי TS, לא בדקתי עד הסוף
const Call = require("./call");
const Router = require("express").Router;
const EventEmitter = require("this.events");
class Call {
read (massage) { }
id_list_message (massage) { }
}
class Yemot_Router extends Router {
constructor() {
super()
this.events = new EventEmitter();
this.this.active_calls = {};
}
/**
*
* @param {string} path
* @param {Call} fn
*/
addFn (path, fn) {
this.all(path, (req, res, next) => {
req.query = check_query(req.query);
const call_id = req.query.ApiCallId;
const [current_call, is_new_req] = this.get_current_call(call_id);
current_call.get_req_vals(req, res, next);
if (is_new_req) {
fn(current_call).then((r) => {
delete this.this.active_calls[call_id];
console.log(call_id, "deleted", r);
});
} else {
this.events.emit(call_id);
}
});
};
get_current_call (call_id) {
let current_call = this.active_calls[call_id];
let is_new_req = false;
if (!current_call) {
current_call = this.active_calls[call_id] = new Call(call_id, this.events);
is_new_req = true;
console.log(call_id + " is new");
}
return [current_call, is_new_req];
};
const check_query = function check_query (query) {
if (typeof query == "object") {
let iterator;
for (const key of Object.keys(query)) {
iterator = query[key];
if (typeof iterator === "object") {
query[key] = iterator[(iterator.length - 1)];
}
}
}
return query;
};
};
module.exports = () => new Yemot_Router()