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

Add public api to register global listener #451

Merged
merged 8 commits into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions AEPCore/Sources/core/MobileCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ public final class MobileCore: NSObject {
EventHub.shared.dispatch(event: event)
}

/// Registers an `EventListener` which will be invoked whenever a event with matched type and source is dispatched
/// - Parameters:
/// - type: An `String` indicates the event type the current listener is listening for
Copy link
Member

Choose a reason for hiding this comment

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

An String indicates > A String indicating

/// - source: An `String` indicates the event source the current listener is listening for
Copy link
Member

Choose a reason for hiding this comment

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

same here

/// - listener: An `EventResponseListener` which will be invoked whenever the `EventHub` receives a event with matched typd and source
Copy link
Member

Choose a reason for hiding this comment

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

typd > type

@objc(registerEventListenerWithType:source:listener:)
public static func registerEventListener(type: String, source: String, listener: @escaping EventListener) {
EventHub.shared.registerEventListener(type: type, source: source, listener: listener)
}

/// Submits a generic event containing the provided IDFA with event type `generic.identity`.
/// - Parameter identifier: the advertising identifier string.
@objc(setAdvertisingIdentifier:)
Expand Down
4 changes: 4 additions & 0 deletions AEPCore/Sources/eventhub/AEPError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@ import Foundation
case callbackTimeout = 1
case callbackNil = 2
case none = 3
case serverError = 4
Copy link
Contributor

Choose a reason for hiding this comment

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

Are these an artifact from some other work? I don't see them used anywhere in this PR.

case networkError = 5
case invalidRequest = 6
case invalidResponse = 7
case errorExtensionNotInitialized = 11
}
17 changes: 17 additions & 0 deletions AEPCore/Sources/eventhub/EventHub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,23 @@ final class EventHub {
responseEventListeners.append(responseListenerContainer!)
}

/// Registers an `EventListener` which will be invoked whenever a event with matched type and source is dispatched
/// - Parameters:
/// - type: An `String` indicates the event type the current listener is listening for
Copy link
Member

Choose a reason for hiding this comment

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

same fixes needed for copy/pasted docs

/// - source: An `String` indicates the event source the current listener is listening for
/// - listener: An `EventResponseListener` which will be invoked whenever the `EventHub` receives a event with matched typd and source
func registerEventListener(type: String, source: String, listener: @escaping EventListener) {

Copy link
Contributor

Choose a reason for hiding this comment

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

nit: remove blank line.

eventHubQueue.async {
// use the event hub placeholder extension to hold all the listeners register from the public API
Copy link
Member

Choose a reason for hiding this comment

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

register > registered

guard let eventHubExtension = self.registeredExtensions.first(where: { $1.sharedStateName == EventHubConstants.NAME })?.value else {
Log.warning(label: self.LOG_TAG, "Error registering event listener")
return
}
eventHubExtension.registerListener(type: type, source: source, listener: listener)
}
}

