-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathRNNodeJsMobile.m
More file actions
150 lines (128 loc) · 4.63 KB
/
RNNodeJsMobile.m
File metadata and controls
150 lines (128 loc) · 4.63 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#import "RNNodeJsMobile.h"
#import "NodeRunner.hpp"
#import <React/RCTEventDispatcher.h>
@implementation RNNodeJsMobile
NSString* const BUILTIN_MODULES_RESOURCE_PATH = @"builtin_modules";
NSString* const NODEJS_PROJECT_RESOURCE_PATH = @"nodejs-project";
NSString* nodePath;
@synthesize bridge = _bridge;
- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}
+ (BOOL)requiresMainQueueSetup
{
return NO;
}
- (id)init
{
self = [super init];
if (self != nil)
{
[[NodeRunner sharedInstance] setCurrentRNNodeJsMobile:self];
}
NSString* builtinModulesPath = [[NSBundle mainBundle] pathForResource:BUILTIN_MODULES_RESOURCE_PATH ofType:@""];
nodePath = [[NSBundle mainBundle] pathForResource:NODEJS_PROJECT_RESOURCE_PATH ofType:@""];
nodePath = [nodePath stringByAppendingString:@":"];
nodePath = [nodePath stringByAppendingString:builtinModulesPath];
return self;
}
RCT_EXPORT_MODULE()
RCT_EXPORT_METHOD(sendMessage:(NSString *)script)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
[[NodeRunner sharedInstance] sendMessageToNode:script];
});
}
-(void)callStartNodeWithScript:(NSString *)script
{
NSArray* nodeArguments = nil;
NSString* dlopenoverridePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@/override-dlopen-paths-preload.js", NODEJS_PROJECT_RESOURCE_PATH] ofType:@""];
// Check if the file to override dlopen lookup exists, for loading native modules from the Frameworks.
if(!dlopenoverridePath)
{
nodeArguments = [NSArray arrayWithObjects:
@"node",
@"-e",
script,
nil
];
} else {
nodeArguments = [NSArray arrayWithObjects:
@"node",
@"-r",
dlopenoverridePath,
@"-e",
script,
nil
];
}
[[NodeRunner sharedInstance] startEngineWithArguments:nodeArguments:nodePath];
}
-(void)callStartNodeProject:(NSString *)mainFileName
{
NSString* srcPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@/%@", NODEJS_PROJECT_RESOURCE_PATH, mainFileName] ofType:@""];
NSArray* nodeArguments = nil;
NSString* dlopenoverridePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@/override-dlopen-paths-preload.js", NODEJS_PROJECT_RESOURCE_PATH] ofType:@""];
// Check if the file to override dlopen lookup exists, for loading native modules from the Frameworks.
if(!dlopenoverridePath)
{
nodeArguments = [NSArray arrayWithObjects:
@"node",
srcPath,
nil
];
} else {
nodeArguments = [NSArray arrayWithObjects:
@"node",
@"-r",
dlopenoverridePath,
srcPath,
nil
];
}
[[NodeRunner sharedInstance] startEngineWithArguments:nodeArguments:nodePath];
}
RCT_EXPORT_METHOD(startNodeWithScript:(NSString *)script options:(NSDictionary *)options)
{
if(![NodeRunner sharedInstance].startedNodeAlready)
{
[NodeRunner sharedInstance].startedNodeAlready=true;
NSThread* nodejsThread = nil;
nodejsThread = [[NSThread alloc]
initWithTarget:self
selector:@selector(callStartNodeWithScript:)
object:script
];
// Set 1MB of stack space for the Node.js thread,
// the same as the iOS application's main thread.
[nodejsThread setStackSize:1024*1024];
[nodejsThread start];
}
}
RCT_EXPORT_METHOD(startNodeProject:(NSString *)mainFileName options:(NSDictionary *)options)
{
if(![NodeRunner sharedInstance].startedNodeAlready)
{
[NodeRunner sharedInstance].startedNodeAlready=true;
NSThread* nodejsThread = nil;
nodejsThread = [[NSThread alloc]
initWithTarget:self
selector:@selector(callStartNodeProject:)
object:mainFileName
];
// Set 1MB of stack space for the Node.js thread,
// the same as the iOS application's main thread.
[nodejsThread setStackSize:1024*1024];
[nodejsThread start];
}
}
-(void) sendMessageBackToReact:(NSString*)message
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
[self.bridge.eventDispatcher sendAppEventWithName:@"nodejs-mobile-react-native-message"
body:@{@"message": message}
];
});
}
@end