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 "ABI47_0_0RunLoopObserver.h" 9 10 #include <ABI47_0_0React/ABI47_0_0debug/ABI47_0_0React_native_assert.h> 11 12 namespace ABI47_0_0facebook { 13 namespace ABI47_0_0React { 14 RunLoopObserver(Activity activities,WeakOwner const & owner)15RunLoopObserver::RunLoopObserver( 16 Activity activities, 17 WeakOwner const &owner) noexcept 18 : activities_(activities), owner_(owner) {} 19 setDelegate(Delegate const * delegate) const20void RunLoopObserver::setDelegate(Delegate const *delegate) const noexcept { 21 // We need these constraints to ensure basic thread-safety. 22 ABI47_0_0React_native_assert(delegate && "A delegate must not be `nullptr`."); 23 ABI47_0_0React_native_assert( 24 !delegate_ && "`RunLoopObserver::setDelegate` must be called once."); 25 delegate_ = delegate; 26 } 27 enable() const28void RunLoopObserver::enable() const noexcept { 29 if (enabled_) { 30 return; 31 } 32 enabled_ = true; 33 34 startObserving(); 35 } 36 disable() const37void RunLoopObserver::disable() const noexcept { 38 if (!enabled_) { 39 return; 40 } 41 enabled_ = false; 42 43 stopObserving(); 44 } 45 activityDidChange(Activity activity) const46void RunLoopObserver::activityDidChange(Activity activity) const noexcept { 47 if (!enabled_) { 48 return; 49 } 50 51 ABI47_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() const58RunLoopObserver::WeakOwner RunLoopObserver::getOwner() const noexcept { 59 return owner_; 60 } 61 62 } // namespace ABI47_0_0React 63 } // namespace ABI47_0_0facebook 64