import { injectable } from '@cloudbeaver/core-di' ; import { GraphQLService , CachedMapResource , ResourceKey , isResourceKeyList , resourceKeyList , ResourceKeyUtils } from '@cloudbeaver/core-sdk' ; import type { DBObject } from './EntityTypes' ; import { NavNodeInfoResource } from './NavNodeInfoResource' ; import { NavTreeResource } from './NavTreeResource' ; @ injectable ( ) export class DBObjectResource extends CachedMapResource < string , DBObject > { constructor ( private graphQLService , private navNodeInfoResource , private navTreeResource ) { super ( ) ; this . navNodeInfoResource . outdateResource ( this ) ; this . navNodeInfoResource . deleteInResource ( this ) ; this . navNodeInfoResource . onDataOutdated . addHandler ( this . outdateChildren . bind ( this ) ) ; } async loadChildren ( parentId , key ) { await this . performUpdate ( key , [ ] , ( ) => this . loadFromChildren ( parentId ) , ( ) => this . isLoaded ( key ) && ! this . isOutdated ( key ) ) ; return this . data ; } protected async loader ( key ) { if ( isResourceKeyList ( key ) ) { const values = [ ] ; for ( const navNodeId of key . list ) { values . push ( await this . loadDBObjectInfo ( navNodeId ) ) ; } this . set ( key , values ) ; } else { this . set ( key , await this . loadDBObjectInfo ( key ) ) ; } return this . data ; } private async loadFromChildren ( parentId ) { const { dbObjects } = await this . graphQLService . sdk . getChildrenDBObjectInfo ( { navNodeId : parentId , } ) ; this . set ( resourceKeyList ( dbObjects . map ( dbObject => dbObject . id ) ) , dbObjects ) ; } private async loadDBObjectInfo ( navNodeId ) { const { objectInfo } = await this . graphQLService . sdk . getDBObjectInfo ( { navNodeId , } ) ; return objectInfo ; } private outdateChildren ( key ) { const childrenToOutdate = [ ] ; ResourceKeyUtils . forEach ( key , key => { childrenToOutdate . push ( ... this . navTreeResource . get ( key ) || [ ] ) ; } ) ; const outdateKey = resourceKeyList ( childrenToOutdate ) ; this . markOutdated ( outdateKey ) ; } }