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 #include "ABI48_0_0RunLoopObserver.h"
9 
10 #include <ABI48_0_0React/ABI48_0_0debug/ABI48_0_0React_native_assert.h>
11 
12 namespace ABI48_0_0facebook {
13 namespace ABI48_0_0React {
14 
RunLoopObserver(Activity activities,WeakOwner const & owner)15 RunLoopObserver::RunLoopObserver(
16     Activity activities,
17     WeakOwner const &owner) noexcept
18     : activities_(activities), owner_(owner) {}
19 
setDelegate(Delegate const * delegate) const20 void RunLoopObserver::setDelegate(Delegate const *delegate) const noexcept {
21   // We need these constraints to ensure basic thread-safety.
22   ABI48_0_0React_native_assert(delegate && "A delegate must not be `nullptr`.");
23   ABI48_0_0React_native_assert(
24       !delegate_ && "`RunLoopObserver::setDelegate` must be called once.");
25   delegate_ = delegate;
26 }
27 
enable() const28 void RunLoopObserver::enable() const noexcept {
29   if (enabled_) {
30     return;
31   }
32   enabled_ = true;
33 
34   startObserving();
35 }
36 
disable() const37 void RunLoopObserver::disable() const noexcept {
38   if (!enabled_) {
39     return;
40   }
41   enabled_ = false;
42 
43   stopObserving();
44 }
45 
activityDidChange(Activity activity) const46 void RunLoopObserver::activityDidChange(Activity activity) const noexcept {
47   if (!enabled_) {
48     return;
49   }
50 
51   ABI48_0_0React_native_assert(
52       !owner_.expired() &&
53       "`owner_` is null. The caller must `lock` the owner and check it for being not null.");
54 
55   delegate_->activityDidChange(delegate_, activity);
56 }
57 
getOwner() const58 RunLoopObserver::WeakOwner RunLoopObserver::getOwner() const noexcept {
59   return owner_;
60 }
61 
62 } // namespace ABI48_0_0React
63 } // namespace ABI48_0_0facebook
64