$since_h; $changed_vendors = []; foreach ($cur_m as $vid => $maxid) { if ($maxid > ($since_m[$vid] ?? 0)) $changed_vendors[] = $vid; } if ($history_changed || $changed_vendors) { echo json_encode([ 'history' => $cur_h, 'messages' => $cur_m, 'history_changed' => $history_changed, 'changed_vendors' => $changed_vendors, ]); return; } if (microtime(true) >= $deadline) { echo json_encode([ 'history' => $cur_h, 'messages' => $cur_m, 'timeout' => true, ]); return; } // Bail early if the client has gone away (cheap check — flush updates the // connection state). Otherwise sleep 1s and loop. if (connection_aborted()) return; usleep(1000000); } function events_snapshot(PDO $pdo, ?int $vendor_filter): array { if ($vendor_filter === null) { $h = (int) $pdo->query('SELECT COALESCE(MAX(id), 0) FROM job_history')->fetchColumn(); $m = []; foreach ($pdo->query('SELECT vendor_id, COALESCE(MAX(id), 0) AS maxid FROM messages GROUP BY vendor_id')->fetchAll() as $r) { $m[(int) $r['vendor_id']] = (int) $r['maxid']; } // Vendors with zero messages get a 0 entry so the client tracks them. foreach ($pdo->query('SELECT id FROM vendors WHERE active = 1')->fetchAll() as $v) { if (!isset($m[(int) $v['id']])) $m[(int) $v['id']] = 0; } } else { $stmt = $pdo->prepare( 'SELECT COALESCE(MAX(h.id), 0) FROM job_history h JOIN jobs j ON j.id = h.job_id WHERE j.vendor_id = ?' ); $stmt->execute([$vendor_filter]); $h = (int) $stmt->fetchColumn(); $stmt = $pdo->prepare('SELECT COALESCE(MAX(id), 0) FROM messages WHERE vendor_id = ?'); $stmt->execute([$vendor_filter]); $m = [$vendor_filter => (int) $stmt->fetchColumn()]; } return [$h, $m]; }