streamline connecting Effects

- Add sanity checks to connect
This commit is contained in:
Michael "Z" Goddard 2018-06-07 22:17:35 -04:00
parent e40e6e0b1b
commit 841b029d7f
No known key found for this signature in database
GPG key ID: 762CD40DD5349872
3 changed files with 25 additions and 7 deletions

View file

@ -111,26 +111,36 @@ class Effect {
* @param {object} target - target whose node to should be connected * @param {object} target - target whose node to should be connected
*/ */
connect (target) { connect (target) {
this.target = target;
if (target === null) { if (target === null) {
return; throw new Error('target may not be null');
} }
const checkForCircularReference = subtarget => {
if (subtarget) {
if (subtarget === this) {
return true;
}
return checkForCircularReference(subtarget.target);
}
};
if (checkForCircularReference(target)) {
throw new Error('Effect cannot connect to itself');
}
this.target = target;
if (this.outputNode !== null) { if (this.outputNode !== null) {
this.outputNode.disconnect(); this.outputNode.disconnect();
} }
let nextTarget = target;
if (this._isPatch) { if (this._isPatch) {
nextTarget = this;
this.outputNode.connect(target.getInputNode()); this.outputNode.connect(target.getInputNode());
} }
if (this.lastEffect === null) { if (this.lastEffect === null) {
this.audioPlayer.connect(nextTarget); this.audioPlayer.connect(this);
} else { } else {
this.lastEffect.connect(nextTarget); this.lastEffect.connect(this);
} }
} }

View file

@ -66,6 +66,10 @@ class PanEffect extends Effect {
* Clean up and disconnect audio nodes. * Clean up and disconnect audio nodes.
*/ */
dispose () { dispose () {
if (!this.initialized) {
return;
}
this.inputNode.disconnect(); this.inputNode.disconnect();
this.leftGain.disconnect(); this.leftGain.disconnect();
this.rightGain.disconnect(); this.rightGain.disconnect();

View file

@ -41,6 +41,10 @@ class VolumeEffect extends Effect {
* Clean up and disconnect audio nodes. * Clean up and disconnect audio nodes.
*/ */
dispose () { dispose () {
if (!this.initialized) {
return;
}
this.outputNode.disconnect(); this.outputNode.disconnect();
this.inputNode = null; this.inputNode = null;