summaryrefslogtreecommitdiff
path: root/shared/metrics-8/node_modules/@jet/engine/lib/metrics/presenters/page-metrics-presenter.js
blob: c04bbd6d46f3df4135a5ed0076fd1ee9fc19efba (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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PageMetricsPresenter = void 0;
const optional_1 = require("@jet/environment/types/optional");
class PageMetricsPresenter {
    constructor(metricsPipeline) {
        this.metricsPipeline = metricsPipeline;
        this.isViewAppeared = false;
    }
    set pageMetrics(pageMetrics) {
        this.pageMetricsStore = pageMetrics;
        if (optional_1.isSome(pageMetrics) && this.isViewAppeared) {
            this.processInstructions("pageEnter");
        }
    }
    get pageMetrics() {
        return this.pageMetricsStore;
    }
    async processInstructions(invocationPoint) {
        var _a, _b, _c;
        if (optional_1.isNothing(this.pageMetrics)) {
            return;
        }
        // istanbul ignore next
        const invocationContext = {
            customMetrics: (_a = this.baseContext) === null || _a === void 0 ? void 0 : _a.customMetrics,
            pageFields: {
                ...(_b = this.baseContext) === null || _b === void 0 ? void 0 : _b.pageFields,
                ...(_c = this.pageMetrics) === null || _c === void 0 ? void 0 : _c.pageFields,
            },
        };
        await Promise.all(this.pageMetrics.instructions.map((instruction) => {
            const { invocationPoints } = instruction;
            if (invocationPoints.length === 0 || !invocationPoints.includes(invocationPoint)) {
                return;
            }
            return this.metricsPipeline.process(instruction.data, invocationContext);
        }));
    }
    async didEnterPage() {
        this.isViewAppeared = true;
        if (optional_1.isSome(this.pageMetrics)) {
            await this.processInstructions("pageEnter");
        }
    }
    async didLeavePage() {
        await this.processInstructions("pageExit");
        this.isViewAppeared = false;
    }
}
exports.PageMetricsPresenter = PageMetricsPresenter;