import { Burpanet } from "../Public/burpanet.js"; import { BurpaResource } from "./BurpaResource.js"; import { ElementWithSrc } from "./ElementWithSource.js"; export class BurpaTransform extends ElementWithSrc { inputElem: BurpaResource | null = null; fnElem: BurpaResource | null = null; feedbackElem: BurpaResource | null = null; constructor() { super(); } connectedCallback() { this.fnElem = this.querySelector('[slot="function"]') as BurpaResource; this.inputElem = this.querySelector('[slot="input"]') as BurpaResource; this.feedbackElem = this.querySelector('[slot="feedback-function"]') as BurpaResource; super.connectedCallback(); } postFeedback(feedback: any) { if (this.inputElem) { let url = this.inputElem.src; console.warn("POSTING FEEDBACK ON TRANSFORM", url, feedback); let response = Burpanet.fetch(url, { method: "POST", body: feedback }) } } async readData() { await customElements.whenDefined("burpa-resource"); this.readInput(); this.readFunction(); } async readInput() { if (this.inputElem) { for await (let d of this.inputElem.source) { this.hasComponents.input = true; this.updateCurrent(); } } } async readFunction() { if (this.fnElem) { for await (let d of this.fnElem.source) { this.hasComponents.function = true; this.updateCurrent(); } } } hasComponents = { input: false, function: false } async updateCurrent() { let data; if (!this.hasComponents.function || !this.hasComponents.input) { return; } if (this.inputElem) { data = await this.inputElem.source.current; } else { data = undefined; } let fn: (input: any) => any; if (this.fnElem) { //console.log("WAITING FOR FUNCTION") let module: any = await this.fnElem.source.current; //console.log("RECEIVED FUNCTION") if (typeof (module) == "string") { module = eval(module); } if (typeof (module) == "function") { fn = module as (input: any) => any; } else { fn = module.default as (input: any) => any; } } else { fn = (input: any) => input; } let transformed; if (fn !== undefined) { transformed = await fn(data); } else { transformed = data; } Burpanet.state.log("Publish", `transform by: ${this.idString()} output='${ElementWithSrc.toString(transformed)}'`); this.source.setCurrent(transformed); } } customElements.define("burpa-transform", BurpaTransform);