@@ -8,18 +8,23 @@ import { Scope } from './scope';
88 * working in case we have a version conflict.
99 */
1010export class Hub {
11+ /** Is a {@link Layer}[] containing the client and scope */
12+ private readonly stack : Layer [ ] = [ ] ;
13+
1114 /**
12- * Creates a new instance of the hub
13- * @param stack Is a {@link Layer}[] containing the client and scope
15+ * Creates a new instance of the hub, will push one {@link Layer} into the
16+ * internal stack on creation.
17+ *
18+ * @param client bound to the hub.
19+ * @param scope bound to the hub.
1420 * @param version number, higher number means higher priority.
1521 */
1622 public constructor (
17- private readonly stack : Layer [ ] = [ ] ,
23+ client ?: any ,
24+ scope : Scope = new Scope ( ) ,
1825 private readonly version : number = API_VERSION ,
1926 ) {
20- if ( stack . length === 0 ) {
21- this . stack . push ( { scope : this . createScope ( ) , type : 'process' } ) ;
22- }
27+ this . stack . push ( { client, scope } ) ;
2328 }
2429
2530 /**
@@ -28,7 +33,7 @@ export class Hub {
2833 * @param method The method to call on the client/client.
2934 * @param args Arguments to pass to the client/fontend.
3035 */
31- public _invokeClient ( method : string , ...args : any [ ] ) : void {
36+ private invokeClient ( method : string , ...args : any [ ] ) : void {
3237 const top = this . getStackTop ( ) ;
3338 if ( top && top . client && top . client [ method ] ) {
3439 top . client [ method ] ( ...args , top . scope ) ;
@@ -61,25 +66,42 @@ export class Hub {
6166 return this . version < version ;
6267 }
6368
69+ /**
70+ * This binds the given client to the current scope.
71+ * @param client An SDK client (client) instance.
72+ */
73+ public bindClient ( client ?: any ) : void {
74+ const top = this . getStackTop ( ) ;
75+ top . client = client ;
76+ if ( top && top . scope && client ) {
77+ top . scope . addScopeListener ( ( s : Scope ) => {
78+ if ( client . getBackend ) {
79+ try {
80+ client . getBackend ( ) . storeScope ( s ) ;
81+ } catch {
82+ // Do nothing
83+ }
84+ }
85+ } ) ;
86+ }
87+ }
88+
6489 /**
6590 * Create a new scope to store context information.
6691 *
6792 * The scope will be layered on top of the current one. It is isolated, i.e. all
6893 * breadcrumbs and context information added to this scope will be removed once
6994 * the scope ends. Be sure to always remove this scope with {@link this.popScope}
7095 * when the operation finishes or throws.
71- * @param client Optional client, defaults to the current client.
7296 */
73- public pushScope ( client ?: any ) : void {
74- const usedClient = client || this . getCurrentClient ( ) ;
97+ public pushScope ( ) : void {
7598 // We want to clone the content of prev scope
7699 const stack = this . getStack ( ) ;
77100 const parentScope =
78101 stack . length > 0 ? stack [ stack . length - 1 ] . scope : undefined ;
79102 this . getStack ( ) . push ( {
80- client : usedClient ,
81- scope : this . createScope ( parentScope ) ,
82- type : 'local' ,
103+ client : this . getClient ( ) ,
104+ scope : Scope . clone ( parentScope ) ,
83105 } ) ;
84106 }
85107
@@ -95,34 +117,20 @@ export class Hub {
95117 }
96118
97119 /**
98- * Creates a new scope with a custom client instance and executes the given
99- * operation within. The scope is automatically removed once the operation
120+ * Creates a new scope with and executes the given operation within.
121+ * The scope is automatically removed once the operation
100122 * finishes or throws.
101123 *
102- * The client can be configured with different options than the enclosing scope,
103- * such as a different DSN or other callbacks.
104- *
105124 * This is essentially a convenience function for:
106125 *
107- * pushScope(client );
126+ * pushScope();
108127 * callback();
109128 * popScope();
110129 *
111- * @param arg1 Either the client or callback.
112- * @param arg2 Either the client or callback.
130+ * @param callback that will be enclosed into push/popScope.
113131 */
114- public withScope ( arg1 : ( ( ) => void ) | any , arg2 ?: ( ( ) => void ) | any ) : void {
115- let callback : ( ) => void = arg1 ;
116- let client : any = arg2 ;
117- if ( ! ! ( arg1 && arg1 . constructor && arg1 . call && arg1 . apply ) ) {
118- callback = arg1 ;
119- client = arg2 ;
120- }
121- if ( ! ! ( arg2 && arg2 . constructor && arg2 . call && arg2 . apply ) ) {
122- callback = arg2 ;
123- client = arg1 ;
124- }
125- this . pushScope ( client ) ;
132+ public withScope ( callback : ( ( ) => void ) ) : void {
133+ this . pushScope ( ) ;
126134 try {
127135 callback ( ) ;
128136 } finally {
@@ -131,7 +139,7 @@ export class Hub {
131139 }
132140
133141 /** Returns the client of the currently active scope. */
134- public getCurrentClient ( ) : any | undefined {
142+ public getClient ( ) : any | undefined {
135143 return this . getStackTop ( ) . client ;
136144 }
137145
@@ -145,18 +153,6 @@ export class Hub {
145153 return this . stack [ this . stack . length - 1 ] ;
146154 }
147155
148- /**
149- * Obtains a new scope instance from the client.
150- *
151- * @param parentScope Optional parent scope to inherit from.
152- * @returns The scope instance or an empty object on error.
153- */
154- public createScope ( parentScope ?: Scope ) : Scope {
155- const newScope = new Scope ( ) ;
156- newScope . setParentScope ( parentScope ) ;
157- return newScope ;
158- }
159-
160156 /**
161157 * Captures an exception event and sends it to Sentry.
162158 *
@@ -193,7 +189,7 @@ export class Hub {
193189 * @param breadcrumb The breadcrumb to record.
194190 */
195191 public addBreadcrumb ( breadcrumb : Breadcrumb ) : void {
196- this . _invokeClient ( 'addBreadcrumb' , breadcrumb ) ;
192+ this . invokeClient ( 'addBreadcrumb' , breadcrumb ) ;
197193 }
198194
199195 /**
@@ -208,4 +204,15 @@ export class Hub {
208204 callback ( top . scope ) ;
209205 }
210206 }
207+
208+ /**
209+ * This will be called to receive the event
210+ * @param callback
211+ */
212+ public addEventProcessor ( callback : ( ) => ( event : SentryEvent ) => void ) : void {
213+ const top = this . getStackTop ( ) ;
214+ if ( top . scope && top . client ) {
215+ top . scope . addEventProcessor ( callback ( ) ) ;
216+ }
217+ }
211218}
0 commit comments