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

refactor: Port MPListenerController to Swift #320

Open
wants to merge 1 commit into
base: refactor/blackout
Choose a base branch
from

Conversation

BrandonStalnaker
Copy link
Contributor

Summary

  • Refacotr MPListenerController and MPListenerProtocol to Swift

Testing Plan

  • Tested using sample app

Reference Issue (For mParticle employees only. Ignore if you are an outside contributor)


import Foundation

@objc public enum MPEndpoint: Int {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's move these 2 enums and the MPListenerProtocol into a dedicated MPListenerProtocol.swift file

* @param sdkListener An instance of a class that implements the MPListenerProtocol
*/
@objc public func addSdkListener(_ sdkListener: any MPListenerProtocol) {
self.sdkListeners.append(sdkListener)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
self.sdkListeners.append(sdkListener)
sdkListeners.append(sdkListener)

Per style guide, let's get rid of these explicit self. calls in

* @param sdkListener An instance of a class that implements the MPListenerProtocol
*/
@objc public func removeSdkListener(_ sdkListener: any MPListenerProtocol) {
self.sdkListeners = self.sdkListeners.filter { sdkListener !== $0 }
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
self.sdkListeners = self.sdkListeners.filter { sdkListener !== $0 }
sdkListeners = sdkListeners.filter { sdkListener !== $0 }

Copy link
Contributor

Choose a reason for hiding this comment

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

Another thing, this actually has different behavior than the original Obj-C code due to the use of the !== identity equality operator. The original removeObject: call in Obj-C will use the isEqual: method to compare objects, which is essentially equivalent to == in Swift, but you're using the === form here (or well the negative !== form vs !=) which checks for address equality, i.e. exact same object.

In this case it may not matter, but was this intentional?

case identityLogin = 0, identityLogout, identityIdentify, identityModify, events, config, alias
}
@objc public enum MPDatabaseTable: Int {
case attributes = 0, breadcrumbs, messages, reporting, sessions, uploads, unknown
Copy link
Contributor

Choose a reason for hiding this comment

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

This style is fine for now, but let's discuss later if we want to use this style or enforce a more classic "every item on it's own line" style.


@objc public func onAPICalled(_ apiName: Selector, parameter1: NSObject?, parameter2: NSObject?, parameter3: NSObject?) {
for delegate in self.sdkListeners {
let stackTrace = [Thread.callStackSymbols]
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
let stackTrace = [Thread.callStackSymbols]
let stackTrace = Thread.callStackSymbols

This has different behavior from the original Obj-C. The Thread.callStackSymbols method returns an array ([String]) already, so this is putting an array inside another array.


@objc public func onAPICalled(_ apiName: Selector, parameter1: NSObject?, parameter2: NSObject?) {
for delegate in self.sdkListeners {
let stackTrace = [Thread.callStackSymbols]
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
let stackTrace = [Thread.callStackSymbols]
let stackTrace = Thread.callStackSymbols

Comment on lines +150 to +151
if parameter1 != nil {
parameters.append(parameter1!)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if parameter1 != nil {
parameters.append(parameter1!)
if let parameter1 = parameter1 {
parameters.append(parameter1)

All of these nil checks should be replaced with if let and all uses of the ! operator removed.

* @param isExternal true, if the call originated from outside of the SDK
* @param objects is the arguments sent to this api, such as the MPEvent in logEvent
*/
@objc optional func onAPICalled(_ apiName: Any!, stackTrace: Any!, isExternal: Any!, objects: Any!)
Copy link
Contributor

Choose a reason for hiding this comment

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

I assume the !'s here are to keep the same public interface as before?

}

@objc public func onAPICalled(_ apiName: Selector, parameter1: NSObject?, parameter2: NSObject?, parameter3: NSObject?) {
for delegate in self.sdkListeners {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
for delegate in self.sdkListeners {
for delegate in sdkListeners {

All of these explicit calls to self should be removed per the style guide

@einsteinx2
Copy link
Contributor

einsteinx2 commented Jan 6, 2025

Also I notice this doesn't seem to currently be used in any tests? We should add some before merging this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants