react-native-workers
Advanced tools
Sorry, the diff of this file is not supported yet
| #ifndef WorkerManager_h | ||
| #define WorkerManager_h | ||
| #import "RCTBridgeModule.h" | ||
| @interface WorkerManager : NSObject <RCTBridgeModule> | ||
| @end | ||
| #endif /* WorkerManager_h */ |
| #import "WorkerManager.h" | ||
| #import "WorkerSelfManager.h" | ||
| #include <stdlib.h> | ||
| #import "RCTBridge.h" | ||
| #import "RCTBridge+Private.h" | ||
| #import "RCTEventDispatcher.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()); | ||
| NSString *bundleURL = [NSString stringWithFormat:@"http://localhost:8081/%@.bundle?platform=ios&dev=true", name]; | ||
| NSLog(@"starting Worker %@", bundleURL); | ||
| RCTBridge *workerBridge = [[RCTBridge alloc] initWithBundleURL:[NSURL URLWithString:bundleURL] | ||
| 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 |
| #ifndef WorkerSelfManager_h | ||
| #define WorkerSelfManager_h | ||
| #import "RCTBridgeModule.h" | ||
| @interface WorkerSelfManager : NSObject <RCTBridgeModule> | ||
| @property int workerId; | ||
| @property RCTBridge *parentBridge; | ||
| @end | ||
| #endif /* WorkerSelfManager_h */ |
| #import "WorkerSelfManager.h" | ||
| #include <stdlib.h> | ||
| #import "RCTBridge.h" | ||
| #import "RCTBridge+Private.h" | ||
| #import "RCTEventDispatcher.h" | ||
| @implementation WorkerSelfManager | ||
| RCT_EXPORT_MODULE(); | ||
| @synthesize bridge = _bridge; | ||
| @synthesize parentBridge = _parentBridge; | ||
| @synthesize workerId = _workerId; | ||
| RCT_EXPORT_METHOD(postMessage: (NSString *)message) | ||
| { | ||
| if (self.parentBridge == nil) { | ||
| NSLog(@"No parent bridge defined - abord sending worker message"); | ||
| return; | ||
| } | ||
| NSString *eventName = [NSString stringWithFormat:@"Worker%i", self.workerId]; | ||
| [self.parentBridge.eventDispatcher sendAppEventWithName:eventName | ||
| body:message]; | ||
| } | ||
| @end |
+0
-1
| module.exports = (WorkerSelf, EventEmitter) => { | ||
@@ -4,0 +3,0 @@ |
+5
-2
| module.exports = (WorkerModule, EventEmitter) => { | ||
@@ -7,3 +6,7 @@ | ||
| constructor(jsPath) { | ||
| this.id = WorkerModule.startWorker(jsPath) | ||
| if (!jsPath || !jsPath.endsWith('.js')) { | ||
| throw new Error("Invalid worker path. Only js files are supported"); | ||
| } | ||
| this.id = WorkerModule.startWorker(jsPath.replace(".js", "")) | ||
| .then(id => { | ||
@@ -10,0 +13,0 @@ EventEmitter.addListener(`Worker${id}`, (message) => { |
+1
-1
| { | ||
| "name": "react-native-workers", | ||
| "version": "0.1.1", | ||
| "version": "0.1.2", | ||
| "description": "react native web workers", | ||
@@ -5,0 +5,0 @@ "main": "main", |
+63
-1
@@ -1,1 +0,63 @@ | ||
| # react-native-workers | ||
| # react-native-workers | ||
| Spin worker threads and run CPU intensive tasks in the background. Bonus point on Android you can keep a worker alive even when a user quit the application :fireworks: | ||
| ## Features | ||
| - JS workers run on both iOS and Android | ||
| - access all native modules from the workers (network, geolocation, async storage ...) | ||
| - write your Android Services in JS :tada: | ||
| ## Warning | ||
| This plugin is still in beta and some features are missing. Current restrictions include: | ||
| - worker files are only loaded from http://localhost:8081. | ||
| - worker files are not yet packaged with your application. | ||
| - no HMR support. no hot-reload support. | ||
| ## Installation | ||
| ```bash | ||
| npm install react-native-workers --save | ||
| ``` | ||
| ## Setup | ||
| ### iOS | ||
| 1. Open your project in XCode, right click on Libraries and click Add Files to "Your Project Name". Look under node_modules/react-native-workers/ios and add `Workers.xcodeproj`. | ||
| 2. Add `libWorkers.a` to `Build Phases -> Link Binary With Libraries. | ||
| ## API | ||
| From your application: | ||
| ```js | ||
| import { Worker } from 'react-native-workers'; | ||
| /* start worker */ | ||
| const worker = new Worker("path/to/js/worker"); | ||
| /* post message to worker. String only ! */ | ||
| worker.postMessage("hello from application"); | ||
| /* get message from worker. String only ! */ | ||
| worker.onmessage = (message) => { | ||
| } | ||
| /* stop worker */ | ||
| worker.terminate(); | ||
| ``` | ||
| From your worker: | ||
| ```js | ||
| import { self } from 'react-native-workers'; | ||
| /* get message from application. String only ! */ | ||
| self.onmessage = (message) => { | ||
| } | ||
| /* post message to application. String only ! */ | ||
| self.postMessage("hello from worker"); | ||
| ``` | ||
| ##### Reload your application to restart your worker with the latest bundle version |
17984
393.79%13
62.5%49
6.52%64
6300%