mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
c80192c2ab
Summary: The purpose of `EventBeat` is handling an asynchronous callback to itself which is being delivered on some 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`. Reviewed By: JoshuaGross Differential Revision: D17128549 fbshipit-source-id: 7ed34fd865430975157fd362f51c4a3d64214430
38 lines
922 B
C++
38 lines
922 B
C++
// Copyright (c) Facebook, Inc. and its affiliates.
|
|
|
|
// This source code is licensed under the MIT license found in the
|
|
// LICENSE file in the root directory of this source tree.
|
|
|
|
#pragma once
|
|
|
|
#include <CoreFoundation/CFRunLoop.h>
|
|
#include <CoreFoundation/CoreFoundation.h>
|
|
#include <react/core/EventBeat.h>
|
|
#include <react/utils/RuntimeExecutor.h>
|
|
|
|
namespace facebook {
|
|
namespace react {
|
|
|
|
/*
|
|
* Event beat associated with main run loop cycle.
|
|
* The callback is always called on the main thread.
|
|
*/
|
|
class MainRunLoopEventBeat final : public EventBeat {
|
|
public:
|
|
MainRunLoopEventBeat(
|
|
EventBeat::SharedOwnerBox const &ownerBox,
|
|
RuntimeExecutor runtimeExecutor);
|
|
~MainRunLoopEventBeat();
|
|
|
|
void induce() const override;
|
|
|
|
private:
|
|
void lockExecutorAndBeat() const;
|
|
|
|
const RuntimeExecutor runtimeExecutor_;
|
|
CFRunLoopObserverRef mainRunLoopObserver_;
|
|
};
|
|
|
|
} // namespace react
|
|
} // namespace facebook
|