fix(DX): no need to specify separate hook for socketio

This commit is contained in:
Ankush Menat 2024-04-06 18:18:37 +05:30
parent 39f7e4e92e
commit a313fb90cb
3 changed files with 28 additions and 21 deletions

View file

@ -543,7 +543,3 @@ default_log_clearing_doctypes = {
"Activity Log": 90,
"Route History": 90,
}
# Technically we have event handlers but `frappe` gets special treatment
# SocketIO server uses this to identify which apps to lookup for event handlers.
has_realtime_event_handlers = False

View file

@ -135,16 +135,10 @@ def can_subscribe_doctype(doctype: str) -> bool:
@frappe.whitelist(allow_guest=True)
def get_user_info():
installed_apps = frappe.get_installed_apps()
apps_with_event = [
app for app in installed_apps if any(frappe.get_hooks("has_realtime_event_handlers", app_name=app))
]
return {
"user": frappe.session.user,
"user_type": frappe.session.data.user_type,
"installed_apps": apps_with_event,
"installed_apps": frappe.get_installed_apps(),
}

View file

@ -33,16 +33,12 @@ function on_connection(socket) {
frappe_handlers(realtime, socket);
socket.installed_apps.forEach((app) => {
let file = `../../${app}/realtime/handlers.js`;
let abs_path = path.resolve(__dirname, file);
if (fs.existsSync(abs_path)) {
try {
let handler_factory = require(file);
handler_factory(socket);
} catch (err) {
console.warn(`failed to load event handlers from ${abs_path}`);
console.warn(err);
}
let app_handler = get_app_handlers(app);
try {
app_handler && app_handler(socket);
} catch (err) {
console.warn(`failed to setup event handlers from ${app}`);
console.warn(err);
}
});
@ -53,6 +49,27 @@ function on_connection(socket) {
});
}
const _app_handlers = {};
function get_app_handlers(app) {
if (app in _app_handlers) {
return _app_handlers[app];
}
let file = `../../${app}/realtime/handlers.js`;
let abs_path = path.resolve(__dirname, file);
let handler = null;
if (fs.existsSync(abs_path)) {
try {
handler = require(file);
} catch (err) {
console.warn(`failed to load event handlers from ${abs_path}`);
console.warn(err);
}
}
_app_handlers[app] = handler;
return handler;
}
realtime.on("connection", on_connection);
// =======================