Skip to content

Instantly share code, notes, and snippets.

@arthuro555
Created October 31, 2021 21:55
Show Gist options
  • Save arthuro555/cbd132fde37f99ecfee8e3866b335c3a to your computer and use it in GitHub Desktop.
Save arthuro555/cbd132fde37f99ecfee8e3866b335c3a to your computer and use it in GitHub Desktop.
Mockup long lived objects list
/**
* A class that manages some objects lists to be "long lived".
* Normal object lists usually last until the end of the execution of the event using it,
* or at least at the end of the events sheet execution.
*/
class LongLivedObjectsLists {
/** True as long as the object list is valid. */
private _valid: boolean = true;
private readonly _objectsLists: ObjectsLists = new Hashtable<
RuntimeObject[]
>();
/**
* Gets the validity of the list.
* If a list is invalid, it means that the owner of its objects
* (the scene) does not exist anymore and the operation using
* those should be cancelled.
*/
isValid(): boolean {
return this._valid;
}
/** Adds an object to the objects lists. */
addObject(object: RuntimeObject) {
if(!this.isValid()) return;
const list = this._objectsLists.get(object.getName());
if (!list.includes(object)) list.push(object);
}
/** Removes an object from the objects lists. */
removeObject(object: RuntimeObject) {
if(!this.isValid()) return;
const list = this._objectsLists.get(object.getName());
list.splice(list.indexOf(object), 1);
}
/** Clears the objects lists. */
clear() {
this._objectsLists.clear();
}
/**
* Marks the objects lists as invalid, should only
* be called by the owner of the objects (the scene).
* @internal
*/
_invalidate() {
// Clear the objects lists as the objects are not safe to be used anymore and should be dereferenced for garbage collection.
this.clear();
this._valid = false;
}
}
class RuntimeScene {
objectLists: LongLivedObjectsLists[];
createLongLivedObjectsLists() {
const list = new LongLivedObjectsLists();
this.objectsLists.push(list);
return list;
}
unloadScene() {
this.objectsLists.forEach(l => l._invalidate());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment