From 841b029d7fbc3219b92b158b69ea09a39627c601 Mon Sep 17 00:00:00 2001 From: "Michael \"Z\" Goddard" Date: Thu, 7 Jun 2018 22:17:35 -0400 Subject: [PATCH] streamline connecting Effects - Add sanity checks to connect --- src/effects/Effect.js | 24 +++++++++++++++++------- src/effects/PanEffect.js | 4 ++++ src/effects/VolumeEffect.js | 4 ++++ 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/effects/Effect.js b/src/effects/Effect.js index 66bcd39..1dc1bbe 100644 --- a/src/effects/Effect.js +++ b/src/effects/Effect.js @@ -111,26 +111,36 @@ class Effect { * @param {object} target - target whose node to should be connected */ connect (target) { - this.target = target; - 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) { this.outputNode.disconnect(); } - let nextTarget = target; if (this._isPatch) { - nextTarget = this; this.outputNode.connect(target.getInputNode()); } if (this.lastEffect === null) { - this.audioPlayer.connect(nextTarget); + this.audioPlayer.connect(this); } else { - this.lastEffect.connect(nextTarget); + this.lastEffect.connect(this); } } diff --git a/src/effects/PanEffect.js b/src/effects/PanEffect.js index 340dcf0..ea50c2f 100644 --- a/src/effects/PanEffect.js +++ b/src/effects/PanEffect.js @@ -66,6 +66,10 @@ class PanEffect extends Effect { * Clean up and disconnect audio nodes. */ dispose () { + if (!this.initialized) { + return; + } + this.inputNode.disconnect(); this.leftGain.disconnect(); this.rightGain.disconnect(); diff --git a/src/effects/VolumeEffect.js b/src/effects/VolumeEffect.js index 0d79052..8e536d7 100644 --- a/src/effects/VolumeEffect.js +++ b/src/effects/VolumeEffect.js @@ -41,6 +41,10 @@ class VolumeEffect extends Effect { * Clean up and disconnect audio nodes. */ dispose () { + if (!this.initialized) { + return; + } + this.outputNode.disconnect(); this.inputNode = null;