import { Destroyable } from "./SceneBase" ; import { GfxDevice } from "./gfx/platform/GfxPlatform" ; class DataShareObject < T extends Destroyable > { constructor ( public object , public lastUsedAge ) { } } export class DataShare { private objects = new Map < string , DataShareObject < Destroyable > > ( ) ; private currentAge = 0 ; public loadNewScene ( ) { this . currentAge ++ ; } private deleteObjectsOlderThan ( device , ageThreshold ) { for ( const [ k , v ] of this . objects . entries ( ) ) { if ( v . lastUsedAge <= ageThreshold ) { v . object . destroy ( device ) ; this . objects . delete ( k ) ; } } } public pruneOldObjects ( device , delta ) { const ageThreshold = this . currentAge - delta ; this . deleteObjectsOlderThan ( device , ageThreshold ) ; } public setObject < T extends Destroyable > ( key , object ) { const dsObject = new DataShareObject ( object , this . currentAge ) ; this . objects . set ( key , dsObject ) ; } public getObject < T extends Destroyable > ( key ) { if ( this . objects . has ( key ) ) { const dsObject = this . objects . get ( key ) ! ; dsObject . lastUsedAge = this . currentAge ; return dsObject . object as T ; } else { return null ; } } public async ensureObject < T extends Destroyable > ( key , ensureFunc ) { let object = this . getObject ( key ) as T ; if ( object === null ) { object = await ensureFunc ( ) ; this . setObject ( key , object ) ; } return object ; } }