Skip to content

Commit

Permalink
fix(core): world deletes it-self from the worlds list to prevent unus…
Browse files Browse the repository at this point in the history
…ed references
  • Loading branch information
HoyosJuan committed Sep 3, 2024
1 parent 73e5030 commit d8598f9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 23 deletions.
23 changes: 2 additions & 21 deletions packages/core/src/core/Worlds/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
BaseScene,
BaseCamera,
BaseRenderer,
DataMap,
} from "../Types";
import { Components } from "../Components";
import { SimpleWorld } from "./src";
Expand All @@ -32,23 +33,11 @@ export class Worlds extends Component implements Updateable, Disposable {
/** {@link Disposable.onDisposed} */
readonly onDisposed = new Event();

/**
* An event that is triggered when a new world is created.
* The event passes the newly created world as a parameter.
*/
readonly onWorldCreated = new Event<World>();

/**
* An event that is triggered when a world is deleted.
* The event passes the UUID of the deleted world as a parameter.
*/
readonly onWorldDeleted = new Event<string>();

/**
* A collection of worlds managed by this component.
* The key is the unique identifier (UUID) of the world, and the value is the World instance.
*/
list = new Map<string, World>();
list = new DataMap<string, World>();

/** {@link Component.enabled} */
enabled = true;
Expand Down Expand Up @@ -78,7 +67,6 @@ export class Worlds extends Component implements Updateable, Disposable {
throw new Error("There is already a world with this name!");
}
this.list.set(id, world);
this.onWorldCreated.trigger(world);
return world;
}

Expand All @@ -88,26 +76,19 @@ export class Worlds extends Component implements Updateable, Disposable {
* @param {World} world - The world to be deleted.
*
* @throws {Error} - Throws an error if the provided world is not found in the list.
*
* @returns {void}
*/
delete(world: World) {
if (!this.list.has(world.uuid)) {
throw new Error("The provided world is not found in the list!");
}

const uuid = world.uuid;
this.list.delete(world.uuid);
world.dispose();
this.onWorldDeleted.trigger(uuid);
}

/**
* Disposes of the Worlds component and all its managed worlds.
* This method sets the enabled flag to false, disposes of all worlds, clears the list,
* and triggers the onDisposed event.
*
* @returns {void}
*/
dispose() {
this.enabled = false;
Expand Down
9 changes: 7 additions & 2 deletions packages/core/src/core/Worlds/src/simple-world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
Updateable,
} from "../../Types";
import { Disposer } from "../../Disposer";
import { Worlds } from "..";

/**
* A class representing a simple world in a 3D environment. It extends the Base class and implements the World interface.
Expand Down Expand Up @@ -53,9 +54,9 @@ export class SimpleWorld<
enabled = true;

/**
* A unique identifier for the world.
* A unique identifier for the world. Is not meant to be changed at any moment.
*/
uuid = UUID.create();
readonly uuid = UUID.create();

/**
* An optional name for the world.
Expand Down Expand Up @@ -200,6 +201,10 @@ export class SimpleWorld<
this._camera = null as any;
this._renderer = null as any;

const worlds = this.components.get(Worlds);
worlds.list.delete(this.uuid);

this.onDisposed.trigger();
this.onDisposed.reset();
}
}

0 comments on commit d8598f9

Please sign in to comment.