// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. import { core, primordials } from "ext:core/mod.js"; import { listen_event, send_to_host } from "ext:core/ops"; const { SymbolAsyncIterator, } = primordials; class IpcConn { id = crypto.randomUUID(); #name; constructor(name){ this.#name = name; } async nextRequest() { let nextRequest; try { nextRequest = await listen_event({name:this.#name}) }catch (error) { console.log(error); throw error; } const request = nextRequest; const respondWith = createRespondWith( this, request, ); return { request, respondWith }; } async send(event,content) { await send_to_host({ id: this.id, event, content: JSON.stringify(content), }) } [SymbolAsyncIterator]() { // deno-lint-ignore no-this-alias const httpConn = this; return { async next() { const reqEvt = await httpConn.nextRequest(); // Change with caution, current form avoids a v8 deopt return {value: reqEvt ?? undefined, done: reqEvt === null}; } }; } } function createRespondWith(ipcConn,request){ return async function respondWith(name,resp) { try{ resp = await resp; await ipcConn.send(name,request) }catch(error){ console.log(error); throw error; } }; } globalThis.IpcConn= IpcConn;