mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-02-11 22:32:38 +08:00
WebWorkers: Implement initial WebWorkers API
Summary: public Implements a basic WebWorkers API that allows posting messages between the main JS thread and a worker background thread. It follows the existing webworkers API from JS. Currently passed memory needs to be JSON serializable and is copied (unfortunately, this is what webkit does as well, but with a more advanced serialization/deserialization process). There are a lot of TODO's: I'll add tasks for them once this is accepted. Reviewed By: lexs Differential Revision: D2779349 fb-gh-sync-id: 8ed04c115d36acf0264ef1f6a12a65dd0c14ff18
This commit is contained in:
committed by
facebook-github-bot-4
parent
dd60964736
commit
72d1826ae3
@@ -402,7 +402,8 @@ public class CatalystInstanceImpl implements CatalystInstance {
|
||||
|
||||
private void decrementPendingJSCalls() {
|
||||
int newPendingCalls = mPendingJSCalls.decrementAndGet();
|
||||
Assertions.assertCondition(newPendingCalls >= 0);
|
||||
// TODO(9604406): handle case of web workers injecting messages to main thread
|
||||
//Assertions.assertCondition(newPendingCalls >= 0);
|
||||
boolean isNowIdle = newPendingCalls == 0;
|
||||
Systrace.traceCounter(
|
||||
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Copyright (c) 2015-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
package com.facebook.react.bridge.queue;
|
||||
|
||||
/**
|
||||
* An Exception handler that posts the Exception to be thrown on the given delegate
|
||||
* MessageQueueThread.
|
||||
*/
|
||||
public class ProxyQueueThreadExceptionHandler implements QueueThreadExceptionHandler {
|
||||
|
||||
private final MessageQueueThread mDelegateThread;
|
||||
|
||||
public ProxyQueueThreadExceptionHandler(MessageQueueThread delegateThread) {
|
||||
mDelegateThread = delegateThread;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleException(final Exception e) {
|
||||
mDelegateThread.runOnQueue(
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* Copyright (c) 2015-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
package com.facebook.react.bridge.webworkers;
|
||||
|
||||
import com.facebook.proguard.annotations.DoNotStrip;
|
||||
import com.facebook.react.bridge.queue.MessageQueueThread;
|
||||
import com.facebook.react.bridge.queue.MessageQueueThreadImpl;
|
||||
import com.facebook.react.bridge.queue.ProxyQueueThreadExceptionHandler;
|
||||
|
||||
@DoNotStrip
|
||||
public class WebWorkers {
|
||||
|
||||
/**
|
||||
* Creates a new MessageQueueThread for a background web worker owned by the JS thread with the
|
||||
* given MessageQueueThread.
|
||||
*/
|
||||
public static MessageQueueThread createWebWorkerThread(int id, MessageQueueThread ownerThread) {
|
||||
return MessageQueueThreadImpl.startNewBackgroundThread(
|
||||
"web-worker-" + id,
|
||||
new ProxyQueueThreadExceptionHandler(ownerThread));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user