damus
damus copied to clipboard
refinements to RelayConnection plus tests for creating requests
Added some polish to the RelayConnection
class:
- Used
final class
to explicitly denote that this class is not intended to be subclassed (helps with compile time and communicates clear intent to all readers). - Marked all properties and functions with the tightest possible access controls, to make future debugging easier and simplify the external interface. This includes using
private(set)
to indicate a property that is read-write to the owning type and read-only to outside types. - Made
socket
optional to avoid wasting memory resources. - Used the
TimeInterval
type for times. It's an alias forDouble
and matches Apple's APIs more accurately. - Removed all unnecessary
self.
s for property and function accessing. - Made use of Swift's excellent protocol extension feature to isolate the functions that convert
NostrRequest
s intoString
s from theRelayConnection
class itself. This also removes the functions from the global namespace to prevent Xcode from suggesting them to any type that doesn't need them. Using them now requires explicit conformance to the protocol. - Added tests for the same functions to demonstrate another benefit of the protocol, which the test class can also conform to for access.
Made socket optional to avoid wasting memory resources
don't we always need socket?
Yes, but the problem was that it was creating and throwing away a socket every time a RelayConnection was allocated. I thought of a better way that eliminates the optionality and doesn't throw away a socket. Will send another commit.
hmm ok I see what you mean now... that commit looks better.
need to make sure that doesn't break anything
Right. Super critical code. I'd love for it to be as clean as possible and have tests.
thanks!
seems to be working ok!