3

I am learning from google codelab example Getting started with the Web Serial API

The example is on glitch so I remixed the code and follow the instructions and copied and pasted these code

let decoder = new TextDecoderStream();
inputDone = port.readable.pipeTo(decoder.writable);
inputStream = decoder.readable;

reader = inputStream.getReader();
readLoop();

Then I got 'TextDecoderStream' is not defined.

user14084021
  • 31
  • 1
  • 2

1 Answers1

4

If it's not supported you can make a poly fill https://developer.mozilla.org/en-US/docs/Web/API/TransformStream

const tds = {
  start(){
    this.decoder = new TextDecoder(this.encoding, this.options)
  },
  transform(chunk, controller) {
    controller.enqueue(this.decoder.decode(chunk))
  }
}
let _jstds_wm = new WeakMap(); /* info holder */
class TextDecoderStream extends TransformStream {
  constructor(encoding = 'utf-8', {...options} = {}) {
    let t = {...tds, encoding, options}

    super(t)
    _jstds_wm.set(this, t)
  }
  get encoding() {return _jstds_wm.get(this).decoder.encoding}
  get fatal() {return _jstds_wm.get(this).decoder.fatal}
  get ignoreBOM() {return _jstds_wm.get(this).decoder.ignoreBOM}
}
andyhasit
  • 14,137
  • 7
  • 49
  • 51
  • I added the code and got 'TransfromSteam' 'TDS' '_jstds_wm' is not defined. – user14084021 Aug 11 '20 at 02:26
  • @user14084021 yes, there was a syntax error on mozilla's own page. I have updated this answer and also asked them to change it: https://github.com/mdn/kuma/issues/7579 – andyhasit Oct 22 '20 at 15:57