UNPKG

1.68 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var react_1 = require("react");
4var actions_1 = require("@shopify/app-bridge/actions");
5var hooks_1 = require("../hooks");
6var performance_1 = require("../features/performance");
7var useFeature_1 = require("../features/utilities/useFeature");
8function Performance(_a) {
9 var dispatchPerformanceMetrics = _a.dispatchPerformanceMetrics;
10 var store = useFeature_1.useFeature(performance_1.feature)[0];
11 var hostContext = hooks_1.useHostContext();
12 var subscribe = react_1.useCallback(function (actionType) {
13 var app = hostContext.app, _a = hostContext.config, appId = _a.appId, shopId = _a.shopId;
14 return app.subscribe(actionType, function () {
15 if (!store || !store.timeOrigin) {
16 throw new Error('Failed to compute app load time. Performance store not valid.');
17 }
18 dispatchPerformanceMetrics({
19 appId: appId,
20 shopId: shopId,
21 isFullPageNavigation: store.isFullPageNavigation,
22 loadTime: Date.now() - store.timeOrigin,
23 appLoadType: actionType,
24 });
25 });
26 }, [dispatchPerformanceMetrics, hostContext, store]);
27 react_1.useEffect(function () {
28 var unsubscribeSkeletonLoad = subscribe(actions_1.Performance.ActionType.SKELETON_PAGE_LOAD);
29 var unsubscribeFullLoad = subscribe(actions_1.Performance.ActionType.FULL_PAGE_LOAD);
30 return function () {
31 unsubscribeSkeletonLoad();
32 unsubscribeFullLoad();
33 };
34 }, [subscribe]);
35 return null;
36}
37exports.default = Performance;