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

[PLAT-7417] Add new APIs for removing Swift callbacks #1240

Merged
merged 2 commits into from
Nov 29, 2021
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
27 changes: 22 additions & 5 deletions Bugsnag/Bugsnag.m
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,18 @@ + (void)setUser:(NSString *_Nullable)userId
}
}

+ (void)addOnSessionBlock:(BugsnagOnSessionBlock _Nonnull)block
{
+ (nonnull BugsnagOnSessionRef)addOnSessionBlock:(nonnull BugsnagOnSessionBlock)block {
if ([self bugsnagStarted]) {
return [self.client addOnSessionBlock:block];
} else {
// We need to return something from this nonnull method; simulate what would have happened.
return [block copy];
}
}

+ (void)removeOnSession:(nonnull BugsnagOnSessionRef)callback {
if ([self bugsnagStarted]) {
[self.client addOnSessionBlock:block];
[self.client removeOnSession:callback];
}
}

Expand All @@ -340,9 +348,18 @@ + (void)updateCodeBundleId:(NSString *)codeBundleId {
// MARK: - OnBreadcrumb
// =============================================================================

+ (void)addOnBreadcrumbBlock:(BugsnagOnBreadcrumbBlock _Nonnull)block {
+ (nonnull BugsnagOnBreadcrumbRef)addOnBreadcrumbBlock:(nonnull BugsnagOnBreadcrumbBlock)block {
if ([self bugsnagStarted]) {
return [self.client addOnBreadcrumbBlock:block];
} else {
// We need to return something from this nonnull method; simulate what would have happened.
return [block copy];
}
}

+ (void)removeOnBreadcrumb:(nonnull BugsnagOnBreadcrumbRef)callback {
if ([self bugsnagStarted]) {
[self.client addOnBreadcrumbBlock:block];
[self.client removeOnBreadcrumb:callback];
}
}

Expand Down
22 changes: 18 additions & 4 deletions Bugsnag/Client/BugsnagClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -621,24 +621,38 @@ - (void)setUser:(NSString *_Nullable)userId
// MARK: - onSession
// =============================================================================

- (void)addOnSessionBlock:(BugsnagOnSessionBlock _Nonnull)block {
[self.configuration addOnSessionBlock:block];
- (nonnull BugsnagOnSessionRef)addOnSessionBlock:(nonnull BugsnagOnSessionBlock)block {
return [self.configuration addOnSessionBlock:block];
}

- (void)removeOnSession:(nonnull BugsnagOnSessionRef)callback {
[self.configuration removeOnSession:callback];
}

- (void)removeOnSessionBlock:(BugsnagOnSessionBlock _Nonnull )block {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[self.configuration removeOnSessionBlock:block];
#pragma clang diagnostic pop
}

// =============================================================================
// MARK: - onBreadcrumb
// =============================================================================

- (void)addOnBreadcrumbBlock:(BugsnagOnBreadcrumbBlock _Nonnull)block {
[self.configuration addOnBreadcrumbBlock:block];
- (nonnull BugsnagOnBreadcrumbRef)addOnBreadcrumbBlock:(nonnull BugsnagOnBreadcrumbBlock)block {
return [self.configuration addOnBreadcrumbBlock:block];
}

- (void)removeOnBreadcrumb:(nonnull BugsnagOnBreadcrumbRef)callback {
[self.configuration removeOnBreadcrumb:callback];
}

- (void)removeOnBreadcrumbBlock:(BugsnagOnBreadcrumbBlock _Nonnull)block {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[self.configuration removeOnBreadcrumbBlock:block];
#pragma clang diagnostic pop
}

// =============================================================================
Expand Down
57 changes: 45 additions & 12 deletions Bugsnag/Configuration/BugsnagConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -279,39 +279,72 @@ - (void)setUser:(NSString *_Nullable)userId
// MARK: - onSendBlock
// =============================================================================

- (void)addOnSendErrorBlock:(BugsnagOnSendErrorBlock _Nonnull)block {
[(NSMutableArray *)self.onSendBlocks addObject:[block copy]];
- (BugsnagOnSendErrorRef)addOnSendErrorBlock:(BugsnagOnSendErrorBlock)block {
BugsnagOnSendErrorBlock callback = [block copy];
[self.onSendBlocks addObject:callback];
return callback;
}

- (void)removeOnSendErrorBlock:(BugsnagOnSendErrorBlock _Nonnull )block
{
[(NSMutableArray *)self.onSendBlocks removeObject:block];
- (void)removeOnSendError:(BugsnagOnSendErrorRef)callback {
if (![callback isKindOfClass:NSClassFromString(@"NSBlock")]) {
bsg_log_err(@"Invalid object type passed to %@", NSStringFromSelector(_cmd));
return;
}
[self.onSendBlocks removeObject:(id)callback];
}

- (void)removeOnSendErrorBlock:(BugsnagOnSendErrorBlock)block {
[self.onSendBlocks removeObject:block];
}

// =============================================================================
// MARK: - onSessionBlock
// =============================================================================

- (void)addOnSessionBlock:(BugsnagOnSessionBlock)block {
[(NSMutableArray *)self.onSessionBlocks addObject:[block copy]];
- (BugsnagOnSessionRef)addOnSessionBlock:(BugsnagOnSessionBlock)block {
BugsnagOnSessionBlock callback = [block copy];
[self.onSessionBlocks addObject:callback];
return callback;
}

- (void)removeOnSession:(BugsnagOnSessionRef)callback {
if (![callback isKindOfClass:NSClassFromString(@"NSBlock")]) {
bsg_log_err(@"Invalid object type passed to %@", NSStringFromSelector(_cmd));
return;
}
[self.onSessionBlocks removeObject:(id)callback];
}

- (void)removeOnSessionBlock:(BugsnagOnSessionBlock)block {
[(NSMutableArray *)self.onSessionBlocks removeObject:block];
[self.onSessionBlocks removeObject:block];
}

// =============================================================================
// MARK: - onBreadcrumbBlock
// =============================================================================

- (void)addOnBreadcrumbBlock:(BugsnagOnBreadcrumbBlock _Nonnull)block {
[(NSMutableArray *)self.onBreadcrumbBlocks addObject:[block copy]];
- (BugsnagOnBreadcrumbRef)addOnBreadcrumbBlock:(BugsnagOnBreadcrumbBlock)block {
BugsnagOnBreadcrumbBlock callback = [block copy];
[self.onBreadcrumbBlocks addObject:callback];
return callback;
}

- (void)removeOnBreadcrumbBlock:(BugsnagOnBreadcrumbBlock _Nonnull)block {
[(NSMutableArray *)self.onBreadcrumbBlocks removeObject:block];
- (void)removeOnBreadcrumb:(BugsnagOnBreadcrumbRef)callback {
if (![callback isKindOfClass:NSClassFromString(@"NSBlock")]) {
bsg_log_err(@"Invalid object type passed to %@", NSStringFromSelector(_cmd));
return;
}
[self.onBreadcrumbBlocks removeObject:(id)callback];
}

- (void)removeOnBreadcrumbBlock:(BugsnagOnBreadcrumbBlock)block {
[self.onBreadcrumbBlocks removeObject:block];
}

// =============================================================================
// MARK: -
// =============================================================================

- (NSDictionary *)sessionApiHeaders {
return @{BugsnagHTTPHeaderNameApiKey: self.apiKey ?: @"",
BugsnagHTTPHeaderNamePayloadVersion: @"1.0",
Expand Down
28 changes: 23 additions & 5 deletions Bugsnag/include/Bugsnag/Bugsnag.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
/**
* @return YES if Bugsnag has been started and the previous launch crashed
*/
+ (BOOL)appDidCrashLastLaunch __attribute__((deprecated("use 'lastRunInfo.crashed' instead")));
+ (BOOL)appDidCrashLastLaunch BSG_DEPRECATED_WITH_REPLACEMENT("use 'lastRunInfo.crashed' instead");

/**
* Information about the last run of the app, and whether it crashed.
Expand Down Expand Up @@ -301,16 +301,25 @@
* Add a callback to be invoked before a session is sent to Bugsnag.
*
* @param block A block which can modify the session
*
* @returns An opaque reference to the callback which can be passed to `removeOnSession:`
*/
+ (void)addOnSessionBlock:(BugsnagOnSessionBlock _Nonnull)block
+ (nonnull BugsnagOnSessionRef)addOnSessionBlock:(nonnull BugsnagOnSessionBlock)block
NS_SWIFT_NAME(addOnSession(block:));

/**
* Remove a callback that would be invoked before a session is sent to Bugsnag.
*
* @param block The block to be removed.
* @param callback The opaque reference of the callback, returned by `addOnSessionBlock:`
*/
+ (void)removeOnSession:(nonnull BugsnagOnSessionRef)callback
NS_SWIFT_NAME(removeOnSession(_:));

/**
* Deprecated
*/
+ (void)removeOnSessionBlock:(BugsnagOnSessionBlock _Nonnull)block
BSG_DEPRECATED_WITH_REPLACEMENT("removeOnSession:")
NS_SWIFT_NAME(removeOnSession(block:));

// =============================================================================
Expand All @@ -322,16 +331,25 @@
* change the breadcrumb contents as needed
*
* @param block A block which returns YES if the breadcrumb should be captured
*
* @returns An opaque reference to the callback which can be passed to `removeOnBreadcrumb:`
*/
+ (void)addOnBreadcrumbBlock:(BugsnagOnBreadcrumbBlock _Nonnull)block
+ (nonnull BugsnagOnBreadcrumbRef)addOnBreadcrumbBlock:(nonnull BugsnagOnBreadcrumbBlock)block
NS_SWIFT_NAME(addOnBreadcrumb(block:));

/**
* Remove the callback that would be invoked when a breadcrumb is captured.
*
* @param block The block to be removed.
* @param callback The opaque reference of the callback, returned by `addOnBreadcrumbBlock:`
*/
+ (void)removeOnBreadcrumb:(nonnull BugsnagOnBreadcrumbRef)callback
NS_SWIFT_NAME(removeOnBreadcrumb(_:));

/**
* Deprecated
*/
+ (void)removeOnBreadcrumbBlock:(BugsnagOnBreadcrumbBlock _Nonnull)block
BSG_DEPRECATED_WITH_REPLACEMENT("removeOnBreadcrumb:")
NS_SWIFT_NAME(removeOnBreadcrumb(block:));

@end
36 changes: 27 additions & 9 deletions Bugsnag/include/Bugsnag/BugsnagClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,19 +184,28 @@ NS_SWIFT_NAME(leaveBreadcrumb(_:metadata:type:));
// =============================================================================

/**
* Add a callback that would be invoked before a session is sent to Bugsnag.
*
* @param block The block to be added.
*/
- (void)addOnSessionBlock:(BugsnagOnSessionBlock _Nonnull)block
* Add a callback to be invoked before a session is sent to Bugsnag.
*
* @param block A block which can modify the session
*
* @returns An opaque reference to the callback which can be passed to `removeOnSession:`
*/
- (nonnull BugsnagOnSessionRef)addOnSessionBlock:(nonnull BugsnagOnSessionBlock)block
NS_SWIFT_NAME(addOnSession(block:));

/**
* Remove a callback that would be invoked before a session is sent to Bugsnag.
*
* @param block The block to be removed.
* @param callback The opaque reference of the callback, returned by `addOnSessionBlock:`
*/
- (void)removeOnSession:(nonnull BugsnagOnSessionRef)callback
NS_SWIFT_NAME(removeOnSession(_:));

/**
* Deprecated
*/
- (void)removeOnSessionBlock:(BugsnagOnSessionBlock _Nonnull )block
BSG_DEPRECATED_WITH_REPLACEMENT("removeOnSession:")
NS_SWIFT_NAME(removeOnSession(block:));

// =============================================================================
Expand All @@ -211,7 +220,7 @@ NS_SWIFT_NAME(leaveBreadcrumb(_:metadata:type:));
/**
* @return YES if Bugsnag has been started and the previous launch crashed
*/
- (BOOL)appDidCrashLastLaunch __attribute__((deprecated("use 'lastRunInfo.crashed' instead")));
- (BOOL)appDidCrashLastLaunch BSG_DEPRECATED_WITH_REPLACEMENT("lastRunInfo.crashed");

/**
* Information about the last run of the app, and whether it crashed.
Expand Down Expand Up @@ -255,16 +264,25 @@ NS_SWIFT_NAME(leaveBreadcrumb(_:metadata:type:));
* change the breadcrumb contents as needed
*
* @param block A block which returns YES if the breadcrumb should be captured
*
* @returns An opaque reference to the callback which can be passed to `removeOnBreadcrumb:`
*/
- (void)addOnBreadcrumbBlock:(BugsnagOnBreadcrumbBlock _Nonnull)block
- (nonnull BugsnagOnBreadcrumbRef)addOnBreadcrumbBlock:(nonnull BugsnagOnBreadcrumbBlock)block
NS_SWIFT_NAME(addOnBreadcrumb(block:));

/**
* Remove the callback that would be invoked when a breadcrumb is captured.
*
* @param block The block to be removed.
* @param callback The opaque reference of the callback, returned by `addOnBreadcrumbBlock:`
*/
- (void)removeOnBreadcrumb:(nonnull BugsnagOnBreadcrumbRef)callback
NS_SWIFT_NAME(removeOnBreadcrumb(_:));

/**
* Deprecated
*/
- (void)removeOnBreadcrumbBlock:(BugsnagOnBreadcrumbBlock _Nonnull)block
BSG_DEPRECATED_WITH_REPLACEMENT("removeOnBreadcrumb:")
NS_SWIFT_NAME(removeOnBreadcrumb(block:));

@end
Loading