Skip to content

Commit

Permalink
fix: Objective-C support for plugin flush() (#87)
Browse files Browse the repository at this point in the history
* fix: Objective-C support for plugin flush()

* fix: fix example TroubleShootingPlugin
  • Loading branch information
falconandy authored Sep 29, 2023
1 parent 64c8a4c commit 726e3e8
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
return event;
}]];

NSMutableArray<AMPBaseEvent*>* collectedEvents = [NSMutableArray array];
[amplitude add:[AMPPlugin initWithType:AMPPluginTypeDestination execute:^AMPBaseEvent* _Nullable(AMPBaseEvent* _Nonnull event) {
[collectedEvents addObject:event];
return nil;
} flush:^() {
NSLog(@"Plugin Flush: %lu events", (unsigned long)collectedEvents.count);
[collectedEvents removeAllObjects];
}]];

[amplitude setUserId:@"User-ObjC"];

AMPIdentify* identify = [AMPIdentify new];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,36 @@ - (void)testPlugin {
XCTAssertEqualObjects(@34, [events[0] objectForKey:@"location_lng"]);
}

- (void)testDestinationPlugin {
XCTestExpectation* expectation = [self expectationWithDescription:@"flush"];
NSMutableArray<AMPBaseEvent*>* collectedEvents = [NSMutableArray array];

Amplitude* amplitude = [self getAmplitude:@"plugin"];
[amplitude add:[AMPPlugin initWithType:AMPPluginTypeDestination execute:^AMPBaseEvent* _Nullable(AMPBaseEvent* _Nonnull event) {
[collectedEvents addObject:event];
return nil;
} flush:^() {
[expectation fulfill];
}]];

[amplitude track:@"Event-A" eventProperties:nil];
[amplitude track:@"Event-B" eventProperties:nil];
[amplitude track:@"Event-C" eventProperties:nil];

[amplitude flush];

[self waitForExpectationsWithTimeout:5.0 handler:^(NSError* error) {
if (error) {
XCTFail("Expectation failed with error: %@", error);
}
}];

XCTAssertEqual(collectedEvents.count, 3);
XCTAssertEqualObjects(@"Event-A", [collectedEvents objectAtIndex:0].eventType);
XCTAssertEqualObjects(@"Event-B", [collectedEvents objectAtIndex:1].eventType);
XCTAssertEqualObjects(@"Event-C", [collectedEvents objectAtIndex:2].eventType);
}

- (Amplitude *)getAmplitude:(NSString *)instancePrefix {
NSString* instanceName = [NSString stringWithFormat:@"%@-%f", instancePrefix, [[NSDate date] timeIntervalSince1970]];
AMPConfiguration* configuration = [AMPConfiguration initWithApiKey:@"API-KEY" instanceName:instanceName];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class TroubleShootingPlugin: DestinationPlugin {
let serverZone = amplitude.configuration.serverZone.rawValue;
let serverUrl = amplitude.configuration.serverUrl ?? "null";

self.amplitude?.logger?.debug(message: "Current Configuration : {\"apiKey\": "+apiKey+", \"serverZone\": "+serverZone.rawValue+", \"serverUrl\": "+serverUrl+"}")
self.amplitude?.logger?.debug(message: "Current Configuration : {\"apiKey\": "+apiKey+", \"serverZone\": "+serverZone+", \"serverUrl\": "+serverUrl+"}")
}

open override func track(event: BaseEvent) -> BaseEvent? {
Expand Down
69 changes: 68 additions & 1 deletion Sources/Amplitude/ObjC/ObjCPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public class ObjCPlugin: NSObject {
internal let type: PluginType
internal let setup: ((ObjCAmplitude) -> Void)?
internal let execute: (ObjCBaseEvent) -> ObjCBaseEvent?
internal let flush: (() -> Void)?

@objc(initWithType:setup:execute:)
public static func initWithType(
Expand All @@ -23,6 +24,25 @@ public class ObjCPlugin: NSObject {
ObjCPlugin(type: type, execute: execute)
}

@objc(initWithType:setup:execute:flush:)
public static func initWithType(
type: PluginType,
setup: @escaping (ObjCAmplitude) -> Void,
execute: @escaping (ObjCBaseEvent) -> ObjCBaseEvent?,
flush: @escaping () -> Void
) -> ObjCPlugin {
ObjCPlugin(type: type, setup: setup, execute: execute, flush: flush)
}

@objc(initWithType:execute:flush:)
public static func initWithType(
type: PluginType,
execute: @escaping (ObjCBaseEvent) -> ObjCBaseEvent?,
flush: @escaping () -> Void
) -> ObjCPlugin {
ObjCPlugin(type: type, execute: execute, flush: flush)
}

@objc(initWithType:setup:execute:)
public init(
type: PluginType,
Expand All @@ -32,17 +52,44 @@ public class ObjCPlugin: NSObject {
self.type = type
self.setup = setup
self.execute = execute
self.flush = nil
}

@objc(initWithType:execute:)
public init(type: PluginType, execute: @escaping (ObjCBaseEvent) -> ObjCBaseEvent?) {
self.type = type
self.setup = nil
self.execute = execute
self.flush = nil
}

@objc(initWithType:setup:execute:flush:)
public init(
type: PluginType,
setup: @escaping (ObjCAmplitude) -> Void,
execute: @escaping (ObjCBaseEvent) -> ObjCBaseEvent?,
flush: @escaping () -> Void
) {
self.type = type
self.setup = setup
self.execute = execute
self.flush = flush
}

@objc(initWithType:execute:flush:)
public init(
type: PluginType,
execute: @escaping (ObjCBaseEvent) -> ObjCBaseEvent?,
flush: @escaping() -> Void)
{
self.type = type
self.setup = nil
self.execute = execute
self.flush = flush
}
}

class ObjCPluginWrapper: Plugin {
class ObjCPluginWrapper: Plugin, EventPlugin {
weak var amplitude: ObjCAmplitude?
let type: PluginType
let wrapped: ObjCPlugin
Expand All @@ -61,4 +108,24 @@ class ObjCPluginWrapper: Plugin {
func execute(event: BaseEvent) -> BaseEvent? {
wrapped.execute(ObjCBaseEvent(event: event))?.event
}

func track(event: BaseEvent) -> BaseEvent? {
execute(event: event)
}

func identify(event: IdentifyEvent) -> IdentifyEvent? {
execute(event: event) as? IdentifyEvent
}

func groupIdentify(event: GroupIdentifyEvent) -> GroupIdentifyEvent? {
execute(event: event) as? GroupIdentifyEvent
}

func revenue(event: RevenueEvent) -> RevenueEvent? {
execute(event: event) as? RevenueEvent
}

func flush() {
wrapped.flush?()
}
}

0 comments on commit 726e3e8

Please sign in to comment.