Skip to content

Commit a676ecf

Browse files
feat: run app from NativeScript initializer instead of static method (#137)
Co-authored-by: Dermendzhiev, Teodor (external - Project) <[email protected]>
1 parent 7c881f5 commit a676ecf

File tree

3 files changed

+48
-42
lines changed

3 files changed

+48
-42
lines changed

AppWithModules/Source Files/main.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ int main(int argc, char *argv[]) {
2323
config.ArgumentsCount = argc;
2424
config.Arguments = argv;
2525

26-
[NativeScript start:config];
26+
[[NativeScript alloc] initWithConfig: config];
2727

2828
return 0;
2929
}

NativeScript/NativeScript.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
@interface NativeScript : NSObject
1616

17-
+ (void)start:(Config*)config;
18-
+ (bool)liveSync;
17+
- (instancetype)initWithConfig:(Config*)config;
18+
- (bool)liveSync;
1919

2020
@end

NativeScript/NativeScript.mm

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,48 +20,54 @@ @implementation Config
2020

2121
@implementation NativeScript
2222

23-
static std::shared_ptr<Runtime> runtime_;
24-
25-
+ (void)start:(Config*)config {
26-
RuntimeConfig.BaseDir = [config.BaseDir UTF8String];
27-
if (config.ApplicationPath != nil) {
28-
RuntimeConfig.ApplicationPath = [[config.BaseDir stringByAppendingPathComponent:config.ApplicationPath] UTF8String];
29-
} else {
30-
RuntimeConfig.ApplicationPath = [[config.BaseDir stringByAppendingPathComponent:@"app"] UTF8String];
23+
std::unique_ptr<Runtime> runtime_;
24+
25+
- (instancetype)initWithConfig:(Config*)config {
26+
27+
if (self = [super init]) {
28+
RuntimeConfig.BaseDir = [config.BaseDir UTF8String];
29+
if (config.ApplicationPath != nil) {
30+
RuntimeConfig.ApplicationPath = [[config.BaseDir stringByAppendingPathComponent:config.ApplicationPath] UTF8String];
31+
} else {
32+
RuntimeConfig.ApplicationPath = [[config.BaseDir stringByAppendingPathComponent:@"app"] UTF8String];
33+
}
34+
RuntimeConfig.MetadataPtr = [config MetadataPtr];
35+
RuntimeConfig.IsDebug = [config IsDebug];
36+
RuntimeConfig.LogToSystemConsole = [config LogToSystemConsole];
37+
38+
Runtime::Initialize();
39+
runtime_ = std::make_unique<Runtime>();
40+
41+
std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
42+
Isolate* isolate = runtime_->CreateIsolate();
43+
runtime_->Init(isolate);
44+
std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now();
45+
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();
46+
printf("Runtime initialization took %llims\n", duration);
47+
48+
if (config.IsDebug) {
49+
Isolate::Scope isolate_scope(isolate);
50+
HandleScope handle_scope(isolate);
51+
v8_inspector::JsV8InspectorClient* inspectorClient = new v8_inspector::JsV8InspectorClient(runtime_.get());
52+
inspectorClient->init();
53+
inspectorClient->registerModules();
54+
inspectorClient->connect([config ArgumentsCount], [config Arguments]);
55+
}
56+
57+
runtime_->RunMainScript();
58+
59+
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true);
60+
61+
tns::Tasks::Drain();
62+
63+
runtime_.reset();
3164
}
32-
RuntimeConfig.MetadataPtr = [config MetadataPtr];
33-
RuntimeConfig.IsDebug = [config IsDebug];
34-
RuntimeConfig.LogToSystemConsole = [config LogToSystemConsole];
35-
36-
Runtime::Initialize();
37-
runtime_ = std::make_shared<Runtime>();
38-
39-
std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
40-
Isolate* isolate = runtime_->CreateIsolate();
41-
runtime_->Init(isolate);
42-
std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now();
43-
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();
44-
printf("Runtime initialization took %llims\n", duration);
45-
46-
if (config.IsDebug) {
47-
Isolate::Scope isolate_scope(isolate);
48-
HandleScope handle_scope(isolate);
49-
v8_inspector::JsV8InspectorClient* inspectorClient = new v8_inspector::JsV8InspectorClient(runtime_.get());
50-
inspectorClient->init();
51-
inspectorClient->registerModules();
52-
inspectorClient->connect([config ArgumentsCount], [config Arguments]);
53-
}
54-
55-
runtime_->RunMainScript();
56-
57-
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true);
58-
59-
tns::Tasks::Drain();
60-
61-
runtime_.reset();
65+
66+
return self;
67+
6268
}
6369

64-
+ (bool)liveSync {
70+
- (bool)liveSync {
6571
if (runtime_ == nullptr) {
6672
return false;
6773
}

0 commit comments

Comments
 (0)