1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #pragma once
9 
10 #include <ABI49_0_0FBReactNativeSpec/ABI49_0_0FBReactNativeSpecJSI.h>
11 #include <functional>
12 #include <optional>
13 #include <string>
14 #include <vector>
15 
16 namespace ABI49_0_0facebook::ABI49_0_0React {
17 class PerformanceEntryReporter;
18 
19 #pragma mark - Structs
20 
21 using RawPerformanceEntry = NativePerformanceObserverCxxBaseRawPerformanceEntry<
22     std::string,
23     int32_t,
24     double,
25     double,
26     // For "event" entries only:
27     std::optional<double>,
28     std::optional<double>,
29     std::optional<uint32_t>>;
30 
31 template <>
32 struct Bridging<RawPerformanceEntry>
33     : NativePerformanceObserverCxxBaseRawPerformanceEntryBridging<
34           std::string,
35           int32_t,
36           double,
37           double,
38           std::optional<double>,
39           std::optional<double>,
40           std::optional<uint32_t>> {};
41 
42 using GetPendingEntriesResult =
43     NativePerformanceObserverCxxBaseGetPendingEntriesResult<
44         std::vector<RawPerformanceEntry>,
45         uint32_t>;
46 
47 template <>
48 struct Bridging<GetPendingEntriesResult>
49     : NativePerformanceObserverCxxBaseGetPendingEntriesResultBridging<
50           std::vector<RawPerformanceEntry>,
51           uint32_t> {};
52 
53 #pragma mark - implementation
54 
55 class NativePerformanceObserver
56     : public ABI49_0_0NativePerformanceObserverCxxSpec<NativePerformanceObserver>,
57       std::enable_shared_from_this<NativePerformanceObserver> {
58  public:
59   NativePerformanceObserver(std::shared_ptr<CallInvoker> jsInvoker);
60   ~NativePerformanceObserver();
61 
62   void startReporting(jsi::Runtime &rt, int32_t entryType);
63 
64   void stopReporting(jsi::Runtime &rt, int32_t entryType);
65 
66   GetPendingEntriesResult popPendingEntries(jsi::Runtime &rt);
67 
68   void setOnPerformanceEntryCallback(
69       jsi::Runtime &rt,
70       std::optional<AsyncCallback<>> callback);
71 
72   void logRawEntry(jsi::Runtime &rt, RawPerformanceEntry entry);
73 
74   std::vector<std::pair<std::string, uint32_t>> getEventCounts(
75       jsi::Runtime &rt);
76 
77   void setDurationThreshold(
78       jsi::Runtime &rt,
79       int32_t entryType,
80       double durationThreshold);
81 
82   void clearEntries(
83       jsi::Runtime &rt,
84       int32_t entryType,
85       std::optional<std::string> entryName);
86 
87   std::vector<RawPerformanceEntry> getEntries(
88       jsi::Runtime &rt,
89       std::optional<int32_t> entryType,
90       std::optional<std::string> entryName);
91 
92  private:
93 };
94 
95 } // namespace ABI49_0_0facebook::ABI49_0_0React
96