11import { Injectable , Logger } from "@nestjs/common" ;
2- import { Interval } from "@nestjs/schedule" ;
32import {
43 FlowBlock ,
54 FlowCollection ,
@@ -43,6 +42,7 @@ import {
4342} from "../../processes/managed-process.entity" ;
4443import { CommonService } from "../../core/services/common.service" ;
4544import { FlowEmulatorService } from "./emulator.service" ;
45+ import { AsyncIntervalScheduler } from "../../core/async-interval-scheduler" ;
4646
4747type BlockData = {
4848 block : FlowBlock ;
@@ -70,6 +70,8 @@ export class FlowAggregatorService implements ProjectContextLifecycle {
7070 private projectContext : ProjectEntity | undefined ;
7171 private emulatorProcess : ManagedProcessEntity | undefined ;
7272 private readonly logger = new Logger ( FlowAggregatorService . name ) ;
73+ private readonly processingIntervalMs = 500 ;
74+ private processingScheduler : AsyncIntervalScheduler ;
7375
7476 constructor (
7577 private blockService : BlocksService ,
@@ -85,9 +87,16 @@ export class FlowAggregatorService implements ProjectContextLifecycle {
8587 private configService : FlowConfigService ,
8688 private processManagerService : ProcessManagerService ,
8789 private commonService : CommonService
88- ) { }
90+ ) {
91+ this . processingScheduler = new AsyncIntervalScheduler ( {
92+ name : "Blockchain processing" ,
93+ intervalInMs : this . processingIntervalMs ,
94+ functionToExecute : this . processBlockchainData . bind ( this ) ,
95+ } ) ;
96+ }
8997
9098 onEnterProjectContext ( project : ProjectEntity ) : void {
99+ this . processingScheduler ?. start ( ) ;
91100 this . projectContext = project ;
92101 this . emulatorProcess = this . processManagerService . get (
93102 FlowEmulatorService . processId
@@ -103,6 +112,7 @@ export class FlowAggregatorService implements ProjectContextLifecycle {
103112 }
104113
105114 onExitProjectContext ( ) : void {
115+ this . processingScheduler ?. stop ( ) ;
106116 this . projectContext = undefined ;
107117 this . processManagerService . removeListener (
108118 ProcessManagerEvent . PROCESS_ADDED ,
@@ -151,9 +161,7 @@ export class FlowAggregatorService implements ProjectContextLifecycle {
151161 return latestUnprocessedBlockHeight - ( nextBlockHeightToProcess - 1 ) ;
152162 }
153163
154- // TODO(milestone-x): Next interval shouldn't start before this function resolves
155- @Interval ( 1000 )
156- async fetchDataFromDataSource ( ) : Promise < void > {
164+ async processBlockchainData ( ) : Promise < void > {
157165 if ( ! this . projectContext ) {
158166 return ;
159167 }
0 commit comments