summaryrefslogtreecommitdiff
path: root/node_modules/@jet/environment/metrics/fetch-timing-metrics-builder.js
blob: 7a658fffaf1e4b2cf18cefac602da6bf3a4474d5 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchTimingMetricsBuilderType = exports.FetchTimingMetricsBuilder = void 0;
const optional_1 = require("../types/optional");
const metatype_1 = require("../util/metatype");
const promise_1 = require("../util/promise");
/** Object to collect `FetchTimingMetrics`, in order to decorate a model object with these metrics. */
class FetchTimingMetricsBuilder {
    constructor() {
        this.metrics = [];
    }
    /**
     * Collects the timing metrics from the response,
     * then execute code for parsing while capturing timing points around it,
     * adding those parse times to the first timing metrics.
     */
    measureParsing(response, body) {
        const responseTimingMetrics = response.metrics.length > 0 ? [...response.metrics] : [];
        const parseStartTime = Date.now();
        const result = body(response);
        const parseEndTime = Date.now();
        // MAINTAINERS NOTE:
        //   Follows app store approach to add additional timing points to the first item.
        //   This may not be strictly correct, and we should revisit this later.
        if (responseTimingMetrics.length > 0) {
            responseTimingMetrics[0].parseStartTime = parseStartTime;
            responseTimingMetrics[0].parseEndTime = parseEndTime;
        }
        this.metrics.push(...responseTimingMetrics);
        return result;
    }
    /**
     * Execute code for model construction while capturing timing points around it,
     * adding those model construction times to the first timing metrics.
     *
     * Use this method when model construction is a synchronous operation.
     * Use `measureModelConstructionAsync()` when model construction is an asynchronous operation.
     */
    measureModelConstruction(body) {
        const buildModelStartTime = Date.now();
        const result = body();
        const buildModelEndTime = Date.now();
        this.saveModelConstructionTimes(buildModelStartTime, buildModelEndTime);
        return result;
    }
    /**
     * Execute and await code for an asynchronous model construction operation while capturing timing points around it,
     * adding those model construction times to the first timing metrics.
     *
     * Use this method when model construction is an asynchronous operation.
     * Use `measureModelConstruction()` when model construction is a synchronous operation.
     */
    async measureModelConstructionAsync(body) {
        const buildModelStartTime = Date.now();
        const result = await body();
        const buildModelEndTime = Date.now();
        this.saveModelConstructionTimes(buildModelStartTime, buildModelEndTime);
        return result;
    }
    saveModelConstructionTimes(startTime, endTime) {
        // MAINTAINERS NOTE:
        //   Follows app store approach to add additional timing points to the first item.
        //   This may not be strictly correct, and we should revisit this later.
        if (this.metrics.length > 0) {
            this.metrics[0].modelConstructionStartTime = startTime;
            this.metrics[0].modelConstructionEndTime = endTime;
        }
        else {
            this.metrics.push({
                modelConstructionStartTime: startTime,
                modelConstructionEndTime: endTime,
            });
        }
    }
    /** Add the recorded `FetchTimingMetrics` as an additional property on the model. */
    decorate(model) {
        if ((0, optional_1.isNothing)(model)) {
            throw new Error("Cannot decorate null or undefined");
        }
        if (typeof model !== "object") {
            throw new Error("View model to decorate must be an object");
        }
        if ((0, promise_1.isPromise)(model)) {
            // TypeScript compiler may be able to enforce this at compile time.
            // A newer version of TypeScript which supports `Awaited` type may help.
            throw new Error("Cannot decorate a Promise object");
        }
        if (this.metrics.length > 0) {
            model["$networkPerformance"] = this.metrics;
        }
    }
}
exports.FetchTimingMetricsBuilder = FetchTimingMetricsBuilder;
/** Metatype for the `FetchTimingMetricsBuilder`, for adding to an object graph. */
exports.fetchTimingMetricsBuilderType = (0, metatype_1.makeMetatype)("jet-engine:fetchTimingMetricsBuilder");
//# sourceMappingURL=fetch-timing-metrics-builder.js.map