Created
October 31, 2021 21:55
-
-
Save arthuro555/cbd132fde37f99ecfee8e3866b335c3a to your computer and use it in GitHub Desktop.
Mockup long lived objects list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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