@@ -3,28 +3,17 @@ import 'dart:async';
3
3
import 'package:meta/meta.dart' ;
4
4
5
5
import '../../sentry_flutter.dart' ;
6
- import 'sentry_native_channel .dart' ;
6
+ import 'sentry_native_binding .dart' ;
7
7
8
- /// [SentryNative] holds state that it fetches from to the native SDKs. Always
9
- /// use the shared instance with [SentryNative()] .
8
+ /// [SentryNative] holds state that it fetches from to the native SDKs.
9
+ /// It forwards to platform-specific implementations of [SentryNativeBinding] .
10
+ /// Any errors are logged and ignored.
10
11
@internal
11
12
class SentryNative {
12
- SentryNative ._();
13
+ final SentryOptions _options;
14
+ final SentryNativeBinding _binding;
13
15
14
- static final SentryNative _instance = SentryNative ._();
15
-
16
- SentryNativeChannel ? _nativeChannel;
17
-
18
- factory SentryNative () {
19
- return _instance;
20
- }
21
-
22
- SentryNativeChannel ? get nativeChannel => _instance._nativeChannel;
23
-
24
- /// Provide [nativeChannel] for native communication.
25
- set nativeChannel (SentryNativeChannel ? nativeChannel) {
26
- _instance._nativeChannel = nativeChannel;
27
- }
16
+ SentryNative (this ._options, this ._binding);
28
17
29
18
// AppStart
30
19
@@ -41,75 +30,99 @@ class SentryNative {
41
30
/// Fetch [NativeAppStart] from native channels. Can only be called once.
42
31
Future <NativeAppStart ?> fetchNativeAppStart () async {
43
32
_didFetchAppStart = true ;
44
- return await _nativeChannel ? .fetchNativeAppStart ( );
33
+ return _invoke ( "fetchNativeAppStart" , _binding .fetchNativeAppStart);
45
34
}
46
35
47
36
// NativeFrames
48
37
49
- Future <void > beginNativeFramesCollection () async {
50
- await _nativeChannel? .beginNativeFrames ();
51
- }
38
+ Future <void > beginNativeFramesCollection () =>
39
+ _invoke ("beginNativeFrames" , _binding.beginNativeFrames);
52
40
53
- Future <NativeFrames ?> endNativeFramesCollection (SentryId traceId) async {
54
- return await _nativeChannel? .endNativeFrames (traceId);
55
- }
41
+ Future <NativeFrames ?> endNativeFramesCollection (SentryId traceId) =>
42
+ _invoke ("endNativeFrames" , () => _binding.endNativeFrames (traceId));
56
43
57
44
// Scope
58
45
59
- Future <void > setContexts (String key, dynamic value) async {
60
- return await _nativeChannel? .setContexts (key, value);
61
- }
46
+ Future <void > setContexts (String key, dynamic value) =>
47
+ _invoke ("setContexts" , () => _binding.setContexts (key, value));
62
48
63
- Future <void > removeContexts (String key) async {
64
- return await _nativeChannel? .removeContexts (key);
65
- }
49
+ Future <void > removeContexts (String key) =>
50
+ _invoke ("removeContexts" , () => _binding.removeContexts (key));
66
51
67
- Future <void > setUser (SentryUser ? sentryUser) async {
68
- return await _nativeChannel? .setUser (sentryUser);
69
- }
52
+ Future <void > setUser (SentryUser ? sentryUser) =>
53
+ _invoke ("setUser" , () => _binding.setUser (sentryUser));
70
54
71
- Future <void > addBreadcrumb (Breadcrumb breadcrumb) async {
72
- return await _nativeChannel? .addBreadcrumb (breadcrumb);
73
- }
55
+ Future <void > addBreadcrumb (Breadcrumb breadcrumb) =>
56
+ _invoke ("addBreadcrumb" , () => _binding.addBreadcrumb (breadcrumb));
74
57
75
- Future <void > clearBreadcrumbs () async {
76
- return await _nativeChannel? .clearBreadcrumbs ();
77
- }
58
+ Future <void > clearBreadcrumbs () =>
59
+ _invoke ("clearBreadcrumbs" , _binding.clearBreadcrumbs);
78
60
79
- Future <void > setExtra (String key, dynamic value) async {
80
- return await _nativeChannel? .setExtra (key, value);
81
- }
61
+ Future <void > setExtra (String key, dynamic value) =>
62
+ _invoke ("setExtra" , () => _binding.setExtra (key, value));
82
63
83
- Future <void > removeExtra (String key) async {
84
- return await _nativeChannel? .removeExtra (key);
85
- }
64
+ Future <void > removeExtra (String key) =>
65
+ _invoke ("removeExtra" , () => _binding.removeExtra (key));
86
66
87
- Future <void > setTag (String key, String value) async {
88
- return await _nativeChannel? .setTag (key, value);
89
- }
67
+ Future <void > setTag (String key, String value) =>
68
+ _invoke ("setTag" , () => _binding.setTag (key, value));
90
69
91
- Future <void > removeTag (String key) async {
92
- return await _nativeChannel? .removeTag (key);
93
- }
70
+ Future <void > removeTag (String key) =>
71
+ _invoke ("removeTag" , () => _binding.removeTag (key));
94
72
95
- Future <int ?> startProfiler (SentryId traceId) async {
96
- return _nativeChannel? .startProfiler (traceId);
97
- }
73
+ int ? startProfiler (SentryId traceId) =>
74
+ _invokeSync ("startProfiler" , () => _binding.startProfiler (traceId));
98
75
99
- Future <void > discardProfiler (SentryId traceId) async {
100
- return _nativeChannel? .discardProfiler (traceId);
101
- }
76
+ Future <void > discardProfiler (SentryId traceId) =>
77
+ _invoke ("discardProfiler" , () => _binding.discardProfiler (traceId));
102
78
103
79
Future <Map <String , dynamic >?> collectProfile (
104
- SentryId traceId, int startTimeNs, int endTimeNs) async {
105
- return _nativeChannel ? . collectProfile (traceId, startTimeNs, endTimeNs);
106
- }
80
+ SentryId traceId, int startTimeNs, int endTimeNs) =>
81
+ _invoke ( "collectProfile" ,
82
+ () => _binding. collectProfile (traceId, startTimeNs, endTimeNs));
107
83
108
84
/// Reset state
109
85
void reset () {
110
86
appStartEnd = null ;
111
87
_didFetchAppStart = false ;
112
88
}
89
+
90
+ // Helpers
91
+ Future <T ?> _invoke <T >(
92
+ String nativeMethodName, Future <T ?> Function () fn) async {
93
+ try {
94
+ return await fn ();
95
+ } catch (error, stackTrace) {
96
+ _logError (nativeMethodName, error, stackTrace);
97
+ // ignore: invalid_use_of_internal_member
98
+ if (_options.devMode) {
99
+ rethrow ;
100
+ }
101
+ return null ;
102
+ }
103
+ }
104
+
105
+ T ? _invokeSync <T >(String nativeMethodName, T ? Function () fn) {
106
+ try {
107
+ return fn ();
108
+ } catch (error, stackTrace) {
109
+ _logError (nativeMethodName, error, stackTrace);
110
+ // ignore: invalid_use_of_internal_member
111
+ if (_options.devMode) {
112
+ rethrow ;
113
+ }
114
+ return null ;
115
+ }
116
+ }
117
+
118
+ void _logError (String nativeMethodName, Object error, StackTrace stackTrace) {
119
+ _options.logger (
120
+ SentryLevel .error,
121
+ 'Native call `$nativeMethodName ` failed' ,
122
+ exception: error,
123
+ stackTrace: stackTrace,
124
+ );
125
+ }
113
126
}
114
127
115
128
class NativeAppStart {
0 commit comments