( count: number, dataType: DataType, onProgress?: (progress: number) => void )
| 265 | // ============================================================================ |
| 266 | |
| 267 | async function generateData( |
| 268 | count: number, |
| 269 | dataType: DataType, |
| 270 | onProgress?: (progress: number) => void |
| 271 | ): Promise<DataPoint[] | OHLCDataPoint[]> { |
| 272 | const chunkSize = 100000; // Generate in chunks to avoid blocking |
| 273 | const chunks: Array<DataPoint[] | OHLCDataPoint[]> = []; |
| 274 | |
| 275 | for (let i = 0; i < count; i += chunkSize) { |
| 276 | const currentChunkSize = Math.min(chunkSize, count - i); |
| 277 | |
| 278 | // Generate chunk |
| 279 | const chunk = generateChunk(i, currentChunkSize, dataType); |
| 280 | chunks.push(chunk); |
| 281 | |
| 282 | // Update progress |
| 283 | if (onProgress) { |
| 284 | onProgress((i + currentChunkSize) / count); |
| 285 | } |
| 286 | |
| 287 | // Yield to event loop every chunk |
| 288 | if (i + chunkSize < count) { |
| 289 | await new Promise(resolve => setTimeout(resolve, 0)); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | // Type assertion is safe because all chunks are guaranteed to be the same type |
| 294 | return chunks.flat() as DataPoint[] | OHLCDataPoint[]; |
| 295 | } |
| 296 | |
| 297 | function generateChunk( |
| 298 | startIndex: number, |
no test coverage detected