Skip to content

Commit

Permalink
Initialize native sdk as a hook
Browse files Browse the repository at this point in the history
  • Loading branch information
matux committed Aug 2, 2023
1 parent 6198568 commit fdf8b2c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
19 changes: 19 additions & 0 deletions rollbar_flutter/lib/src/hooks/native_hook.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:flutter/services.dart';
import 'package:rollbar_dart/rollbar.dart';

import '../method_channel.dart';
import 'hook.dart';

class NativeHook implements Hook {
static const _platform = MethodChannel('com.rollbar.flutter');

@override
Future<void> install(final Config config) async {
await _platform.initialize(config: config);
}

@override
Future<void> uninstall() async {
await _platform.close();
}
}
18 changes: 18 additions & 0 deletions rollbar_flutter/lib/src/method_channel.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:flutter/services.dart';
import 'package:rollbar_flutter/rollbar_flutter.dart';

extension RollbarMethodChannel on MethodChannel {
/// The platform-specific path where we can persist data if needed.
Future<String> get persistencePath async =>
await invokeMethod('persistencePath');

/// Initializes the native Apple/Android SDK Rollbar notifier
/// using the given configuration.
Future<void> initialize({required Config config}) async =>
await invokeMethod('initialize', config.toMap());

/// Unwinds the native Apple/Android SDK Rollbar notifier.
///
/// This is a no-op at the moment.
Future<void> close() async => await invokeMethod('close');
}
27 changes: 13 additions & 14 deletions rollbar_flutter/lib/src/rollbar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,9 @@ import 'package:rollbar_dart/rollbar.dart';
import 'hooks/hook.dart';
import 'hooks/flutter_hook.dart';
import 'hooks/platform_hook.dart';
import 'hooks/native_hook.dart';
import 'platform_transformer.dart';

extension _Methods on MethodChannel {
Future<void> initialize({required Config config}) async =>
await invokeMethod('initialize', config.toMap());

/// The platform-specific path where we can persist data if needed.
Future<String> get persistencePath async =>
await invokeMethod('persistencePath');
}
import 'method_channel.dart';

typedef RollbarClosure = FutureOr<void> Function();

Expand All @@ -35,13 +28,20 @@ class RollbarFlutter {
RollbarClosure appRunner,
) async {
if (!config.handleUncaughtErrors) {
await _run(config, appRunner);
} else if (requiresCustomZone) {
await _run(config, appRunner, [NativeHook()]);
} else if (!PlatformHook.isAvailable) {
await runZonedGuarded(
() async => await _run(config, appRunner, [FlutterHook()]),
() async => await _run(config, appRunner, [
FlutterHook(),
NativeHook(),
]),
Rollbar.error);
} else {
await _run(config, appRunner, [FlutterHook(), PlatformHook()]);
await _run(config, appRunner, [
FlutterHook(),
PlatformHook(),
NativeHook(),
]);
}
}

Expand All @@ -62,7 +62,6 @@ class RollbarFlutter {
await hook.install(config);
}

await _platform.initialize(config: config);
await appRunner();
}

Expand Down

0 comments on commit fdf8b2c

Please sign in to comment.