1

我正在学习 google codelab 示例Web Serial API 入门

该示例出现故障,因此我重新混合了代码并按照说明复制并粘贴了这些代码

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

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

然后我得到'TextDecoderStream'没有定义。

4

1 回答 1

1

如果不支持,您可以进行多边形填充 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}
}
于 2020-08-11T01:55:16.600 回答