On Thu, 23 May 2019, 'Bradford Smith' via Closure Compiler Discuss wrote:
> I believe the fix for this problem is to have the ES6 Class -> ES5 class
> transpilation use Reflect.construct, depending on a polyfill if needed.
[...]
I did some investigation today. I don't think the compiler would produce
usable code in the case of Audio Worklets (and potentially others),
because this would assume to control the calling code.
Because, registerProcessor() is given the class name, and takes
responsibity for constructing objects (with 'new', I suppose)
I tried many things but could not find a way to exploit
Reflect.construct() to operate in the context of 'this'; the usual
.call() is off-limits for the DOM object.
I'm not an expert so it would be a pleasure to be proved wrong here --
what should I use in the constructor, below?
It's interesting, as I have read today suggestions ES6 classes are a nicer
syntax but otherwise the same; this would mean they have a small
piece of additional capability.
I don't think I have an option but to bridge this with un-compiled code.
Here's my test case (no compliation required), many thanks.
<!DOCTYPE html>
<html>
<body>
<button onClick="go()">Start</button>
<script>
async function go() {
var ac = new AudioContext();
await ac.audioWorklet.addModule('./test-awp.js');
console.log('Loaded');
var worklet = new AudioWorkletNode(ac, 'reflect'); // or 'example'
worklet.port.onmessage = function(event) {
console.log(event);
}
}
</script>
</body>
/*
* An attempt to make this work using EC5 classes
*/
var ReflectProcessor = function() {
console.log("ReflectProcessor");
//
// How do we invoke the AudioWorkletProcessor constructor here?
//
// AudioWorkletProcessor.call(this); // not on a DOM object
//
// Reflect.construct(AudioWorkletProcessor); // returns object; no effect
//
// var x = new AudioWorkletProcessor();
// this.port = x.port; // can't assign
//
// var x = new AudioWorkletProcessor();
// Object.assign(this, x); // returns object; no effect
//
this.port.postMessage('Hello from ReflectProcessor');
}
ReflectProcessor.prototype = Object.create(AudioWorkletProcessor.prototype);
ReflectProcessor.prototype.process = function(ins, outs, parameters) {
return true;
}
registerProcessor('reflect', ReflectProcessor);
/*
* Suggested use of AudioWorkletProcessor, which works
*/
class ExampleProcessor extends AudioWorkletProcessor {
constructor() {
console.log("ExampleProcessor");
super();
this.port.postMessage('Hello from ExampleProcessor');
}
process(ins, outs, parameters) {
return true;
}
}
registerProcessor('example', ExampleProcessor);
--
Mark