@@ -3,88 +3,83 @@ import { execute } from './broker-protection/execute.js';
33import { retry } from '../timer-utils.js' ;
44import { ErrorResponse } from './broker-protection/types.js' ;
55
6- /**
7- * @param {typeof ContentFeature } ContentFeatureClass
8- */
9- export function ActionExecutorMixin ( ContentFeatureClass ) {
10- return class Mixin extends ContentFeatureClass {
11- /**
12- * @param {any } action
13- * @param {Record<string, any> } data
14- * @param {any } retryConfig
15- */
16- async processActionAndNotify ( action , data , retryConfig ) {
17- try {
18- if ( ! action ) {
19- return this . messaging . notify ( 'actionError' , { error : 'No action found.' } ) ;
20- }
6+ export class ActionExecutorBase extends ContentFeature {
7+ /**
8+ * @param {any } action
9+ * @param {Record<string, any> } data
10+ * @param {any } retryConfig
11+ */
12+ async processActionAndNotify ( action , data , retryConfig ) {
13+ try {
14+ if ( ! action ) {
15+ return this . messaging . notify ( 'actionError' , { error : 'No action found.' } ) ;
16+ }
2117
22- const { results, exceptions } = await this . exec ( action , data , retryConfig ) ;
18+ const { results, exceptions } = await this . exec ( action , data , retryConfig ) ;
2319
24- if ( results ) {
25- // there might only be a single result.
26- const parent = results [ 0 ] ;
27- const errors = results . filter ( ( x ) => 'error' in x ) ;
20+ if ( results ) {
21+ // there might only be a single result.
22+ const parent = results [ 0 ] ;
23+ const errors = results . filter ( ( x ) => 'error' in x ) ;
2824
29- // if there are no secondary actions, or just no errors in general, just report the parent action
30- if ( results . length === 1 || errors . length === 0 ) {
31- return this . messaging . notify ( 'actionCompleted' , { result : parent } ) ;
32- }
25+ // if there are no secondary actions, or just no errors in general, just report the parent action
26+ if ( results . length === 1 || errors . length === 0 ) {
27+ return this . messaging . notify ( 'actionCompleted' , { result : parent } ) ;
28+ }
3329
34- // here we must have secondary actions that failed.
35- // so we want to create an error response with the parent ID, but with the errors messages from
36- // the children
37- const joinedErrors = errors . map ( ( x ) => x . error . message ) . join ( ', ' ) ;
38- const response = new ErrorResponse ( {
39- actionID : action . id ,
40- message : 'Secondary actions failed: ' + joinedErrors ,
41- } ) ;
30+ // here we must have secondary actions that failed.
31+ // so we want to create an error response with the parent ID, but with the errors messages from
32+ // the children
33+ const joinedErrors = errors . map ( ( x ) => x . error . message ) . join ( ', ' ) ;
34+ const response = new ErrorResponse ( {
35+ actionID : action . id ,
36+ message : 'Secondary actions failed: ' + joinedErrors ,
37+ } ) ;
4238
43- return this . messaging . notify ( 'actionCompleted' , { result : response } ) ;
44- } else {
45- return this . messaging . notify ( 'actionError' , { error : 'No response found, exceptions: ' + exceptions . join ( ', ' ) } ) ;
46- }
47- } catch ( e ) {
48- console . log ( 'unhandled exception: ' , e ) ;
49- return this . messaging . notify ( 'actionError' , { error : e . toString ( ) } ) ;
39+ return this . messaging . notify ( 'actionCompleted' , { result : response } ) ;
40+ } else {
41+ return this . messaging . notify ( 'actionError' , { error : 'No response found, exceptions: ' + exceptions . join ( ', ' ) } ) ;
5042 }
43+ } catch ( e ) {
44+ console . log ( 'unhandled exception: ' , e ) ;
45+ return this . messaging . notify ( 'actionError' , { error : e . toString ( ) } ) ;
5146 }
47+ }
5248
53- /**
54- * Recursively execute actions with the same dataset, collecting all results/exceptions for
55- * later analysis
56- * @param {any } action
57- * @param {Record<string, any> } data
58- * @param {any } retryConfig
59- * @return {Promise<{results: ActionResponse[], exceptions: string[]}> }
60- */
61- async exec ( action , data , retryConfig ) {
62- const { result, exceptions } = await retry ( ( ) => execute ( action , data , document ) , retryConfig ) ;
49+ /**
50+ * Recursively execute actions with the same dataset, collecting all results/exceptions for
51+ * later analysis
52+ * @param {any } action
53+ * @param {Record<string, any> } data
54+ * @param {any } retryConfig
55+ * @return {Promise<{results: ActionResponse[], exceptions: string[]}> }
56+ */
57+ async exec ( action , data , retryConfig ) {
58+ const { result, exceptions } = await retry ( ( ) => execute ( action , data , document ) , retryConfig ) ;
6359
64- if ( result ) {
65- if ( 'success' in result && Array . isArray ( result . success . next ) ) {
66- const nextResults = [ ] ;
67- const nextExceptions = [ ] ;
60+ if ( result ) {
61+ if ( 'success' in result && Array . isArray ( result . success . next ) ) {
62+ const nextResults = [ ] ;
63+ const nextExceptions = [ ] ;
6864
69- for ( const nextAction of result . success . next ) {
70- const { results : subResults , exceptions : subExceptions } = await this . exec ( nextAction , data , retryConfig ) ;
65+ for ( const nextAction of result . success . next ) {
66+ const { results : subResults , exceptions : subExceptions } = await this . exec ( nextAction , data , retryConfig ) ;
7167
72- nextResults . push ( ...subResults ) ;
73- nextExceptions . push ( ...subExceptions ) ;
74- }
75- return { results : [ result , ...nextResults ] , exceptions : exceptions . concat ( nextExceptions ) } ;
68+ nextResults . push ( ...subResults ) ;
69+ nextExceptions . push ( ...subExceptions ) ;
7670 }
77- return { results : [ result ] , exceptions : [ ] } ;
71+ return { results : [ result , ... nextResults ] , exceptions : exceptions . concat ( nextExceptions ) } ;
7872 }
79- return { results : [ ] , exceptions } ;
73+ return { results : [ result ] , exceptions : [ ] } ;
8074 }
81- } ;
75+ return { results : [ ] , exceptions } ;
76+ }
8277}
8378
8479/**
8580 * @typedef {import("./broker-protection/types.js").ActionResponse } ActionResponse
8681 */
87- export default class BrokerProtection extends ActionExecutorMixin ( ContentFeature ) {
82+ export default class BrokerProtection extends ActionExecutorBase {
8883 init ( ) {
8984 this . messaging . subscribe ( 'onActionReceived' , async ( /** @type {any } */ params ) => {
9085 const { action, data } = params . state ;
0 commit comments