@@ -28,6 +28,7 @@ export interface IDynamicDsService<DS> {
2828 destroyDynamicDatasource ( templateName : string , currentBlockHeight : number , index : number ) : Promise < void > ;
2929 getDynamicDatasources ( forceReload ?: boolean ) : Promise < DS [ ] > ;
3030 getDynamicDatasourcesByTemplate ( templateName : string ) : DynamicDatasourceInfo [ ] ;
31+ getDatasourceParamByIndex ( index : number ) : DatasourceParams | undefined ;
3132}
3233
3334@Injectable ( )
@@ -94,6 +95,14 @@ export class DynamicDsService<DS extends BaseDataSource = BaseDataSource, P exte
9495 }
9596 }
9697
98+ /**
99+ * Get all active (non-destroyed) dynamic datasources for a specific template.
100+ *
101+ * @param templateName - The name of the template to filter by
102+ * @returns Array of datasource info objects with global indices. The `index` field
103+ * represents the global position in the internal datasource array and should
104+ * be used when calling `destroyDynamicDatasource()`.
105+ */
97106 getDynamicDatasourcesByTemplate ( templateName : string ) : DynamicDatasourceInfo [ ] {
98107 if ( ! this . _datasourceParams ) {
99108 throw new Error ( 'DynamicDsService has not been initialized' ) ;
@@ -112,6 +121,19 @@ export class DynamicDsService<DS extends BaseDataSource = BaseDataSource, P exte
112121 } ) ) ;
113122 }
114123
124+ /**
125+ * Get datasource parameters by global index.
126+ *
127+ * @param index - Global index in the internal datasource parameters array
128+ * @returns DatasourceParams if found, undefined otherwise
129+ */
130+ getDatasourceParamByIndex ( index : number ) : DatasourceParams | undefined {
131+ if ( ! this . _datasourceParams || index < 0 || index >= this . _datasourceParams . length ) {
132+ return undefined ;
133+ }
134+ return this . _datasourceParams [ index ] ;
135+ }
136+
115137 async destroyDynamicDatasource (
116138 templateName : string ,
117139 currentBlockHeight : number ,
@@ -143,13 +165,17 @@ export class DynamicDsService<DS extends BaseDataSource = BaseDataSource, P exte
143165 throw new Error ( `Dynamic datasource at index ${ index } is already destroyed` ) ;
144166 }
145167
146- // Update the datasource
168+ // Update the datasource params
147169 const updatedParams = { ...dsParam , endBlock : currentBlockHeight } ;
148170 this . _datasourceParams [ index ] = updatedParams ;
149171
150- if ( this . _datasources [ index ] ) {
151- ( this . _datasources [ index ] as any ) . endBlock = currentBlockHeight ;
172+ // Update the datasource object if it exists
173+ // Note: _datasources and _datasourceParams arrays should always be in sync.
174+ // If the index is valid for params, it must also be valid for datasources.
175+ if ( ! this . _datasources [ index ] ) {
176+ throw new Error ( `Datasources array out of sync with params at index ${ index } ` ) ;
152177 }
178+ ( this . _datasources [ index ] as any ) . endBlock = currentBlockHeight ;
153179
154180 await this . metadata . set ( METADATA_KEY , this . _datasourceParams , tx ) ;
155181
0 commit comments