11import { animate , cancelAnimation , getRangeValue } from "../Utils/MathUtils.js" ;
2- import {
3- defaultFps ,
4- defaultFpsLimit ,
5- millisecondsToSeconds ,
6- minFpsLimit ,
7- removeDeleteCount ,
8- removeMinIndex ,
9- } from "./Utils/Constants.js" ;
2+ import { defaultFps , defaultFpsLimit , millisecondsToSeconds , minFpsLimit } from "./Utils/Constants.js" ;
103import { CanvasManager } from "./CanvasManager.js" ;
11- import type { Engine } from "./Engine .js" ;
4+ import type { CustomEventArgs } from "../Types/CustomEventArgs .js" ;
125import { EventListeners } from "./Utils/EventListeners.js" ;
136import { EventType } from "../Enums/Types/EventType.js" ;
147import type { IContainerPlugin } from "./Interfaces/IContainerPlugin.js" ;
@@ -25,6 +18,14 @@ import { Retina } from "./Retina.js";
2518import { getLogger } from "../Utils/LogUtils.js" ;
2619import { loadOptions } from "../Utils/OptionsUtils.js" ;
2720
21+ interface ContainerParams {
22+ dispatchCallback : ( eventType : string , args ?: CustomEventArgs ) => void ;
23+ id : string ;
24+ onDestroy : ( remove : boolean ) => void ;
25+ pluginManager : PluginManager ;
26+ sourceOptions ?: ISourceOptions ;
27+ }
28+
2829/**
2930 * Checks if the container is still usable
3031 * @param container - the container to check
@@ -132,12 +133,12 @@ export class Container {
132133 private _delay : number ;
133134 private _delayTimeout ?: number | NodeJS . Timeout ;
134135 private readonly _delta : IDelta = { value : 0 , factor : 0 } ;
136+ private readonly _dispatchCallback ;
135137 private _drawAnimationFrame ?: number ;
136138 /**
137139 * The container duration
138140 */
139141 private _duration ;
140- private readonly _engine ;
141142 private readonly _eventListeners ;
142143 private _firstStart ;
143144 private _initialSourceOptions ;
@@ -149,19 +150,23 @@ export class Container {
149150 * The container lifetime
150151 */
151152 private _lifeTime ;
153+ private readonly _onDestroy ;
152154 private _options ;
153155 private _paused ;
156+ private readonly _pluginManager ;
154157 private _smooth ;
155158 private _sourceOptions ;
156159
157160 /**
158161 * This is the core class, create an instance to have a new working particles manager
159- * @param engine - the engine used by container
160- * @param id - the id to identify this instance
161- * @param sourceOptions - the options to load
162+ * @param params -
162163 */
163- constructor ( engine : Engine , id : string , sourceOptions ?: ISourceOptions ) {
164- this . _engine = engine ;
164+ constructor ( params : ContainerParams ) {
165+ const { dispatchCallback, pluginManager, id, onDestroy, sourceOptions } = params ;
166+
167+ this . _pluginManager = pluginManager ;
168+ this . _dispatchCallback = dispatchCallback ;
169+ this . _onDestroy = onDestroy ;
165170 this . id = Symbol ( id ) ;
166171 this . fpsLimit = 120 ;
167172 this . hdr = false ;
@@ -182,15 +187,15 @@ export class Container {
182187 this . shapeDrawers = new Map ( ) ;
183188 this . particleUpdaters = [ ] ;
184189 this . retina = new Retina ( this ) ;
185- this . canvas = new CanvasManager ( this . _engine . pluginManager , this ) ;
186- this . particles = new ParticlesManager ( this . _engine . pluginManager , this ) ;
190+ this . canvas = new CanvasManager ( this . _pluginManager , this ) ;
191+ this . particles = new ParticlesManager ( this . _pluginManager , this ) ;
187192 this . plugins = [ ] ;
188193 this . particleDestroyedPlugins = [ ] ;
189194 this . particleCreatedPlugins = [ ] ;
190195 this . particlePositionPlugins = [ ] ;
191196 /* tsParticles variables with default values */
192- this . _options = loadContainerOptions ( this . _engine . pluginManager , this ) ;
193- this . actualOptions = loadContainerOptions ( this . _engine . pluginManager , this ) ;
197+ this . _options = loadContainerOptions ( this . _pluginManager , this ) ;
198+ this . actualOptions = loadContainerOptions ( this . _pluginManager , this ) ;
194199
195200 /* ---------- tsParticles - start ------------ */
196201 this . _eventListeners = new EventListeners ( this ) ;
@@ -260,24 +265,17 @@ export class Container {
260265 this . particleUpdaters = [ ] ;
261266 this . plugins . length = 0 ;
262267
263- this . _engine . pluginManager . clearPlugins ( this ) ;
268+ this . _pluginManager . clearPlugins ( this ) ;
264269
265270 this . destroyed = true ;
266271
267- if ( remove ) {
268- const mainArr = this . _engine . items ,
269- idx = mainArr . indexOf ( this ) ;
270-
271- if ( idx >= removeMinIndex ) {
272- mainArr . splice ( idx , removeDeleteCount ) ;
273- }
274- }
272+ this . _onDestroy ( remove ) ;
275273
276274 this . dispatchEvent ( EventType . containerDestroyed ) ;
277275 }
278276
279277 dispatchEvent ( type : string , data ?: unknown ) : void {
280- this . _engine . dispatchEvent ( type , {
278+ this . _dispatchCallback ( type , {
281279 container : this ,
282280 data,
283281 } ) ;
@@ -335,7 +333,7 @@ export class Container {
335333
336334 const allContainerPlugins = new Map < IPlugin , IContainerPlugin > ( ) ;
337335
338- for ( const plugin of this . _engine . pluginManager . plugins ) {
336+ for ( const plugin of this . _pluginManager . plugins ) {
339337 const containerPlugin = await plugin . getPlugin ( this ) ;
340338
341339 if ( containerPlugin . preInit ) {
@@ -348,13 +346,8 @@ export class Container {
348346 await this . initDrawersAndUpdaters ( ) ;
349347
350348 /* options settings */
351- this . _options = loadContainerOptions (
352- this . _engine . pluginManager ,
353- this ,
354- this . _initialSourceOptions ,
355- this . sourceOptions ,
356- ) ;
357- this . actualOptions = loadContainerOptions ( this . _engine . pluginManager , this , this . _options ) ;
349+ this . _options = loadContainerOptions ( this . _pluginManager , this , this . _initialSourceOptions , this . sourceOptions ) ;
350+ this . actualOptions = loadContainerOptions ( this . _pluginManager , this , this . _options ) ;
358351
359352 this . plugins . length = 0 ;
360353 this . particleDestroyedPlugins . length = 0 ;
@@ -416,7 +409,7 @@ export class Container {
416409 }
417410
418411 async initDrawersAndUpdaters ( ) : Promise < void > {
419- const pluginManager = this . _engine . pluginManager ;
412+ const pluginManager = this . _pluginManager ;
420413
421414 this . effectDrawers = await pluginManager . getEffectDrawers ( this , true ) ;
422415 this . shapeDrawers = await pluginManager . getShapeDrawers ( this , true ) ;
@@ -508,13 +501,8 @@ export class Container {
508501
509502 this . _initialSourceOptions = sourceOptions ;
510503 this . _sourceOptions = sourceOptions ;
511- this . _options = loadContainerOptions (
512- this . _engine . pluginManager ,
513- this ,
514- this . _initialSourceOptions ,
515- this . sourceOptions ,
516- ) ;
517- this . actualOptions = loadContainerOptions ( this . _engine . pluginManager , this , this . _options ) ;
504+ this . _options = loadContainerOptions ( this . _pluginManager , this , this . _initialSourceOptions , this . sourceOptions ) ;
505+ this . actualOptions = loadContainerOptions ( this . _pluginManager , this , this . _options ) ;
518506
519507 return this . refresh ( ) ;
520508 }
0 commit comments