Commit 30000648 authored by Travis Nuttall's avatar Travis Nuttall

rename to Thread

parent 5987c1e3
...@@ -7,11 +7,11 @@ import { ...@@ -7,11 +7,11 @@ import {
TouchableOpacity, TouchableOpacity,
} from 'react-native'; } from 'react-native';
import { worker } from 'react-native-thread'; import { Thread } from 'react-native-thread';
class ThreadExample extends Component { class ThreadExample extends Component {
componentDidMount() { componentDidMount() {
this.worker = new worker('worker.js'); this.worker= new Thread('worker.js');
this.worker.onmessage = (message) => { this.worker.onmessage = (message) => {
console.log("Got message from worker", message); console.log("Got message from worker", message);
......
export self from './js/self'; export { default as self } from './js/self';
export worker from './js/worker'; export { default as Thread } from './js/Thread';
// import { NativeModules } from 'react-native';
//
// const { RNThread } = NativeModules;
//
// export default RNThread;
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
objects = { objects = {
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
C039B44A1F6CF2A4009CB65E /* WorkerManager.m in Sources */ = {isa = PBXBuildFile; fileRef = C039B4471F6CF2A4009CB65E /* WorkerManager.m */; }; C039B44A1F6CF2A4009CB65E /* ThreadManager.m in Sources */ = {isa = PBXBuildFile; fileRef = C039B4471F6CF2A4009CB65E /* ThreadManager.m */; };
C039B44B1F6CF2A4009CB65E /* WorkerSelfManager.m in Sources */ = {isa = PBXBuildFile; fileRef = C039B4491F6CF2A4009CB65E /* WorkerSelfManager.m */; }; C039B44B1F6CF2A4009CB65E /* WorkerSelfManager.m in Sources */ = {isa = PBXBuildFile; fileRef = C039B4491F6CF2A4009CB65E /* WorkerSelfManager.m */; };
/* End PBXBuildFile section */ /* End PBXBuildFile section */
...@@ -25,9 +25,9 @@ ...@@ -25,9 +25,9 @@
/* Begin PBXFileReference section */ /* Begin PBXFileReference section */
134814201AA4EA6300B7C361 /* libRNThread.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRNThread.a; sourceTree = BUILT_PRODUCTS_DIR; }; 134814201AA4EA6300B7C361 /* libRNThread.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRNThread.a; sourceTree = BUILT_PRODUCTS_DIR; };
C039B4461F6CF2A4009CB65E /* WorkerManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WorkerManager.h; sourceTree = "<group>"; }; C039B4461F6CF2A4009CB65E /* ThreadManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ThreadManager.h; sourceTree = "<group>"; };
C039B4471F6CF2A4009CB65E /* WorkerManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WorkerManager.m; sourceTree = "<group>"; }; C039B4471F6CF2A4009CB65E /* ThreadManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ThreadManager.m; sourceTree = "<group>"; };
C039B4481F6CF2A4009CB65E /* WorkerSelfManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WorkerSelfManager.h; sourceTree = "<group>"; }; C039B4481F6CF2A4009CB65E /* ThreadSelfManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ThreadSelfManager.h; sourceTree = "<group>"; };
C039B4491F6CF2A4009CB65E /* WorkerSelfManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WorkerSelfManager.m; sourceTree = "<group>"; }; C039B4491F6CF2A4009CB65E /* WorkerSelfManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WorkerSelfManager.m; sourceTree = "<group>"; };
/* End PBXFileReference section */ /* End PBXFileReference section */
...@@ -53,9 +53,9 @@ ...@@ -53,9 +53,9 @@
58B511D21A9E6C8500147676 = { 58B511D21A9E6C8500147676 = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
C039B4461F6CF2A4009CB65E /* WorkerManager.h */, C039B4461F6CF2A4009CB65E /* ThreadManager.h */,
C039B4471F6CF2A4009CB65E /* WorkerManager.m */, C039B4471F6CF2A4009CB65E /* ThreadManager.m */,
C039B4481F6CF2A4009CB65E /* WorkerSelfManager.h */, C039B4481F6CF2A4009CB65E /* ThreadSelfManager.h */,
C039B4491F6CF2A4009CB65E /* WorkerSelfManager.m */, C039B4491F6CF2A4009CB65E /* WorkerSelfManager.m */,
134814211AA4EA7D00B7C361 /* Products */, 134814211AA4EA7D00B7C361 /* Products */,
); );
...@@ -117,7 +117,7 @@ ...@@ -117,7 +117,7 @@
isa = PBXSourcesBuildPhase; isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
C039B44A1F6CF2A4009CB65E /* WorkerManager.m in Sources */, C039B44A1F6CF2A4009CB65E /* ThreadManager.m in Sources */,
C039B44B1F6CF2A4009CB65E /* WorkerSelfManager.m in Sources */, C039B44B1F6CF2A4009CB65E /* WorkerSelfManager.m in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
......
#ifndef WorkerManager_h #ifndef ThreadManager_h
#define WorkerManager_h #define ThreadManager_h
#import "WorkerSelfManager.h" #import "ThreadSelfManager.h"
#import <React/RCTBridge.h> #import <React/RCTBridge.h>
#import <React/RCTBridge+Private.h> #import <React/RCTBridge+Private.h>
#import <React/RCTEventDispatcher.h> #import <React/RCTEventDispatcher.h>
#import <React/RCTBundleURLProvider.h> #import <React/RCTBundleURLProvider.h>
@interface WorkerManager : NSObject <RCTBridgeModule> @interface ThreadManager : NSObject <RCTBridgeModule>
@end @end
#endif #endif
#import "ThreadManager.h"
#include <stdlib.h>
@implementation ThreadManager
@synthesize bridge = _bridge;
NSMutableDictionary *threads;
RCT_EXPORT_MODULE();
RCT_REMAP_METHOD(startThread,
name: (NSString *)name
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
if (threads == nil) {
threads = [[NSMutableDictionary alloc] init];
}
int threadId = abs(arc4random());
NSURL *threadURL = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:name fallbackResource:nil];
NSLog(@"starting Thread %@", [threadURL absoluteString]);
RCTBridge *threadBridge = [[RCTBridge alloc] initWithBundleURL:threadURL
moduleProvider:nil
launchOptions:nil];
ThreadSelfManager *threadSelf = [threadBridge moduleForName:@"ThreadSelfManager"];
[threadSelf setThreadId:threadId];
[threadSelf setParentBridge:self.bridge];
[threads setObject:threadBridge forKey:[NSNumber numberWithInt:threadId]];
resolve([NSNumber numberWithInt:threadId]);
}
RCT_EXPORT_METHOD(stopThread:(int)threadId)
{
if (threads == nil) {
NSLog(@"Empty list of threads. abort stopping thread with id %i", threadId);
return;
}
RCTBridge *threadBridge = threads[[NSNumber numberWithInt:threadId]];
if (threadBridge == nil) {
NSLog(@"Thread is NIl. abort stopping thread with id %i", threadId);
return;
}
[threadBridge invalidate];
[threads removeObjectForKey:[NSNumber numberWithInt:threadId]];
}
RCT_EXPORT_METHOD(postThreadMessage: (int)threadId message:(NSString *)message)
{
if (threads == nil) {
NSLog(@"Empty list of threads. abort posting to thread with id %i", threadId);
return;
}
RCTBridge *threadBridge = threads[[NSNumber numberWithInt:threadId]];
if (threadBridge == nil) {
NSLog(@"Thread is NIl. abort posting to thread with id %i", threadId);
return;
}
[threadBridge.eventDispatcher sendAppEventWithName:@"ThreadMessage"
body:message];
}
- (void)invalidate {
if (threads == nil) {
return;
}
for (NSNumber *threadId in threads) {
RCTBridge *threadBridge = threads[threadId];
[threadBridge invalidate];
}
[threads removeAllObjects];
threads = nil;
}
@end
#ifndef WorkerSelfManager_h #ifndef ThreadSelfManager_h
#define WorkerSelfManager_h #define ThreadSelfManager_h
#import <React/RCTBridge.h> #import <React/RCTBridge.h>
#import <React/RCTBridge+Private.h> #import <React/RCTBridge+Private.h>
#import <React/RCTEventDispatcher.h> #import <React/RCTEventDispatcher.h>
@interface WorkerSelfManager : NSObject <RCTBridgeModule> @interface ThreadSelfManager : NSObject <RCTBridgeModule>
@property int workerId; @property int threadId;
@property RCTBridge *parentBridge; @property RCTBridge *parentBridge;
@end @end
......
#import "WorkerManager.h"
#include <stdlib.h>
@implementation WorkerManager
@synthesize bridge = _bridge;
NSMutableDictionary *workers;
RCT_EXPORT_MODULE();
RCT_REMAP_METHOD(startWorker,
name: (NSString *)name
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
if (workers == nil) {
workers = [[NSMutableDictionary alloc] init];
}
int workerId = abs(arc4random());
NSURL *workerURL = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:name fallbackResource:nil];
NSLog(@"starting Worker %@", [workerURL absoluteString]);
RCTBridge *workerBridge = [[RCTBridge alloc] initWithBundleURL:workerURL
moduleProvider:nil
launchOptions:nil];
WorkerSelfManager *workerSelf = [workerBridge moduleForName:@"WorkerSelfManager"];
[workerSelf setWorkerId:workerId];
[workerSelf setParentBridge:self.bridge];
[workers setObject:workerBridge forKey:[NSNumber numberWithInt:workerId]];
resolve([NSNumber numberWithInt:workerId]);
}
RCT_EXPORT_METHOD(stopWorker:(int)workerId)
{
if (workers == nil) {
NSLog(@"Empty list of workers. abort stopping worker with id %i", workerId);
return;
}
RCTBridge *workerBridge = workers[[NSNumber numberWithInt:workerId]];
if (workerBridge == nil) {
NSLog(@"Worker is NIl. abort stopping worker with id %i", workerId);
return;
}
[workerBridge invalidate];
[workers removeObjectForKey:[NSNumber numberWithInt:workerId]];
}
RCT_EXPORT_METHOD(postWorkerMessage: (int)workerId message:(NSString *)message)
{
if (workers == nil) {
NSLog(@"Empty list of workers. abort posting to worker with id %i", workerId);
return;
}
RCTBridge *workerBridge = workers[[NSNumber numberWithInt:workerId]];
if (workerBridge == nil) {
NSLog(@"Worker is NIl. abort posting to worker with id %i", workerId);
return;
}
[workerBridge.eventDispatcher sendAppEventWithName:@"WorkerMessage"
body:message];
}
- (void)invalidate {
if (workers == nil) {
return;
}
for (NSNumber *workerId in workers) {
RCTBridge *workerBridge = workers[workerId];
[workerBridge invalidate];
}
[workers removeAllObjects];
workers = nil;
}
@end
#import "WorkerSelfManager.h" #import "ThreadSelfManager.h"
#include <stdlib.h> #include <stdlib.h>
@implementation WorkerSelfManager @implementation ThreadSelfManager
RCT_EXPORT_MODULE(); RCT_EXPORT_MODULE();
@synthesize bridge = _bridge; @synthesize bridge = _bridge;
@synthesize parentBridge = _parentBridge; @synthesize parentBridge = _parentBridge;
@synthesize workerId = _workerId; @synthesize threadId = _threadId;
RCT_EXPORT_METHOD(postMessage: (NSString *)message) RCT_EXPORT_METHOD(postMessage: (NSString *)message)
{ {
if (self.parentBridge == nil) { if (self.parentBridge == nil) {
NSLog(@"No parent bridge defined - abord sending worker message"); NSLog(@"No parent bridge defined - abord sending thread message");
return; return;
} }
NSString *eventName = [NSString stringWithFormat:@"Worker%i", self.workerId]; NSString *eventName = [NSString stringWithFormat:@"thread%i", self.threadId];
[self.parentBridge.eventDispatcher sendAppEventWithName:eventName [self.parentBridge.eventDispatcher sendAppEventWithName:eventName
body:message]; body:message];
......
...@@ -3,17 +3,17 @@ import { ...@@ -3,17 +3,17 @@ import {
DeviceEventEmitter, DeviceEventEmitter,
} from 'react-native'; } from 'react-native';
const { WorkerManager } = NativeModules; const { ThreadManager } = NativeModules;
export default class Worker { export default class Thread {
constructor(jsPath) { constructor(jsPath) {
if (!jsPath || !jsPath.endsWith('.js')) { if (!jsPath || !jsPath.endsWith('.js')) {
throw new Error("Invalid worker path. Only js files are supported"); throw new Error("Invalid worker path. Only js files are supported");
} }
this.id = WorkerManager.startWorker(jsPath.replace(".js", "")) this.id = ThreadManager.startThread(jsPath.replace(".js", ""))
.then(id => { .then(id => {
DeviceEventEmitter.addListener(`Worker${id}`, (message) => { DeviceEventEmitter.addListener(`Thread${id}`, (message) => {
!!message && this.onmessage && this.onmessage(message); !!message && this.onmessage && this.onmessage(message);
}); });
return id; return id;
...@@ -22,10 +22,10 @@ export default class Worker { ...@@ -22,10 +22,10 @@ export default class Worker {
} }
postMessage(message) { postMessage(message) {
this.id.then(id => WorkerManager.postWorkerMessage(id, message)); this.id.then(id => ThreadManager.postThreadMessage(id, message));
} }
terminate() { terminate() {
this.id.then(WorkerManager.stopWorker); this.id.then(ThreadManager.stopThread);
} }
} }
...@@ -3,18 +3,18 @@ import { ...@@ -3,18 +3,18 @@ import {
DeviceEventEmitter, DeviceEventEmitter,
} from 'react-native'; } from 'react-native';
const { WorkerSelfManager } = NativeModules; const { ThreadSelfManager } = NativeModules;
const self = { const self = {
onmessage: null, onmessage: null,
postMessage: (message) => { postMessage: (message) => {
if (!message) { return; } if (!message) { return; }
WorkerSelfManager.postMessage(message); ThreadSelfManager.postMessage(message);
} }
}; };
DeviceEventEmitter.addListener("WorkerMessage", (message) => { DeviceEventEmitter.addListener("ThreadMessage", (message) => {
!!message && self.onmessage && self.onmessage(message); !!message && self.onmessage && self.onmessage(message);
}); });
......
...@@ -20,6 +20,5 @@ ...@@ -20,6 +20,5 @@
"peerDependencies": { "peerDependencies": {
"react-native": "^0.41.2", "react-native": "^0.41.2",
"react-native-windows": "0.41.0-rc.1" "react-native-windows": "0.41.0-rc.1"
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment