mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-24 04:16:00 +08:00
Fabric: Introducing EventBeat concept (and MainRunLoopEventBeat)
Summary: @public EventBeat is an abstraction around proper event scheduling combining proper timing and proper threading. Event Queues use Event Beat to ensure that events are delivered on proper threads and in proper timing (probably batched). Consumers can `request` the next beat and `induce` immediatly beat. MainRunLoopEventBeat implements particular Event Beat synchronized with the main event loop and calling a callback on the main thread. Reviewed By: mdvacca Differential Revision: D8886229 fbshipit-source-id: 1a42fcbf4cd61c6cb4c502890566c98b00226f31
This commit is contained in:
committed by
Facebook Github Bot
parent
122dc37afe
commit
57bbce9bd9
33
React/Fabric/Utils/MainRunLoopEventBeat.h
Normal file
33
React/Fabric/Utils/MainRunLoopEventBeat.h
Normal file
@@ -0,0 +1,33 @@
|
||||
// Copyright (c) 2004-present, Facebook, Inc.
|
||||
|
||||
// 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/CoreFoundation.h>
|
||||
#include <CoreFoundation/CFRunLoop.h>
|
||||
#include <fabric/events/EventBeat.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();
|
||||
~MainRunLoopEventBeat();
|
||||
|
||||
void induce() const override;
|
||||
|
||||
private:
|
||||
CFRunLoopObserverRef mainRunLoopObserver_;
|
||||
};
|
||||
|
||||
} // namespace react
|
||||
} // namespace facebook
|
||||
46
React/Fabric/Utils/MainRunLoopEventBeat.mm
Normal file
46
React/Fabric/Utils/MainRunLoopEventBeat.mm
Normal file
@@ -0,0 +1,46 @@
|
||||
// Copyright (c) 2004-present, Facebook, Inc.
|
||||
|
||||
// This source code is licensed under the MIT license found in the
|
||||
// LICENSE file in the root directory of this source tree.
|
||||
|
||||
#import "MainRunLoopEventBeat.h"
|
||||
|
||||
#import <React/RCTUtils.h>
|
||||
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
MainRunLoopEventBeat::MainRunLoopEventBeat() {
|
||||
mainRunLoopObserver_ =
|
||||
CFRunLoopObserverCreateWithHandler(
|
||||
NULL /* allocator */,
|
||||
kCFRunLoopBeforeWaiting /* activities */,
|
||||
true /* repeats */,
|
||||
0 /* order */,
|
||||
^(CFRunLoopObserverRef observer, CFRunLoopActivity activity) {
|
||||
this->beat();
|
||||
}
|
||||
);
|
||||
|
||||
assert(mainRunLoopObserver_);
|
||||
|
||||
CFRunLoopAddObserver(CFRunLoopGetMain(), mainRunLoopObserver_, kCFRunLoopCommonModes);
|
||||
}
|
||||
|
||||
MainRunLoopEventBeat::~MainRunLoopEventBeat() {
|
||||
CFRunLoopRemoveObserver(CFRunLoopGetMain(), mainRunLoopObserver_, kCFRunLoopCommonModes);
|
||||
CFRelease(mainRunLoopObserver_);
|
||||
}
|
||||
|
||||
void MainRunLoopEventBeat::induce() const {
|
||||
if (!this->isRequested_) {
|
||||
return;
|
||||
}
|
||||
|
||||
RCTExecuteOnMainQueue(^{
|
||||
this->beat();
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace react
|
||||
} // namespace facebook
|
||||
Reference in New Issue
Block a user