import { createCache , getValue } from '@glimmer/tracking/primitives/cache' ; import { getOwner , setOwner } from '@ember/application' ; import { associateDestroyableChild , destroy } from '@ember/destroyable' ; import { capabilities as helperCapabilities , setHelperManager } from '@ember/helper' ; import type { ArgsWrapper , Cache , LooseArgs } from '../types' ; export declare interface Resource < T extends LooseArgs = ArgsWrapper > { args : T ; } export class Resource < T extends LooseArgs = ArgsWrapper > { static next < Args extends ArgsWrapper , R extends Resource < Args > > ( prev , args ) { return new this ( getOwner ( prev ) , args , prev ) as R ; } param { unknown } owner param { T } args param { Resource < T > } previous constructor ( owner , public args , previous ? ) { setOwner ( this , owner ) ; } } class ResourceManager { capabilities = helperCapabilities ( '3.23' , { hasValue : true , hasDestroyable : true , } ) ; constructor ( protected owner ) { } createHelper ( Class , args ) { let owner = this . owner ; let instance ; let cache = createCache ( ( ) => { let newInstance = new Class ( owner , args , instance ) ; associateDestroyableChild ( cache , newInstance ) ; if ( instance ) destroy ( instance ) ; instance = newInstance ; return instance ; } ) ; return cache ; } getValue ( cache ) { let instance = getValue ( cache ) ; return instance ; } getDestroyable ( cache ) { return cache ; } } setHelperManager ( ( owner ) => new ResourceManager ( owner ) , Resource ) ;