/** @format */ import { INVOKE_SERVICE_API } from '../../constants'; type ServiceMethod = ( args: Args, publish: (res: Res) => void, ) => void; const serviceMethods: Record> = Object.create(null); export function subscribeServiceMethod() { QinServiceJSBridge.subscribe(INVOKE_SERVICE_API, onInvokeServiceMethod); } export function registerServiceMethod( name: string, fn: ServiceMethod, ) { if (!serviceMethods[name]) { serviceMethods[name] = fn; } } function onInvokeServiceMethod( { id, name, args, }: { id: number; name: string; args: any; }, pageId: number, ) { const publish = (res: unknown) => { id && QinServiceJSBridge.publishHandler( INVOKE_SERVICE_API + '.' + id, res, pageId, ); }; const handler = serviceMethods[name]; if (handler) { handler(args, publish); } else { publish({}); } }