@@ -120,7 +120,20 @@ export type ApprovalStateChange = {
120
120
payload : [ ApprovalControllerState , Patch [ ] ] ;
121
121
} ;
122
122
123
- export type ApprovalControllerEvents = ApprovalStateChange ;
123
+ export type ApprovalRequestAccepted = {
124
+ type : `${typeof controllerName } :requestAccepted`;
125
+ payload : [ ApprovalRequest < Record < string , Json > > , unknown ] ;
126
+ } ;
127
+
128
+ export type ApprovalRequestRejected = {
129
+ type : `${typeof controllerName } :requestRejected`;
130
+ payload : [ ApprovalRequest < Record < string , Json > > , Error ] ;
131
+ } ;
132
+
133
+ export type ApprovalControllerEvents =
134
+ | ApprovalStateChange
135
+ | ApprovalRequestAccepted
136
+ | ApprovalRequestRejected ;
124
137
125
138
export type ApprovalControllerMessenger = RestrictedControllerMessenger <
126
139
typeof controllerName ,
@@ -421,7 +434,13 @@ export class ApprovalController extends BaseController<
421
434
* @param value - The value to resolve the approval promise with.
422
435
*/
423
436
accept ( id : string , value ?: unknown ) : void {
424
- this . _deleteApprovalAndGetCallbacks ( id ) . resolve ( value ) ;
437
+ const [ approvalRequest , callbacks ] = this . _deleteApprovalAndGetData ( id ) ;
438
+ callbacks . resolve ( value ) ;
439
+ this . messagingSystem . publish (
440
+ `${ controllerName } :requestAccepted` as const ,
441
+ approvalRequest ,
442
+ value ,
443
+ ) ;
425
444
}
426
445
427
446
/**
@@ -432,7 +451,13 @@ export class ApprovalController extends BaseController<
432
451
* @param error - The error to reject the approval promise with.
433
452
*/
434
453
reject ( id : string , error : Error ) : void {
435
- this . _deleteApprovalAndGetCallbacks ( id ) . reject ( error ) ;
454
+ const [ approvalRequest , callbacks ] = this . _deleteApprovalAndGetData ( id ) ;
455
+ callbacks . reject ( error ) ;
456
+ this . messagingSystem . publish (
457
+ `${ controllerName } :requestRejected` as const ,
458
+ approvalRequest ,
459
+ error ,
460
+ ) ;
436
461
}
437
462
438
463
/**
@@ -564,6 +589,29 @@ export class ApprovalController extends BaseController<
564
589
} ) ;
565
590
}
566
591
592
+ /**
593
+ * Gets the approval callbacks for the given id, deletes the entry, and then
594
+ * returns the callbacks for promise resolution.
595
+ * Throws an error if no approval is found for the given id.
596
+ *
597
+ * @param id - The id of the approval request.
598
+ * @returns The promise callbacks associated with the approval request.
599
+ */
600
+ private _deleteApprovalAndGetData (
601
+ id : string ,
602
+ ) : [ ApprovalRequest < Record < string , Json > > , ApprovalCallbacks ] {
603
+ const callbacks = this . _approvals . get ( id ) ;
604
+ if ( ! callbacks ) {
605
+ throw new Error ( `Approval request with id '${ id } ' not found.` ) ;
606
+ }
607
+
608
+ // We've already verified that the approval exists.
609
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
610
+ const approvalRequest = this . state . pendingApprovals [ id ] ! ;
611
+ this . _delete ( id ) ;
612
+ return [ approvalRequest , callbacks ] ;
613
+ }
614
+
567
615
/**
568
616
* Deletes the approval with the given id. The approval promise must be
569
617
* resolved or reject before this method is called.
@@ -593,24 +641,6 @@ export class ApprovalController extends BaseController<
593
641
} ) ;
594
642
}
595
643
596
- /**
597
- * Gets the approval callbacks for the given id, deletes the entry, and then
598
- * returns the callbacks for promise resolution.
599
- * Throws an error if no approval is found for the given id.
600
- *
601
- * @param id - The id of the approval request.
602
- * @returns The promise callbacks associated with the approval request.
603
- */
604
- private _deleteApprovalAndGetCallbacks ( id : string ) : ApprovalCallbacks {
605
- const callbacks = this . _approvals . get ( id ) ;
606
- if ( ! callbacks ) {
607
- throw new Error ( `Approval request with id '${ id } ' not found.` ) ;
608
- }
609
-
610
- this . _delete ( id ) ;
611
- return callbacks ;
612
- }
613
-
614
644
/**
615
645
* Checks whether there are any approvals associated with the given
616
646
* origin.
0 commit comments