Skip to content

Commit ebdd59a

Browse files
sherginfacebook-github-bot
authored andcommitted
Fabric: Initializing EventBeat::OwnerBox with non-null value ahead of time
Summary: We use `EventBeat::OwnerBox` in the `EventBeat` infra to represent weak ownership of classes that contain that, so those classes can ensure own life-time at some critical points. Before this diff, we stored a pointer to EventDispatcher right after we create it. However, some components that we build require to have a valid pointer from the beginning (even before the EventDispatcher is created). To satisfy this requirement, we create a dummy pointer first and use it as an owner, then we merge (share control block) the pointer to an EventDispatcher into that. It works because the owner pointer never gets dereferenced (it's `void *`), so it does not matter which object it represents while it represents correct ownership. In the future, this approach will allow us to remove the concept of `OwnerBox` completely and use just `std::weak_ptr<void>` instead. Changelog: [Internal] Fabric-specific internal change. Reviewed By: mdvacca Differential Revision: D21441109 fbshipit-source-id: 7457c77bd31cd577f38a26e28e27eb7e33b6ad24
1 parent b65b00a commit ebdd59a

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

ReactCommon/fabric/scheduler/Scheduler.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ Scheduler::Scheduler(
3232
auto uiManager = std::make_shared<UIManager>();
3333
auto eventOwnerBox = std::make_shared<EventBeat::OwnerBox>();
3434

35+
// A dummy pointer to share a control block (and life-time) with
36+
// an actual `owner` later.
37+
auto owner = std::make_shared<bool const>(false);
38+
eventOwnerBox->owner = owner;
39+
3540
auto eventPipe = [uiManager](
3641
jsi::Runtime &runtime,
3742
const EventTarget *eventTarget,
@@ -47,14 +52,15 @@ Scheduler::Scheduler(
4752
uiManager->updateState(stateUpdate);
4853
};
4954

50-
eventDispatcher_ = std::make_shared<EventDispatcher>(
55+
auto eventDispatcher = std::make_unique<EventDispatcher const>(
5156
eventPipe,
5257
statePipe,
5358
schedulerToolbox.synchronousEventBeatFactory,
5459
schedulerToolbox.asynchronousEventBeatFactory,
5560
eventOwnerBox);
5661

57-
eventOwnerBox->owner = eventDispatcher_;
62+
eventDispatcher_ =
63+
std::shared_ptr<EventDispatcher const>(owner, eventDispatcher.release());
5864

5965
componentDescriptorRegistry_ = schedulerToolbox.componentRegistryFactory(
6066
eventDispatcher_, schedulerToolbox.contextContainer);

0 commit comments

Comments
 (0)