Skip to content

Commit b9f4c18

Browse files
committed
refactor: extend actionexecutor
1 parent 1987305 commit b9f4c18

File tree

3 files changed

+61
-66
lines changed

3 files changed

+61
-66
lines changed

injected/src/features/autofill-import.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import ContentFeature from '../content-feature';
21
import { isBeingFramed, withRetry } from '../utils';
3-
import { ActionExecutorMixin } from './broker-protection';
2+
import { ActionExecutorBase } from './broker-protection';
43

54
export const ANIMATION_DURATION_MS = 1000;
65
export const ANIMATION_ITERATIONS = Infinity;
@@ -35,7 +34,7 @@ const TAKEOUT_DOWNLOAD_URL_BASE = '/takeout/download';
3534
* 2. Find the element to animate based on the path - using structural selectors first and then fallback to label texts),
3635
* 3. Animate the element, or tap it if it should be autotapped.
3736
*/
38-
export default class AutofillImport extends ActionExecutorMixin(ContentFeature) {
37+
export default class AutofillImport extends ActionExecutorBase {
3938
#exportButtonSettings;
4039

4140
#settingsButtonSettings;

injected/src/features/broker-protection.js

Lines changed: 58 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,88 +3,83 @@ import { execute } from './broker-protection/execute.js';
33
import { retry } from '../timer-utils.js';
44
import { 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;

messaging/lib/android-adsjs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ export class AndroidAdsjsMessagingConfig {
189189
* @internal
190190
*/
191191
sendMessageThrows(message) {
192+
console.log('DEEP sendMessageThrows', message);
192193
if (!this.objectName) {
193194
throw new Error('Object name not set for WebMessageListener');
194195
}

0 commit comments

Comments
 (0)