Skip to content

Commit b898bd1

Browse files
committed
Fix missing realtime broadcasts for step:started and step:completed events (#306)
## Summary Fixes critical bug where clients were not receiving `step:started` and `step:completed` realtime events. PostgreSQL's query optimizer was eliminating CTEs containing `realtime.send()` calls because they weren't referenced by subsequent operations. ## Root Cause PostgreSQL eliminates unreferenced CTEs during query optimization. The CTEs containing `realtime.send()` calls existed in the code but were optimized away because they weren't referenced by the final RETURN statement or subsequent operations. ## Solution Moved `realtime.send()` calls directly into RETURNING clauses of UPDATE statements, ensuring they execute atomically with state changes and cannot be optimized away. ## What's Fixed - `step:started` events now broadcast when steps begin executing - `step:completed` events now broadcast for empty map steps - `step:completed` events now broadcast for cascade completions - Dependent steps can now properly track when upstream steps start ## Changes ### Core Package (@pgflow/core) - `start_ready_steps()`: Broadcasts `step:started` and `step:completed` events in RETURNING clauses - `cascade_complete_taskless_steps()`: Broadcasts `step:completed` events atomically with cascade completion - `complete_task()`: Added PERFORM statements for `run:failed` and `step:failed` broadcasts ### Client Package (@pgflow/client) - Added `applySnapshot()` methods to FlowRun and FlowStep for proper initial state hydration without event emission - Enhanced test coverage for realtime event lifecycles with comprehensive matchers - Added edge case documentation for `waitForStatus()` with empty map steps and abort signals ### Testing - Added comprehensive realtime event lifecycle tests covering event emission, ordering, sequences, and payload validation - Added edge case tests for empty map steps and cascade completions ### Documentation - Updated abort signal support examples in `waitForStatus()` - Documented empty map step behavior (skip `step:started`, go directly to `step:completed`) - Added edge case documentation for `waitForStatus()` timing ## Breaking Changes None ## Migration This release includes database migrations. Users should follow the [update guide](/deploy/update-pgflow/) after upgrading packages.
1 parent c22a1e5 commit b898bd1

File tree

4 files changed

+88
-286
lines changed

4 files changed

+88
-286
lines changed

pkgs/core/supabase/tests/cascade_complete_taskless_steps/cascade-tests.md

Lines changed: 0 additions & 284 deletions
This file was deleted.

pkgs/website/src/content/docs/build/starting-flows/typescript-client.mdx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,46 @@ if (result.status === FlowRunStatus.Failed) {
221221
}
222222
```
223223

224+
## Canceling Wait Operations
225+
226+
Use `AbortSignal` to cancel waiting operations when needed:
227+
228+
```typescript
229+
const controller = new AbortController();
230+
231+
// Start waiting with abort signal
232+
const waitPromise = run.waitForStatus(FlowRunStatus.Completed, {
233+
timeoutMs: 60000,
234+
signal: controller.signal,
235+
});
236+
237+
// Cancel if user navigates away (browser example)
238+
window.addEventListener('beforeunload', () => {
239+
controller.abort();
240+
});
241+
242+
// Or cancel on user action
243+
document.getElementById('cancel-btn')?.addEventListener('click', () => {
244+
controller.abort();
245+
});
246+
247+
try {
248+
await waitPromise;
249+
console.log('Workflow completed:', run.output);
250+
} catch (error) {
251+
if (error.message.includes('Aborted')) {
252+
console.log('User canceled the operation');
253+
} else {
254+
throw error; // Re-throw other errors
255+
}
256+
}
257+
```
258+
259+
This is particularly useful in interactive applications where users might:
260+
- Navigate away before workflow completion
261+
- Cancel long-running operations
262+
- Switch between different workflows
263+
224264
## Resource Cleanup
225265

226266
:::caution
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
draft: false
3+
title: 'pgflow 0.7.2: Fix Missing Realtime Broadcasts for Step Events'
4+
description: 'Critical fix for missing step:started and step:completed realtime events'
5+
date: 2025-01-04
6+
authors:
7+
- jumski
8+
tags:
9+
- bugfix
10+
- realtime
11+
- client
12+
- core
13+
featured: true
14+
---
15+
16+
import { Aside } from "@astrojs/starlight/components";
17+
18+
pgflow 0.7.2 fixes missing realtime broadcasts that prevented clients from receiving `step:started` and `step:completed` events. PostgreSQL's query optimizer was eliminating CTEs containing `realtime.send()` calls because they weren't referenced by subsequent operations.
19+
20+
## What's Fixed
21+
22+
- `step:started` events now broadcast when steps begin executing
23+
- `step:completed` events now broadcast for empty map steps
24+
- `step:completed` events now broadcast for cascade completions
25+
- Client applySnapshot() methods added for proper initial state hydration without event emission
26+
27+
## Additional Changes
28+
29+
- Enhanced test coverage for realtime event lifecycles
30+
- Documentation updates for abort signal support and empty map step behavior
31+
32+
## Upgrading
33+
34+
<Aside type="note" title="Migration Required">
35+
This release includes database migrations. See the [update guide](/deploy/update-pgflow/) for instructions.
36+
</Aside>
37+
38+
---
39+
40+
**Questions or issues?** Join our [Discord community](https://www.pgflow.dev/discord/) or [open an issue on GitHub](https://github.com/pgflow-dev/pgflow/issues).

0 commit comments

Comments
 (0)