summaryrefslogtreecommitdiff
path: root/shared/metrics-8/node_modules/@jet/engine/lib/actions/action-dispatcher.js
blob: 1280b0615bb9e1c67173e2a41167c06c7e0f31b9 (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ActionDispatcher = void 0;
const optional_1 = require("@jet/environment/types/optional");
class ActionDispatcher {
    constructor(metricsPipeline) {
        this.implementations = {};
        this.metricsPipeline = metricsPipeline;
    }
    register(type, implementation) {
        if (type in this.implementations) {
            console.error(`An implementation is already registered for ${type}`);
        }
        this.implementations[type] = implementation;
    }
    async perform(action, metricsBehavior) {
        if (!(action.$kind in this.implementations)) {
            // 1. If there is no implementation registered for the type of action
            //    we were passed, we check for a chained dispatcher to forward to.
            //    If one is found, we forward this call without doing any work.
            //    If none is found, we give up.
            if (optional_1.isSome(this.next)) {
                return await this.next.perform(action, metricsBehavior);
            }
            else {
                return "unsupported";
            }
        }
        // 2. We have an implementation for the action we were given.
        //    We are responsible for processing metrics for that action.
        this.processMetrics(action, metricsBehavior);
        if (optional_1.isSome(this.next)) {
            // 3a. If we have another dispatcher we are chained to, we forward to it
            //     if the implementation we have for the given action decides it cannot
            //     support performing it.
            const outcome = await this.implementations[action.$kind](action);
            if (outcome === "unsupported") {
                return await this.next.perform(action, { behavior: "notProcessed" });
            }
            else {
                return outcome;
            }
        }
        else {
            // 3b. We hand off control to the implementation we have for the given action type.
            //     If the implementation cannot perform the action, we give up.
            return await this.implementations[action.$kind](action);
        }
    }
    processMetrics(action, metricsBehavior) {
        if (metricsBehavior.behavior === "notProcessed") {
            return;
        }
        const actionMetrics = action.actionMetrics;
        const context = {
            customMetrics: actionMetrics.custom,
            pageFields: metricsBehavior.context.pageFields,
        };
        action.actionMetrics.data.forEach((data) => {
            this.metricsPipeline.process(data, context);
        });
    }
}
exports.ActionDispatcher = ActionDispatcher;