advanced-ussd-builder
Version:
Advanced USSD Menu Builder with persistent state and navigation
2 lines (1 loc) • 2.93 kB
JavaScript
Object.defineProperty(exports,"__esModule",{value:!0}),exports.HandlerRegistry=void 0;const index_1=require("./index"),crypto_1=require("crypto");class HandlerRegistry{constructor(){this.handlers=new Map,this.pathHandlers=new Map}static getInstance(){return HandlerRegistry.instance||(HandlerRegistry.instance=new HandlerRegistry),HandlerRegistry.instance}generateHandlerId(){return`handler_${(0,crypto_1.randomBytes)(16).toString("hex")}_${Date.now()}`}register(n){const e=this.generateHandlerId();return this.handlers.set(e,n),e}registerPath(n,e){const r=`path_${(0,crypto_1.randomBytes)(8).toString("hex")}`,t=e?`${n}#${e}`:n;return this.pathHandlers.set(r,t),r}get(n){return this.handlers.get(n)}getPath(n){const e=this.pathHandlers.get(n);if(!e)return;const r=e.split("#");if(!r[0])return;const t={path:r[0]};return r[1]&&(t.handlerName=r[1]),t}serialize(n){const e={...n};if("handler"in n){const r=n;if(typeof r.handler=="function"&&r.handler_type==="function"){const t=this.register(r.handler);e.handler=t,e._handler_ref=!0}else if(typeof r.handler=="string"&&r.handler_type==="path"){const t=this.registerPath(r.handler,r.handler_name_in_destination);e.handler=t,e._path_ref=!0}}if("proxy_config"in n&&n.proxy_config?.proxy_handler){const r=n.proxy_config.proxy_handler;if(typeof r=="function"){const t=this.register(r);e.proxy_config={...e.proxy_config,proxy_handler:t},e._proxy_handler_ref=!0}}return e.children&&Array.isArray(e.children)&&(e.children=e.children.map(r=>{if(r instanceof index_1.UssdMenu||r&&typeof r=="object"&&(r.handler||r.proxy_config||r.render)){const t=this.serialize(r);return JSON.parse(t)}return r})),JSON.stringify(e)}deserialize(n){const e=JSON.parse(n);if(e._handler_ref&&typeof e.handler=="string"){const r=this.get(e.handler);r?e.handler=r:console.warn(`Handler ${e.handler} not found in registry`),delete e._handler_ref}else if(e._path_ref&&typeof e.handler=="string"){const r=this.getPath(e.handler);r&&(e.handler=r.path,r.handlerName&&(e.handler_name_in_destination=r.handlerName)),delete e._path_ref}if(e._proxy_handler_ref&&e.proxy_config?.proxy_handler&&typeof e.proxy_config.proxy_handler=="string"){const r=this.get(e.proxy_config.proxy_handler);r?e.proxy_config.proxy_handler=r:console.warn(`Proxy handler ${e.proxy_config.proxy_handler} not found in registry`),delete e._proxy_handler_ref}return e.children&&Array.isArray(e.children)&&(e.children=e.children.map(r=>r&&typeof r=="object"&&(r._handler_ref||r._path_ref||r._proxy_handler_ref)?this.deserialize(JSON.stringify(r)):r)),"title"in e&&"children"in e&&"custom_input"in e?new index_1.UssdMenu(e.title,e.children,e.custom_input):e}cleanup(n=36e5){const e=Date.now(),r=[];this.handlers.forEach((t,a)=>{const i=a.split("_"),s=parseInt(i[i.length-1]||"0");e-s>n&&r.push(a)}),r.forEach(t=>this.handlers.delete(t))}clear(){this.handlers.clear(),this.pathHandlers.clear()}}exports.HandlerRegistry=HandlerRegistry;
;