Repository: sz3/libcimbar
File: web/recv-worker.js (JavaScript)

Code before:
      if (format == "NV12") {
        type = 12;
      }
      else if (format == "I420") {
        type = 420;
      }

      try {
        // then decode in wasm, fool
        const fountainBuff = RecvWorker.fountainBuff();
        var len = Module._cimbard_scan_extract_decode(RecvWorker.imgBuff().byteOffset, width, height, type, fountainBuff.byteOffset, fountainBuff.length);

        if (len <= 0) {
          var errmsg = RecvWorker.get_error();
          errmsg = len + " " + errmsg;
          //console.log(errmsg);
          if (len == 0) {
            self.postMessage({ nodata: true, res: errmsg });
          }
          else if (len != -3) {
            self.postMessage({ error: true, res: errmsg });
          }
        }
        else { //if (len > 0) {
          console.log('len is ' + len);
          //self.postMessage({ error: true, res: len });
          const msgbuf = new Uint8Array(Module.HEAPU8.buffer, fountainBuff.byteOffset, len).slice();
          console.log(msgbuf);
          self.postMessage(msgbuf.buffer, [msgbuf.buffer]);
        }
        // in main, const receivedArray = new Uint8Array(event.data);
      } catch (e) {
        console.log(e);
      }

    },

    get_error: function () {
      const errbuff = RecvWorker.mallocPlease("error", 256);
      const errlen = Module._cimbard_get_report(errbuff.byteOffset, errbuff.length);
      if (errlen > 0) {
        const errview = new Uint8Array(Module.HEAPU8.buffer, errbuff.byteOffset, errlen);
        const decoder = new TextDecoder();
        return decoder.decode(errview);
      }
      return "";
    },

    mallocPlease: function (name, size) {
      if (_buffs[name] === undefined || size > _buffs[name].length) {
        try {

Review comment (suggestion):
There's a bit of a ticking time bomb in the way our decode queue is tracked, and I'd like to fix it before it goes off.

Specifically: we manually track inflight processing jobs. Which means, every code path *must* send a message back to the main thread.

Code after:
      } catch (e) {
        console.log(e);
      }

      var type = 4;
      if (format == "NV12") {
        type = 12;
      }
      else if (format == "I420") {
        type = 420;
      }

      // then decode in wasm, fool
      const fountainBuff = RecvWorker.fountainBuff();
      var len = Module._cimbard_scan_extract_decode(RecvWorker.imgBuff().byteOffset, width, height, type, fountainBuff.byteOffset, fountainBuff.length);
      if (len <= 0) {
        var errmsg = RecvWorker.get_error();
        errmsg = len + " " + errmsg;
        let msg = { res: errmsg };
        if (len == 0)
          msg.nodata = true;
        else if (len == -3)
          msg.failed_extract = true;
        else
          msg.error = true;
        self.postMessage(msg);
      }
      else { //if (len > 0) {
        console.log('len is ' + len);
        const msgbuf = new Uint8Array(Module.HEAPU8.buffer, fountainBuff.byteOffset, len).slice();
        //console.log(msgbuf);
        self.postMessage({ mode: mode, buff: msgbuf }, [msgbuf.buffer]);
      }
      // in main, const receivedArray = event.data.buff;
    },

    get_error: function () {
      const errbuff = RecvWorker.mallocPlease("error", 256);
      const errlen = Module._cimbard_get_report(errbuff.byteOffset, errbuff.length);
      if (errlen > 0) {
        const errview = new Uint8Array(Module.HEAPU8.buffer, errbuff.byteOffset, errlen);
        const decoder = new TextDecoder();
        return decoder.decode(errview);
      }
      return "";
    },

    mallocPlease: function (name, size) {
      if (_buffs[name] === undefined || size > _buffs[name].length) {
        try {
          if (size > _buffs[name].length) {