Skip to content

Commit

Permalink
WebWorkers: Add c++ API for working with MessageQueueThread
Browse files Browse the repository at this point in the history
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
astreet authored and facebook-github-bot-4 committed Jan 12, 2016
1 parent 5fb1966 commit dd60964
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class MessageQueueThreadRegistry {
/**
* @return the MessageQueueThread that owns the current Thread.
*/
@DoNotStrip
public static MessageQueueThread myMessageQueueThread() {
return Assertions.assertNotNull(
sMyMessageQueueThread.get(),
Expand Down
32 changes: 32 additions & 0 deletions ReactAndroid/src/main/jni/react/jni/JMessageQueueThread.cpp
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()));
}

} }

41 changes: 41 additions & 0 deletions ReactAndroid/src/main/jni/react/jni/JMessageQueueThread.h
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;";
};

} }

0 comments on commit dd60964

Please sign in to comment.