[PR #40] [MERGED] refactor(ws): extract reifyFrame helper for duplicated frame reification #42

Closed
opened 2026-10-26 22:34:18 +00:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Comfy-Org/comfy-swift-sdk/pull/40
Author: @mattmillerai
Created: 7/11/2026
Status: ✅ Merged
Merged: 7/21/2026
Merged by: @mattmillerai

Base: main ← Head: matt/be-2860-reify-frame-helper


📝 Commits (3)

  • a3ef60f refactor(ws): extract reifyFrame helper for duplicated frame reification
  • f15ac0f fix(ws): guard reifyFrame against top-level JSON scalar bodies
  • b29f9f9 test(ws): cover reifyFrame scalar-body guard against uncatchable crash

📊 Changes

2 files changed (+94 additions, -12 deletions)

View changed files

📝 Sources/ComfySwiftSDK/Internal/WebSocketSession.swift (+19 -12)
➕ Tests/ComfySwiftSDKTests/ReifyFrameTests.swift (+75 -0)

📄 Description

ELI-5

Two spots in the WebSocket read loop did the exact same little dance: take a
frame body that had been decoded into an untyped Any, turn it back into JSON
bytes, then decode those bytes into a real typed struct. This PR pulls that
repeated dance into one small helper (reifyFrame) and calls it from both
spots, so the round-trip logic lives in one place instead of two.

What changed

  • Added private static func reifyFrame<T: Decodable>(_ raw: AnyDecodable?) -> T?
    to WebSocketSession. It unwraps the frame body, round-trips it through
    JSONSerialization + JSONDecoder, and returns nil on any failure.
  • Rewired the executed (ExecutedFrameData) and execution_error
    (ExecutionErrorFrameData) frame handlers to call it, replacing the two
    copies of the inline round-trip.

Why

The two handlers each open-coded the same envelope.data?.value →
JSONSerialization.data(withJSONObject:) → JSONDecoder().decode(T.self)
reification. Extracting it removes the duplication and gives the round-trip a
single named home.

Behavior-preserving

This is a pure refactor. The helper reproduces the original try?-fallback
semantics exactly: absent body or any failed step yields nil, and each caller
keeps its existing if let fallback behavior. The runReadLoop state machine
and its local buffers are left untouched (the broader "decompose the switch"
idea was intentionally out of scope — the loop stays the single owner of its
buffers).

Testing

  • swift build clean.
  • Full test suite green: 226 tests in 28 suites passed. The WS frame-decode
    paths are exercised via the integration/smoke suites.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Comfy-Org/comfy-swift-sdk/pull/40 **Author:** [@mattmillerai](https://github.com/mattmillerai) **Created:** 7/11/2026 **Status:** ✅ Merged **Merged:** 7/21/2026 **Merged by:** [@mattmillerai](https://github.com/mattmillerai) **Base:** `main` ← **Head:** `matt/be-2860-reify-frame-helper` --- ### 📝 Commits (3) - [`a3ef60f`](https://github.com/Comfy-Org/comfy-swift-sdk/commit/a3ef60f7371f5810e69bfc55ffc6e42ab8c54374) refactor(ws): extract reifyFrame helper for duplicated frame reification - [`f15ac0f`](https://github.com/Comfy-Org/comfy-swift-sdk/commit/f15ac0fee11190e282d24b826722a4c1d919fbc1) fix(ws): guard reifyFrame against top-level JSON scalar bodies - [`b29f9f9`](https://github.com/Comfy-Org/comfy-swift-sdk/commit/b29f9f998c1e34564f4bcbbe1a78b81ef382dd5a) test(ws): cover reifyFrame scalar-body guard against uncatchable crash ### 📊 Changes **2 files changed** (+94 additions, -12 deletions) <details> <summary>View changed files</summary> 📝 `Sources/ComfySwiftSDK/Internal/WebSocketSession.swift` (+19 -12) ➕ `Tests/ComfySwiftSDKTests/ReifyFrameTests.swift` (+75 -0) </details> ### 📄 Description ## ELI-5 Two spots in the WebSocket read loop did the exact same little dance: take a frame body that had been decoded into an untyped `Any`, turn it back into JSON bytes, then decode those bytes into a real typed struct. This PR pulls that repeated dance into one small helper (`reifyFrame`) and calls it from both spots, so the round-trip logic lives in one place instead of two. ## What changed - Added `private static func reifyFrame<T: Decodable>(_ raw: AnyDecodable?) -> T?` to `WebSocketSession`. It unwraps the frame body, round-trips it through `JSONSerialization` + `JSONDecoder`, and returns `nil` on any failure. - Rewired the `executed` (`ExecutedFrameData`) and `execution_error` (`ExecutionErrorFrameData`) frame handlers to call it, replacing the two copies of the inline round-trip. ## Why The two handlers each open-coded the same `envelope.data?.value` → `JSONSerialization.data(withJSONObject:)` → `JSONDecoder().decode(T.self)` reification. Extracting it removes the duplication and gives the round-trip a single named home. ## Behavior-preserving This is a pure refactor. The helper reproduces the original `try?`-fallback semantics exactly: absent body or any failed step yields `nil`, and each caller keeps its existing `if let` fallback behavior. The `runReadLoop` state machine and its local buffers are left untouched (the broader "decompose the switch" idea was intentionally out of scope — the loop stays the single owner of its buffers). ## Testing - `swift build` clean. - Full test suite green: 226 tests in 28 suites passed. The WS frame-decode paths are exercised via the integration/smoke suites. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-26 22:34:18 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Comfy-Org/comfy-swift-sdk#42
No description provided.