{"version":3,"sources":["io_handler.ts"],"sourcesContent":["\n// handles std input and output\n\nimport { RuntimeError } from \"../types/errors.ts\";\nimport { Type } from \"../types/type.ts\";\nimport type { datex_meta, datex_scope, dxb_header } from \"../utils/global_types.ts\";\nimport { client_type } from \"../utils/constants.ts\";\nimport { Endpoint, Target, target_clause } from \"../types/addressing.ts\";\nimport { Runtime } from \"./runtime.ts\";\nimport { Datex } from \"../mod.ts\";\nimport { CommunicationInterfaceSocket } from \"../network/communication-interface.ts\";\n\ntype ioEventHandler = (header:dxb_header, dxb:ArrayBuffer, socket?:CommunicationInterfaceSocket)=>void;\n\n// handles observers for all incoming/outgoing DATEX\nexport class IOHandler {\n\n    // redirect std/print\n    private static std_out:(data:any[], meta:datex_meta)=>void|Promise<void> = async (data:any[], meta:datex_meta)=>{\n        for (let d=0; d<data.length;d++) {\n            data[d] = Runtime.collapseValueCast(await Runtime.castValue(Type.std.text, data[d]));\n        }\n        client_type == \"browser\" ? console.log(...data) : console.log(\"\\x1b[90mprint\\x1b[0m \" + data.join(\"\\n\"))\n    }\n    // redirect std/printf\n    private static std_outf = (data:any[], meta:datex_meta)=>{\n        console.log(\"\\x1b[90mprintf\\x1b[0m \" + data.map(v=>Runtime.valueToDatexStringExperimental(v, true, true, false, true)).join(\"\\n\"))\n    }\n    // redirect to std/read\n    private static std_in:(data:any[])=>any = ()=>{throw new RuntimeError(\"No input available\")};\n    \n\n    // std/print, std/printf, std/read redirects for specific targets\n    private static e_std_outs = new Map<Target, globalThis.Function>();\n    private static e_std_outfs = new Map<Target, globalThis.Function>();;\n    private static e_std_ins = new Map<Target, globalThis.Function>();\n\n    // listeners for all incoming DATEX requests\n    private static datex_in_handler?: ioEventHandler/* = (header, dxb)=>{\n        console.debug('from ' + header.sender, DatexRuntime.decompile(dxb));\n    }*/\n    private static datex_in_handlers_per_endpoint = new Map<Target, ioEventHandler>();\n\n    // listeners for all outgoing DATEX requests\n    private static datex_out_handler?: ioEventHandler\n    private static datex_out_handlers_per_endpoint = new Map<Endpoint, ioEventHandler>();\n    \n    // listen for finished scopes with return value: sid -> callback\n    private static scope_result_listeners = new Map<number, (scope:datex_scope)=>void>();\n\n    // set std redirects\n    static setStdOut(output_callback:(data:any[], meta:datex_meta)=>void|Promise<void>, endpoint?:Target){\n        if (endpoint) this.e_std_outs.set(endpoint, output_callback);\n        else this.std_out = output_callback;\n    }\n    static setStdOutF(output_callback:(data:any[], meta:datex_meta)=>void|Promise<void>, endpoint?:Target){\n        if (endpoint) this.e_std_outfs.set(endpoint, output_callback);\n        else this.std_outf = output_callback;\n    }\n    static setStdIn(output_callback:(data:any[])=>any, endpoint?:Target){\n        if (endpoint) this.e_std_ins.set(endpoint, output_callback);\n        else this.std_in = output_callback;\n    }\n\n    // set DATEX listeners\n    static onDatexReceived(handler:ioEventHandler, endpoint?:Target){\n        if (endpoint) this.datex_in_handlers_per_endpoint.set(endpoint, handler);\n        else this.datex_in_handler = handler;\n    }\n    static onDatexSent(handler:ioEventHandler, endpoint?:Endpoint){\n        if (endpoint) this.datex_out_handlers_per_endpoint.set(endpoint, handler);  \n        else this.datex_out_handler = handler;\n    } \n\n    static resetDatexHandlers() {\n        this.datex_in_handler = undefined;\n        this.datex_out_handler = undefined;\n    }\n\n    // add scope result listener\n    static addScopeResultListener(sid:number, output_callback:(data:datex_scope)=>void){\n        this.scope_result_listeners.set(sid, output_callback);\n    }\n    \n\n    // redirected from std/print etc.\n    public static async stdOutF(params:any[], meta: datex_meta){\n        if(this.e_std_outfs.has(meta.caller)) await this.e_std_outfs.get(endpoint)(params, meta);\n        else if (this.std_outf) await this.std_outf(params, meta);\n    }\n    public static stdOut(params:any[], meta: datex_meta){\n        for (let i=0;i<params.length;i++) params[i] = Datex.ReactiveValue.collapseValue(params[i],true,true);\n        if(this.e_std_outs.has(meta.caller)) this.e_std_outs.get(endpoint)(params, meta);\n        else if (this.std_out) this.std_out(params, meta);\n    }\n    public static async stdIn(msg_start:any, msg_end:any, endpoint:Target){\n        if(this.e_std_ins.has(endpoint)) return this.e_std_ins.get(endpoint)([msg_start, msg_end]);\n        else return this.std_in([msg_start, msg_end]);\n    }\n\n    // called when scope received \n    static handleDatexReceived(header:dxb_header, dxb:ArrayBuffer, socket?: CommunicationInterfaceSocket) {\n        const endpoint = header.sender as Endpoint;\n\n        if (this.datex_in_handlers_per_endpoint.has(endpoint)) this.datex_in_handlers_per_endpoint.get(endpoint)!(header, dxb, socket);\n        if (this.datex_in_handler) this.datex_in_handler(header, dxb, socket);\n    }\n\n    // called when datex sent out\n    static async handleDatexSent(dxb:ArrayBuffer, to:target_clause, socket?: CommunicationInterfaceSocket) {\n        if (this.datex_out_handler || this.datex_out_handlers_per_endpoint.has(<Endpoint>to)) {\n            let header = <dxb_header> (await Runtime.parseHeader(dxb, null, true));\n\n            if (this.datex_out_handler) this.datex_out_handler(header, dxb, socket)\n            if (this.datex_out_handlers_per_endpoint.has(<Endpoint>to)) this.datex_out_handlers_per_endpoint.get(<Endpoint>to)!(header, dxb, socket)\n        }\n    }\n  \n    // when scope execution finished succesfully\n    static handleScopeFinished(sid:number, scope:datex_scope) {\n        if (this.scope_result_listeners.has(sid)) {\n            this.scope_result_listeners.get(sid)(scope);\n            this.scope_result_listeners.delete(sid);\n        }\n    }\n}\n"],"names":["RuntimeError","Type","client_type","Runtime","Datex","IOHandler","std_out","data","meta","d","length","collapseValueCast","castValue","std","text","console","log","join","std_outf","map","v","valueToDatexStringExperimental","std_in","e_std_outs","Map","e_std_outfs","e_std_ins","datex_in_handler","datex_in_handlers_per_endpoint","datex_out_handler","datex_out_handlers_per_endpoint","scope_result_listeners","setStdOut","output_callback","endpoint","set","setStdOutF","setStdIn","onDatexReceived","handler","onDatexSent","resetDatexHandlers","undefined","addScopeResultListener","sid","stdOutF","params","has","caller","get","stdOut","i","ReactiveValue","collapseValue","stdIn","msg_start","msg_end","handleDatexReceived","header","dxb","socket","sender","handleDatexSent","to","parseHeader","handleScopeFinished","scope","delete"],"mappings":"AAGA,SAASA,YAAY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