Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace duplicated cascades of case statements with utility methods on State #5982

Merged

Conversation

acb-mv
Copy link
Contributor

@acb-mv acb-mv commented Mar 19, 2024

This replaces recurring cascades of case statements that mutate State's associated values with one utility method on State: mutateAssociatedData, which take a function that might modify the appropriate associated value, and if the current state has such a value, applies it and returns whether a modification took place. The function is given a StateAssociatedData type, which is a protocol encompassing both the connection and blocked state data's common fields.

The benefit of this change is increased separation of concerns: code that cares only about the connection/error state no longer needs to know which states exist, which will make changing the logic of the state machine simpler, as there will be fewer places where changes are needed.


This change is Reviewable

@acb-mv acb-mv added the iOS Issues related to iOS label Mar 19, 2024
@acb-mv acb-mv requested a review from buggmagnet March 19, 2024 16:39
@acb-mv acb-mv self-assigned this Mar 19, 2024
@buggmagnet buggmagnet force-pushed the features/ios-post-quantum branch from e649074 to ea48edf Compare March 20, 2024 10:28
@acb-mv acb-mv force-pushed the PacketTunnel-State-mutation-refactor branch 3 times, most recently from e7e0b1d to cf18096 Compare March 21, 2024 09:28
@acb-mv acb-mv marked this pull request as ready for review March 21, 2024 09:50
@buggmagnet buggmagnet force-pushed the features/ios-post-quantum branch from 230370b to e0809f5 Compare March 21, 2024 12:20
@buggmagnet buggmagnet requested a review from rablador March 21, 2024 12:21
@acb-mv acb-mv force-pushed the PacketTunnel-State-mutation-refactor branch from 68ad708 to 7fe698f Compare March 21, 2024 13:22
@acb-mv acb-mv force-pushed the PacketTunnel-State-mutation-refactor branch from 7fe698f to 213c112 Compare March 21, 2024 13:51
Copy link
Contributor

@buggmagnet buggmagnet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 7 of 7 files at r2.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @acb-mv)


ios/PacketTunnelCore/Actor/State.swift line 99 at r2 (raw file):

}

// perhaps this should have a better name?

nit
This looks fine, but we should (ideally) be able to understand where it's used from the name alone.
We could rename it ConnectionStateAssociatedData for example, although that might feel a bit redundant.


ios/PacketTunnelCore/Actor/State+Extensions.swift line 131 at r2 (raw file):

            var associatedData: StateAssociatedData = connState
            modifier(&associatedData)
            self = self.replacingConnectionData(with: associatedData as! ConnectionData)

It's a bit annoying that we have to do this, but at least we should remove the swiftlint warning here and down in the blocked state.

Let's add a comment that specifices that both ConnectionData and BlockingData do implement StateAssociatedData so the cast is guaranteed to work.

Copy link
Contributor Author

@acb-mv acb-mv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @buggmagnet)


ios/PacketTunnelCore/Actor/State.swift line 99 at r2 (raw file):

Previously, buggmagnet wrote…

nit
This looks fine, but we should (ideally) be able to understand where it's used from the name alone.
We could rename it ConnectionStateAssociatedData for example, although that might feel a bit redundant.

Except that this is not specific to a connection state, it can also apply to a blockage/error state. While the type this belongs to is named State and protocols cannot live under namespaces, I'm using State as a prefix for the protocol name. Perhaps in future, it may do to move State under PacketTunnelActor and call it PacketTunnelActorStateAssociatedData or some abbreviation thereof (PacketTunnelActorStateData?)

Another option would be to make the name an adjective, which is something that goes well with protocols, though I haven't thought of a good one (StateDataBearing perhaps? Though that sounds awkward).

In any case, the comment is a leftover from before I renamed it and the name was worse, and I can delete it.


ios/PacketTunnelCore/Actor/State+Extensions.swift line 131 at r2 (raw file):

Previously, buggmagnet wrote…

It's a bit annoying that we have to do this, but at least we should remove the swiftlint warning here and down in the blocked state.

Let's add a comment that specifices that both ConnectionData and BlockingData do implement StateAssociatedData so the cast is guaranteed to work.

Is there a format for this comment that will shut swiftlint up, or is this just for human consumption?

@acb-mv
Copy link
Contributor Author

acb-mv commented Mar 21, 2024

ios/PacketTunnelCore/Actor/State+Extensions.swift line 131 at r2 (raw file):

Previously, acb-mv wrote…

Is there a format for this comment that will shut swiftlint up, or is this just for human consumption?

done

Copy link
Contributor

@buggmagnet buggmagnet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewed 2 of 2 files at r4, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved

@buggmagnet buggmagnet merged commit 0092b1b into features/ios-post-quantum Mar 21, 2024
6 of 7 checks passed
@buggmagnet buggmagnet deleted the PacketTunnel-State-mutation-refactor branch March 21, 2024 14:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
iOS Issues related to iOS
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants