Skip to content

Commit

Permalink
improve PerformanceObserver options bridging
Browse files Browse the repository at this point in the history
  • Loading branch information
robik committed Aug 8, 2024
1 parent fb503d7 commit caf3b65
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jsi::Object NativePerformanceObserver::createObserver(jsi::Runtime& rt, NativePe
return observerObj;
}

void NativePerformanceObserver::observe(jsi::Runtime& rt, jsi::Object observerObj, jsi::Object options) {
void NativePerformanceObserver::observe(jsi::Runtime& rt, jsi::Object observerObj, NativePerformanceObserverObserveOptions options) {
auto observer =
std::dynamic_pointer_cast<PerformanceObserver>(observerObj.getNativeState(rt));

Expand All @@ -59,23 +59,21 @@ void NativePerformanceObserver::observe(jsi::Runtime& rt, jsi::Object observerOb
}

// observer of type multiple
if (options.hasProperty(rt, "entryTypes")) {
if (options.entryTypes.has_value()) {
std::unordered_set<PerformanceEntryType> entryTypes;
auto rawTypes = options.entryTypes.value();

auto types = options.getPropertyAsObject(rt, "entryTypes").asArray(rt);
for (auto i = 0; i < types.size(rt); ++i) {
auto rawValue = types.getValueAtIndex(rt, i).asNumber();
if (auto entryType = parseEntryType(rawValue); entryType) {
for (auto i = 0; i < rawTypes.size(); ++i) {
if (auto entryType = parseEntryType(rawTypes[i]); entryType) {
entryTypes.insert(*entryType);
}
}

observer->observe(types);
observer->observe(entryTypes);
}
else { // single
auto buffered = options.getProperty(rt, "buffered").asBool();
auto type = options.getProperty(rt, "type").asNumber();
if (auto entryType = parseEntryType(type); entryType) {
auto buffered = options.buffered.value_or(false);
if (auto entryType = parseEntryType(options.type.value()); entryType) {
observer->observe(entryType, buffered);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
namespace facebook::react {

using NativePerformanceObserverCallback = AsyncCallback<std::vector<PerformanceEntry>&&, size_t>;
using NativePerformanceObserverObserveOptions = NativePerformanceObserverPerformanceObserverInit<
// entryTypes
std::optional<std::vector<int>>,
// type
std::optional<int>,
// buffered
std::optional<bool>>;

#pragma mark - Structs

Expand All @@ -41,6 +48,9 @@ struct Bridging<PerformanceEntryType> {
}
};

template <>
struct Bridging<NativePerformanceObserverObserveOptions> : NativePerformanceObserverPerformanceObserverInitBridging<NativePerformanceObserverObserveOptions> {};

template <>
struct Bridging<PerformanceEntry>
: NativePerformanceObserverRawPerformanceEntryBridging<PerformanceEntry> {};
Expand All @@ -53,7 +63,7 @@ class NativePerformanceObserver
NativePerformanceObserver(std::shared_ptr<CallInvoker> jsInvoker);

jsi::Object createObserver(jsi::Runtime& rt, NativePerformanceObserverCallback callback);
void observe(jsi::Runtime& rt, jsi::Object observer, jsi::Object options);
void observe(jsi::Runtime& rt, jsi::Object observer, NativePerformanceObserverObserveOptions options);
void disconnect(jsi::Runtime& rt, jsi::Object observer);
std::vector<PerformanceEntry> takeRecords(jsi::Runtime& rt, jsi::Object observerObj);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ PerformanceObserver::~PerformanceObserver() {
}
}

void PerformanceObserver::pushEntry(const facebook::react::PerformanceEntry& entry) {
void PerformanceObserver::pushEntry(const PerformanceEntry& entry) {
buffer_.add(entry);
}

std::vector<PerformanceEntry> PerformanceObserver::takeRecords() {
return buffer_.consume();
}

bool PerformanceObserver::isObserving(facebook::react::PerformanceEntryType type) const {
bool PerformanceObserver::isObserving(PerformanceEntryType type) const {
return observedTypes_.contains(type);
}

void PerformanceObserver::observe(facebook::react::PerformanceEntryType type, bool buffered) {
void PerformanceObserver::observe(PerformanceEntryType type, bool buffered) {
// we assume that `type` was checked on JS side and is correct
observedTypes_.clear();
observedTypes_.insert(type);
Expand Down

0 comments on commit caf3b65

Please sign in to comment.