1+ import 'dart:async' ;
12import 'dart:collection' ;
23
3- import 'sentry_attachment/sentry_attachment.dart' ;
44import 'event_processor.dart' ;
55import 'protocol.dart' ;
6+ import 'scope_observer.dart' ;
7+ import 'sentry_attachment/sentry_attachment.dart' ;
68import 'sentry_options.dart' ;
9+ import 'sentry_span_interface.dart' ;
710import 'sentry_tracer.dart' ;
8- import 'tracing.dart' ;
911
1012/// Scope data to be sent with the event
1113class Scope {
@@ -46,8 +48,17 @@ class Scope {
4648 }
4749 }
4850
49- /// Information about the current user.
50- SentryUser ? user;
51+ SentryUser ? _user;
52+
53+ /// Get the current user.
54+ SentryUser ? get user => _user;
55+
56+ /// Set the current user.
57+ Future <void > setUser (SentryUser ? user) async {
58+ _user = user;
59+ await _callScopeObservers (
60+ (scopeObserver) async => await scopeObserver.setUser (user));
61+ }
5162
5263 List <String > _fingerprint = [];
5364
@@ -93,15 +104,21 @@ class Scope {
93104 Map <String , dynamic > get contexts => Map .unmodifiable (_contexts);
94105
95106 /// add an entry to the Scope's contexts
96- void setContexts (String key, dynamic value) {
107+ Future < void > setContexts (String key, dynamic value) async {
97108 _contexts[key] = (value is num || value is bool || value is String )
98109 ? {'value' : value}
99110 : value;
111+
112+ await _callScopeObservers (
113+ (scopeObserver) async => await scopeObserver.setContexts (key, value));
100114 }
101115
102116 /// Removes a value from the Scope's contexts
103- void removeContexts (String key) {
117+ Future < void > removeContexts (String key) async {
104118 _contexts.remove (key);
119+
120+ await _callScopeObservers (
121+ (scopeObserver) async => await scopeObserver.removeContexts (key));
105122 }
106123
107124 /// Scope's event processor list
@@ -114,14 +131,17 @@ class Scope {
114131
115132 final SentryOptions _options;
116133
117- final List <SentryAttachment > _attachements = [];
134+ final List <SentryAttachment > _attachments = [];
135+
136+ List <SentryAttachment > get attachments => List .unmodifiable (_attachments);
118137
119- List <SentryAttachment > get attachements => List .unmodifiable (_attachements);
138+ @Deprecated ('Use attachments instead' )
139+ List <SentryAttachment > get attachements => attachments;
120140
121141 Scope (this ._options);
122142
123143 /// Adds a breadcrumb to the breadcrumbs queue
124- void addBreadcrumb (Breadcrumb breadcrumb, {dynamic hint}) {
144+ Future < void > addBreadcrumb (Breadcrumb breadcrumb, {dynamic hint}) async {
125145 // bail out if maxBreadcrumbs is zero
126146 if (_options.maxBreadcrumbs == 0 ) {
127147 return ;
@@ -151,19 +171,25 @@ class Scope {
151171 }
152172
153173 _breadcrumbs.add (breadcrumb);
174+
175+ await _callScopeObservers (
176+ (scopeObserver) async => await scopeObserver.addBreadcrumb (breadcrumb));
154177 }
155178
156179 void addAttachment (SentryAttachment attachment) {
157- _attachements .add (attachment);
180+ _attachments .add (attachment);
158181 }
159182
160183 void clearAttachments () {
161- _attachements .clear ();
184+ _attachments .clear ();
162185 }
163186
164187 /// Clear all the breadcrumbs
165- void clearBreadcrumbs () {
188+ Future < void > clearBreadcrumbs () async {
166189 _breadcrumbs.clear ();
190+
191+ await _callScopeObservers (
192+ (scopeObserver) async => await scopeObserver.clearBreadcrumbs ());
167193 }
168194
169195 /// Adds an event processor
@@ -172,36 +198,46 @@ class Scope {
172198 }
173199
174200 /// Resets the Scope to its default state
175- void clear () {
176- clearBreadcrumbs ();
201+ Future < void > clear () async {
202+ await clearBreadcrumbs ();
177203 clearAttachments ();
178204 level = null ;
179205 _span = null ;
180206 _transaction = null ;
181- user = null ;
207+ await setUser ( null ) ;
182208 _fingerprint = [];
183209 _tags.clear ();
184210 _extra.clear ();
185211 _eventProcessors.clear ();
186212 }
187213
188214 /// Sets a tag to the Scope
189- void setTag (String key, String value) {
215+ Future < void > setTag (String key, String value) async {
190216 _tags[key] = value;
217+ await _callScopeObservers (
218+ (scopeObserver) async => await scopeObserver.setTag (key, value));
191219 }
192220
193221 /// Removes a tag from the Scope
194- void removeTag (String key) {
222+ Future < void > removeTag (String key) async {
195223 _tags.remove (key);
224+ await _callScopeObservers (
225+ (scopeObserver) async => await scopeObserver.removeTag (key));
196226 }
197227
198228 /// Sets an extra to the Scope
199- void setExtra (String key, dynamic value) {
229+ Future < void > setExtra (String key, dynamic value) async {
200230 _extra[key] = value;
231+ await _callScopeObservers (
232+ (scopeObserver) async => await scopeObserver.setExtra (key, value));
201233 }
202234
203235 /// Removes an extra from the Scope
204- void removeExtra (String key) => _extra.remove (key);
236+ Future <void > removeExtra (String key) async {
237+ _extra.remove (key);
238+ await _callScopeObservers (
239+ (scopeObserver) async => await scopeObserver.removeExtra (key));
240+ }
205241
206242 Future <SentryEvent ?> applyToEvent (
207243 SentryEvent event, {
@@ -327,11 +363,12 @@ class Scope {
327363 Scope clone () {
328364 final clone = Scope (_options)
329365 ..level = level
330- ..user = user
331366 ..fingerprint = List .from (fingerprint)
332367 .._transaction = _transaction
333368 .._span = _span;
334369
370+ clone.setUser (user);
371+
335372 for (final tag in _tags.keys) {
336373 clone.setTag (tag, _tags[tag]! );
337374 }
@@ -354,10 +391,19 @@ class Scope {
354391 }
355392 });
356393
357- for (final attachment in _attachements ) {
394+ for (final attachment in _attachments ) {
358395 clone.addAttachment (attachment);
359396 }
360397
361398 return clone;
362399 }
400+
401+ Future <void > _callScopeObservers (
402+ Future <void > Function (ScopeObserver ) action) async {
403+ if (_options.enableScopeSync) {
404+ for (final scopeObserver in _options.scopeObservers) {
405+ await action (scopeObserver);
406+ }
407+ }
408+ }
363409}
0 commit comments