/// Creates a new `SharedState` for the extension with provided data, versioned at `event`
/// If `event` is nil, one of two behaviors will be observed:
/// 1. If this extension has not previously published a shared state, shared state will be versioned at 0
Expand Down
1 change: 1 addition & 0 deletions AEPCore/Sources/eventhub/EventSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class EventSource: NSObject {
public static let requestIdentity = "com.adobe.eventSource.requestIdentity"
public static let requestProfile = "com.adobe.eventSource.requestProfile"
public static let requestReset = "com.adobe.eventSource.requestReset"
public static let notification = "com.adobe.eventSource.notification"
Copy link
Contributor

Choose a reason for hiding this comment

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

Unrelated to this PR?

public static let responseContent = "com.adobe.eventSource.responseContent"
public static let responseIdentity = "com.adobe.eventSource.responseIdentity"
public static let responseProfile = "com.adobe.eventSource.responseProfile"
Expand Down
1 change: 1 addition & 0 deletions AEPCore/Sources/eventhub/EventType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class EventType: NSObject {
public static let userProfile = "com.adobe.eventType.userProfile"
public static let places = "com.adobe.eventType.places"
public static let offerDecisioning = "com.adobe.eventType.offerDecisioning"
public static let edge = "com.adobe.eventType.edge"
Copy link
Contributor

Choose a reason for hiding this comment

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

Unrelated?

public static let genericTrack = "com.adobe.eventType.generic.track"
public static let genericLifecycle = "com.adobe.eventType.generic.lifecycle"
public static let genericIdentity = "com.adobe.eventType.generic.identity"
Expand Down
36 changes: 36 additions & 0 deletions AEPCore/Tests/EventHubTests/EventHubTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,42 @@ class EventHubTests: XCTestCase {
wait(for: [expectation, expectation1], timeout: 1)
}

func testEventHubTestTegisterEventListener() {
// setup
let expectation = XCTestExpectation(description: "Listener is invoked exactly once")
expectation.assertForOverFulfill = true
let event = Event(name: "Test", type: EventType.analytics, source: EventSource.requestContent, data: nil)

// test
eventHub.registerEventListener(type: EventType.analytics, source: EventSource.requestContent) { event in
expectation.fulfill()
}

eventHub.start()
eventHub.dispatch(event: event)

// verify
wait(for: [expectation], timeout: 1)
}

func testEventHubTestTegisterEventListenerUnMatchedEvent() {
// setup
let expectation = XCTestExpectation(description: "Listener is not invoked")
expectation.isInverted = true
let event = Event(name: "Test", type: "wrong", source: "wrong", data: nil)

// test
eventHub.registerEventListener(type: EventType.analytics, source: EventSource.requestContent) { event in
expectation.fulfill()
}

eventHub.start()
eventHub.dispatch(event: event)

// verify
wait(for: [expectation], timeout: 0.5)
}

func testEventHubTestResponseListener() {
// setup
let expectation = XCTestExpectation(description: "Response listener is invoked exactly once")
Expand Down
52 changes: 52 additions & 0 deletions AEPCore/Tests/MobileCoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,58 @@ class MobileCoreTests: XCTestCase {
wait(for: [responseExpectation], timeout: 1.0)
}

/// Tests that the event listener only receive the events it is registered for
func testRegisterEventListener() {
// setup
let expectedEvent1 = Event(name: "test", type: EventType.analytics, source: EventSource.requestContent, data: nil)
let expectedEvent2 = Event(name: "test", type: EventType.analytics, source: EventSource.requestContent, data: nil)
let unexpectedEvent = Event(name: "test", type: "wrong", source: "wrong", data: nil)
let responseExpectation = XCTestExpectation(description: "Should receive the event")
responseExpectation.expectedFulfillmentCount = 2
EventHub.shared.start()

// test
MobileCore.registerEventListener(type: EventType.analytics, source: EventSource.requestContent) { event in
responseExpectation.fulfill()
}
// dispatch the events
MobileCore.dispatch(event: expectedEvent1)
MobileCore.dispatch(event: unexpectedEvent)
MobileCore.dispatch(event: expectedEvent2)

// verify
wait(for: [responseExpectation], timeout: 1.0)
}

/// Tests that the event listeners listening for same events can all receives the events
func testRegisterEventListenerMultipleLisenersForSameEvents() {
// setup
let expectedEvent1 = Event(name: "test", type: EventType.analytics, source: EventSource.requestContent, data: nil)
let expectedEvent2 = Event(name: "test", type: EventType.analytics, source: EventSource.requestContent, data: nil)
let unexpectedEvent = Event(name: "test", type: "wrong", source: "wrong", data: nil)
let responseExpectation1 = XCTestExpectation(description: "Should receive the events")
responseExpectation1.expectedFulfillmentCount = 2
let responseExpectation2 = XCTestExpectation(description: "Should receive the events")
responseExpectation2.expectedFulfillmentCount = 2
EventHub.shared.start()

// test
MobileCore.registerEventListener(type: EventType.analytics, source: EventSource.requestContent) { event in
responseExpectation1.fulfill()
}

MobileCore.registerEventListener(type: EventType.analytics, source: EventSource.requestContent) { event in
responseExpectation2.fulfill()
}
// dispatch the events
MobileCore.dispatch(event: expectedEvent1)
MobileCore.dispatch(event: unexpectedEvent)
MobileCore.dispatch(event: expectedEvent2)

// verify
wait(for: [responseExpectation1,responseExpectation2], timeout: 1.0)
}

// MARK: setWrapperType(...) tests

/// No wrapper tag should be appended when the setWrapperType API is never invoked
Expand Down