* Write the current partial message to disk (throttled by mtime) * Ensures writes happen during rapid streaming (crash-resilient)
(
workspaceId: WorkspaceId,
streamInfo: WorkspaceStreamInfo
)
| 729 | * Ensures writes happen during rapid streaming (crash-resilient) |
| 730 | */ |
| 731 | private async schedulePartialWrite( |
| 732 | workspaceId: WorkspaceId, |
| 733 | streamInfo: WorkspaceStreamInfo |
| 734 | ): Promise<void> { |
| 735 | const now = Date.now(); |
| 736 | const timeSinceLastWrite = now - streamInfo.lastPartialWriteTime; |
| 737 | |
| 738 | // If enough time has passed, write immediately |
| 739 | if (timeSinceLastWrite >= this.PARTIAL_WRITE_THROTTLE_MS) { |
| 740 | await this.flushPartialWrite(workspaceId, streamInfo); |
| 741 | return; |
| 742 | } |
| 743 | |
| 744 | // Otherwise, schedule write for remaining time (fire-and-forget for scheduled writes) |
| 745 | if (streamInfo.partialWriteTimer) { |
| 746 | clearTimeout(streamInfo.partialWriteTimer); |
| 747 | } |
| 748 | |
| 749 | const remainingTime = this.PARTIAL_WRITE_THROTTLE_MS - timeSinceLastWrite; |
| 750 | streamInfo.partialWriteTimer = setTimeout(() => { |
| 751 | void this.flushPartialWrite(workspaceId, streamInfo); |
| 752 | }, remainingTime); |
| 753 | } |
| 754 | |
| 755 | private async awaitPendingPartialWrite(streamInfo: WorkspaceStreamInfo): Promise<void> { |
| 756 | if (streamInfo.partialWritePromise) { |
no test coverage detected