This repository was archived by the owner on Sep 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathWorkerManager.m
More file actions
93 lines (71 loc) · 2.46 KB
/
WorkerManager.m
File metadata and controls
93 lines (71 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#import "WorkerManager.h"
#import "WorkerSelfManager.h"
#include <stdlib.h>
#import "React/RCTBridge.h"
#import "RCTBridge+Private.h"
#import "RCTEventDispatcher.h"
#import "RCTBundleURLProvider.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