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

feat: add middleware removed callback #506

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions Sources/Amplitude/AMPMiddleware.m
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,10 @@ - (void)amplitude:(Amplitude *)amplitude didOptOut:(BOOL)optOut {
}
}

- (void)amplitudeDidRemoveMiddleware:(Amplitude *)amplitude {
if (self.didRemoveMiddleware) {
self.didRemoveMiddleware(amplitude);
}
}

@end
3 changes: 3 additions & 0 deletions Sources/Amplitude/AMPMiddlewareRunner.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,7 @@
- (void)dispatchAmplitude:(nonnull Amplitude *)amplitude didChangeUserId:(nonnull NSString *)userId;
- (void)dispatchAmplitude:(nonnull Amplitude *)amplitude didOptOut:(BOOL)optOut;

- (void)dispatchAmplitudeDidRemoveMiddleware:(nonnull Amplitude *)amplitude
toMiddleware:(nonnull id<AMPMiddleware>)middleware;

@end
8 changes: 8 additions & 0 deletions Sources/Amplitude/AMPMiddlewareRunner.m
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ - (void)dispatchAmplitude:(Amplitude *)amplitude didOptOut:(BOOL)optOut {
}
}

- (void)dispatchAmplitudeDidRemoveMiddleware:(Amplitude *)amplitude
toMiddleware:(id<AMPMiddleware>)middleware {
if ([AMPMiddlewareRunner object:middleware
respondsToSelector:@selector(amplitudeDidRemoveMiddleware:)]) {
[middleware amplitudeDidRemoveMiddleware:amplitude];
}
}

// AMPMiddleware never conformed to NSObject, which means we can't use the standard
// [object respondsToSelector:] syntax to check for protocol conformance to optional methods.
+ (BOOL)object:(id)object respondsToSelector:(SEL)selector {
Expand Down
1 change: 1 addition & 0 deletions Sources/Amplitude/Amplitude.m
Original file line number Diff line number Diff line change
Expand Up @@ -1668,6 +1668,7 @@ - (void)addEventMiddleware:(id<AMPMiddleware> _Nonnull)middleware {
}

- (void)removeEventMiddleware:(id<AMPMiddleware>)middleware {
[_middlewareRunner dispatchAmplitudeDidRemoveMiddleware:self toMiddleware:middleware];
[_middlewareRunner remove:middleware];
}

Expand Down
2 changes: 2 additions & 0 deletions Sources/Amplitude/Public/AMPMiddleware.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ typedef void (^AMPMiddlewareNext)(AMPMiddlewarePayload *_Nullable newPayload);
- (void)amplitude:(nonnull Amplitude *)amplitude didChangeSessionId:(long long)sessionId;
- (void)amplitude:(nonnull Amplitude *)amplitude didChangeUserId:(nonnull NSString *)userId;
- (void)amplitude:(nonnull Amplitude *)amplitude didOptOut:(BOOL)optOut;
- (void)amplitudeDidRemoveMiddleware:(nonnull Amplitude *)amplitude;

@end

Expand All @@ -72,6 +73,7 @@ typedef void (^AMPMiddlewareBlock)(AMPMiddlewarePayload *_Nonnull payload, AMPMi
@property (nonatomic, copy, nullable) void (^didChangeSessionId)(Amplitude * _Nonnull amplitude, long long sessionId);
@property (nonatomic, copy, nullable) void (^didChangeUserId)(Amplitude * _Nonnull amplitude, NSString * _Nonnull userId);
@property (nonatomic, copy, nullable) void (^didOptOut)(Amplitude * _Nonnull amplitude, BOOL optOut);
@property (nonatomic, copy, nullable) void (^didRemoveMiddleware)(Amplitude * _Nonnull amplitude);

- (instancetype _Nonnull)initWithBlock:(AMPMiddlewareBlock _Nonnull)block NS_DESIGNATED_INITIALIZER;

Expand Down
6 changes: 6 additions & 0 deletions Tests/AmplitudeTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -1444,6 +1444,12 @@ - (void)testRemoveMiddleware {
[receivedEventExpectation fulfill];
next(payload);
}];

const XCTestExpectation *didRemoveExpectation = [self expectationWithDescription:@"Removed middleware"];
middleware.didRemoveMiddleware = ^(Amplitude *amplitude) {
[didRemoveExpectation fulfill];
};

[client addEventMiddleware:middleware];

[client logEvent:@"test"];
Expand Down
Loading