Skip to content

Commit cd60d99

Browse files
committed
feat: improved jsdoc and also introduced a new public method
1 parent af7339f commit cd60d99

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

packages/node-core/src/indexer/dynamic-ds.service.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

packages/node-core/src/indexer/indexer.manager.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,21 @@ export abstract class BaseIndexerManager<
127127

128128
// Remove the destroyed datasource from current processing
129129
// The datasource at the global index now has an endBlock set
130-
const destroyedDsParam = (this.dynamicDsService as any)._datasourceParams[index];
130+
const destroyedDsParam = this.dynamicDsService.getDatasourceParamByIndex(index);
131+
132+
if (!destroyedDsParam) {
133+
logger.warn(`Unable to filter destroyed datasource at index ${index} - params not found`);
134+
return;
135+
}
131136

132137
// Filter out the destroyed datasource by matching startBlock and args
138+
// Note: Reassigning filteredDataSources is intentional - subsequent handlers
139+
// within the same block will see the updated filtered list
133140
filteredDataSources = filteredDataSources.filter((fds) => {
134141
const fdsStartBlock = (fds as any).startBlock;
135-
const fdsArgs = JSON.stringify((fds as any).options || {});
142+
// For custom datasources, args are stored in processor.options
143+
// For runtime datasources, they may be stored differently
144+
const fdsArgs = JSON.stringify((fds as any).processor?.options || (fds as any).options || {});
136145
const paramArgs = JSON.stringify(destroyedDsParam.args || {});
137146

138147
// Keep datasource if it doesn't match the destroyed one

packages/types-core/src/interfaces.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,26 @@
44
export type DynamicDatasourceCreator = (name: string, args: Record<string, unknown>) => Promise<void>;
55
export type DynamicDatasourceDestructor = (name: string, index: number) => Promise<void>;
66

7+
/**
8+
* Information about a dynamic datasource instance.
9+
*/
710
export interface DynamicDatasourceInfo {
11+
/**
12+
* Global index of the datasource in the internal storage array.
13+
* Use this value when calling destroyDynamicDatasource().
14+
*/
815
index: number;
16+
/** Template name this datasource was created from */
917
templateName: string;
18+
/** Block height where this datasource starts processing */
1019
startBlock: number;
20+
/** Block height where this datasource stops processing (if destroyed) */
1121
endBlock?: number;
22+
/** Arguments passed when creating this datasource */
1223
args?: Record<string, unknown>;
1324
}
1425

15-
export type DynamicDatasourceGetter = (templateName: string) => Promise<DynamicDatasourceInfo[]>;
26+
export type DynamicDatasourceGetter = (templateName: string) => DynamicDatasourceInfo[];
1627

1728
export interface Cache<T extends Record<string, any> = Record<string, any>> {
1829
set(key: keyof T, value: T[keyof T]): Promise<void>;

0 commit comments

Comments
 (0)