Skip to content

Commit

Permalink
add comment to EventBeat clarifying its use (#47065)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #47065

changelog: [internal]

Just a comment explaining intended use.

Differential Revision: D64469189
  • Loading branch information
sammy-SC authored and facebook-github-bot committed Oct 16, 2024
1 parent fa50e13 commit 5fc40f1
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions packages/react-native/ReactCommon/react/renderer/core/EventBeat.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,33 @@ namespace facebook::react {
/*
* Event Beat serves two interleaving purposes: synchronization of event queues
* and ensuring that event dispatching happens on proper threads.
*
* EventBeat is meant to be subclassed by platform-specific implementations.
* The platform-specific implementation must call EventBeat::induce(). The
* appropriate time to call induce is when the host platform events were queued
* in the EventQueue for a given UI frame. For example, on iOS,
* EventBeat::induce() is called before the main run loop goes to sleep. On
* Android, EventBeat::induce() is called from Android's Choreographer.
*/
class EventBeat {
public:
/*
* The concept of `Owner`
* The purpose of `EventBeat` is handling an asynchronous callback to itself
* which is being delivered on some different thread. That brings a challenge
* which is being delivered on a different thread. That brings a challenge
* of ensuring that the `EventBeat` object stays valid during the timeframe of
* callback execution. The concept of Owner helps with that. The owner is a
* shared pointer that retains (probably indirectly) the `EventBeat` object.
* To ensure the correctness of the call, `EventBeat` retains the owner
* (practically creating a retain cycle) during executing the callback. In
* case if the pointer to the owner already null, `EventBeat` skips executing
* the callback. It's impossible to retain itself directly or refer to the
* shared pointer to itself from a constructor. `OwnerBox` is designed to work
* around this issue; it allows to store the pointer later, right after the
* creation of some other object that owns an `EventBeat`.
* weakly during executing the callback. In case if the pointer to the owner
* already null, `EventBeat` skips executing the callback. It's impossible to
* retain itself directly or refer to the shared pointer to itself from a
* constructor. `OwnerBox` is designed to work around this issue; it allows to
* store the pointer later, right after the creation of some other object that
* owns an `EventBeat`.
*/
using Owner = std::weak_ptr<const void>;
struct OwnerBox {
Owner owner;
std::weak_ptr<const void> owner;
};

using Factory = std::function<std::unique_ptr<EventBeat>(
Expand Down

0 comments on commit 5fc40f1

Please sign in to comment.