summaryrefslogtreecommitdiff
path: root/src/jet/action-handlers/compound-action.ts
blob: 9cf1be02abd7bbf5e67cf6eda00dd8f876b51ed7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import type { LoggerFactory } from '@amp/web-apps-logger';
import type { Jet } from '~/jet';
import type { CompoundAction } from '~/jet/models';

export type Dependencies = {
    jet: Jet;
    logger: LoggerFactory;
};

export async function registerHandler(dependencies: Dependencies) {
    const { jet, logger } = dependencies;

    const log = logger.loggerFor('jet/action-handlers/compound-action');

    jet.onAction('compoundAction', async (action: CompoundAction) => {
        log.info('received CompoundAction:', action);

        const { subactions = [] } = action;

        // Perform actions in sequence
        for (const action of subactions) {
            await jet.perform(action).catch((e) => {
                // Throwing error stops for...of execution
                // TODO: rdar://73165545 (Error Handling Across App)
                throw new Error(
                    `an error occurred while handling CompoundAction: ${e}`,
                );
            });
        }

        return 'performed';
    });
}