import { createCache , getValue } from '@glimmer/tracking/primitives/cache' ; import { setOwner } from '@ember/application' ; import { associateDestroyableChild , registerDestructor } from '@ember/destroyable' ; import { capabilities as helperCapabilities , setHelperManager } from '@ember/helper' ; import type { ArgsWrapper , Cache , LooseArgs } from '../types' ; export declare interface LifecycleResource < T extends LooseArgs = ArgsWrapper > { args : T ; setup ( ) : void ; update ( ) : void ; teardown ( ) : void ; } export class LifecycleResource < T extends LooseArgs = ArgsWrapper > { constructor ( owner , public args ) { setOwner ( this , owner ) ; } } class LifecycleResourceManager { capabilities = helperCapabilities ( '3.23' , { hasValue : true , hasDestroyable : true , } ) ; constructor ( protected owner ) { } createHelper ( Class , args ) { let owner = this . owner ; let instance ; let cache = createCache ( ( ) => { if ( instance === undefined ) { instance = setupInstance ( cache , Class , owner , args ) ; } else { instance . update ( ) ; } return instance ; } ) ; return cache ; } getValue ( cache ) { let instance = getValue ( cache ) ; return instance ; } getDestroyable ( cache ) { return cache ; } } function setupInstance ( cache , Class , owner , args ) { let instance = new Class ( owner , args ) ; associateDestroyableChild ( cache , instance ) ; if ( 'setup' in instance ) { instance . setup ( ) ; } if ( 'teardown' in instance ) { registerDestructor ( instance , ( ) => instance . teardown ( ) ) ; } return instance ; } setHelperManager ( ( owner ) => new LifecycleResourceManager ( owner ) , LifecycleResource ) ;