damus icon indicating copy to clipboard operation
damus copied to clipboard

refinements to RelayConnection plus tests for creating requests

Open bryanmontz opened this issue 2 years ago • 5 comments

Added some polish to the RelayConnection class:

  1. 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).
  2. 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.
  3. Made socket optional to avoid wasting memory resources.
  4. Used the TimeInterval type for times. It's an alias for Double and matches Apple's APIs more accurately.
  5. Removed all unnecessary self.s for property and function accessing.
  6. Made use of Swift's excellent protocol extension feature to isolate the functions that convert NostrRequests into Strings from the RelayConnection 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.
  7. Added tests for the same functions to demonstrate another benefit of the protocol, which the test class can also conform to for access.

bryanmontz avatar Feb 18 '23 06:02 bryanmontz

Made socket optional to avoid wasting memory resources

don't we always need socket?

jb55 avatar Feb 18 '23 16:02 jb55

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.

bryanmontz avatar Feb 18 '23 17:02 bryanmontz

hmm ok I see what you mean now... that commit looks better.

jb55 avatar Feb 18 '23 17:02 jb55

need to make sure that doesn't break anything

jb55 avatar Feb 18 '23 17:02 jb55

Right. Super critical code. I'd love for it to be as clean as possible and have tests.

bryanmontz avatar Feb 18 '23 17:02 bryanmontz

thanks!

jb55 avatar Feb 19 '23 16:02 jb55

seems to be working ok!

jb55 avatar Feb 19 '23 16:02 jb55