@@ -272,6 +272,7 @@ const createScatterplot = (
272272 lassoColor = toRgba ( lassoColor , true ) ;
273273 reticleColor = toRgba ( reticleColor , true ) ;
274274
275+ let isDestroyed = false ;
275276 let backgroundColorBrightness = rgbBrightness ( backgroundColor ) ;
276277 let camera ;
277278 let lasso ;
@@ -2032,10 +2033,22 @@ const createScatterplot = (
20322033 * @param {import('./types').ScatterplotMethodOptions['draw'] } options
20332034 * @returns {Promise<void> }
20342035 */
2035- const publicDraw = ( newPoints , options = { } ) =>
2036- toArrayOrientedPoints ( newPoints ) . then (
2036+ const publicDraw = ( newPoints , options = { } ) => {
2037+ if ( isDestroyed ) {
2038+ return Promise . reject ( new Error ( 'The instance was already destroyed' ) ) ;
2039+ }
2040+ return toArrayOrientedPoints ( newPoints ) . then (
20372041 ( points ) =>
20382042 new Promise ( ( resolve ) => {
2043+ if ( isDestroyed ) {
2044+ // In the special case where the instance was destroyed after
2045+ // scatterplot.draw() was called but before toArrayOrientedPoints()
2046+ // resolved, we will _not_ reject the promise as this would be
2047+ // confusing. Instead we will immediately resolve and return.
2048+ resolve ( ) ;
2049+ return ;
2050+ }
2051+
20392052 let pointsCached = false ;
20402053 if ( points ) {
20412054 if ( options . transition ) {
@@ -2085,6 +2098,7 @@ const createScatterplot = (
20852098 }
20862099 } )
20872100 ) ;
2101+ } ;
20882102
20892103 /** @type {<F extends Function>(f: F) => (...args: Parameters<F>) => ReturnType<F> } */
20902104 const withDraw =
@@ -3238,6 +3252,7 @@ const createScatterplot = (
32383252 } ;
32393253
32403254 const destroy = ( ) => {
3255+ isDestroyed = true ;
32413256 cancelFrameListener ( ) ;
32423257 window . removeEventListener ( 'keyup' , keyUpHandler , false ) ;
32433258 window . removeEventListener ( 'blur' , blurHandler , false ) ;
@@ -3261,15 +3276,15 @@ const createScatterplot = (
32613276 pointConnections . destroy ( ) ;
32623277 reticleHLine . destroy ( ) ;
32633278 reticleVLine . destroy ( ) ;
3264- pubSub . publish ( 'destroy' ) ;
3265- pubSub . clear ( ) ;
32663279 if ( ! initialProperties . renderer ) {
32673280 // Since the user did not pass in an externally created renderer we can
32683281 // assume that the renderer is only used by this scatter plot instance.
32693282 // Therefore it's save to destroy it when this scatter plot instance is
32703283 // destroyed.
32713284 renderer . destroy ( ) ;
32723285 }
3286+ pubSub . publish ( 'destroy' ) ;
3287+ pubSub . clear ( ) ;
32733288 } ;
32743289
32753290 init ( ) ;
0 commit comments