forked from facebook/react-native
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WebWorkers: Add c++ API for working with MessageQueueThread
Summary: public Adds a fbjni API similar to JNativeRunnable so that you can post ##std::function<void()>## to MessageQueueThreads. Reviewed By: lexs Differential Revision: D2779094 fb-gh-sync-id: 8f873fc93fb6b817268e9422c0b6f85c3e453676
- Loading branch information
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
ReactAndroid/src/main/jni/react/jni/JMessageQueueThread.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2004-present Facebook. All Rights Reserved. | ||
|
||
#include "JMessageQueueThread.h" | ||
|
||
#include <fb/log.h> | ||
#include <folly/Memory.h> | ||
#include <jni/fbjni.h> | ||
|
||
#include "JNativeRunnable.h" | ||
|
||
namespace facebook { | ||
namespace react { | ||
|
||
JMessageQueueThread::JMessageQueueThread(alias_ref<MessageQueueThread::javaobject> jobj) : | ||
m_jobj(make_global(jobj)) { | ||
} | ||
|
||
void JMessageQueueThread::runOnQueue(std::function<void()>&& runnable) { | ||
static auto method = MessageQueueThread::javaClassStatic()-> | ||
getMethod<void(Runnable::javaobject)>("runOnQueue"); | ||
method(m_jobj, JNativeRunnable::newObjectCxxArgs(runnable).get()); | ||
} | ||
|
||
/* static */ | ||
std::unique_ptr<JMessageQueueThread> JMessageQueueThread::currentMessageQueueThread() { | ||
static auto method = MessageQueueThreadRegistry::javaClassStatic()-> | ||
getStaticMethod<MessageQueueThread::javaobject()>("myMessageQueueThread"); | ||
return folly::make_unique<JMessageQueueThread>(method(MessageQueueThreadRegistry::javaClassStatic())); | ||
} | ||
|
||
} } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2004-present Facebook. All Rights Reserved. | ||
|
||
#pragma once | ||
|
||
#include <functional> | ||
|
||
#include <jni/fbjni.h> | ||
|
||
using namespace facebook::jni; | ||
|
||
namespace facebook { | ||
namespace react { | ||
|
||
class MessageQueueThread : public jni::JavaClass<MessageQueueThread> { | ||
public: | ||
static constexpr auto kJavaDescriptor = "Lcom/facebook/react/bridge/queue/MessageQueueThread;"; | ||
}; | ||
|
||
class JMessageQueueThread { | ||
public: | ||
JMessageQueueThread(alias_ref<MessageQueueThread::javaobject> jobj); | ||
|
||
/** | ||
* Enqueues the given function to run on this MessageQueueThread. | ||
*/ | ||
void runOnQueue(std::function<void()>&& runnable); | ||
|
||
/** | ||
* Returns the current MessageQueueThread that owns this thread. | ||
*/ | ||
static std::unique_ptr<JMessageQueueThread> currentMessageQueueThread(); | ||
private: | ||
global_ref<MessageQueueThread::javaobject> m_jobj; | ||
}; | ||
|
||
class MessageQueueThreadRegistry : public jni::JavaClass<MessageQueueThreadRegistry> { | ||
public: | ||
static constexpr auto kJavaDescriptor = "Lcom/facebook/react/bridge/queue/MessageQueueThreadRegistry;"; | ||
}; | ||
|
||
} } |