-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathUnistylesModule.mm
77 lines (53 loc) · 1.69 KB
/
UnistylesModule.mm
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
#import "UnistylesModule.h"
#import "UnistylesRuntime.h"
#import <React/RCTBridge+Private.h>
#import <jsi/jsi.h>
using namespace facebook;
@implementation UnistylesModule
RCT_EXPORT_MODULE(Unistyles)
#pragma mark - Lifecycle
- (instancetype)init {
if ((self = [super init])) {
self.platform = [[Platform alloc] init];
}
return self;
}
+ (BOOL)requiresMainQueueSetup {
return YES;
}
#pragma mark - Event emitter
- (NSArray<NSString *> *)supportedEvents {
return @[@"__unistylesOnChange"];
}
- (void)startObserving {
self.hasListeners = YES;
}
- (void)stopObserving {
self.hasListeners = NO;
}
- (void)emitEvent:(NSString *)eventName withBody:(NSDictionary *)body {
if (self.hasListeners) {
[self sendEventWithName:@"__unistylesOnChange" body:body];
}
}
#pragma mark - Core
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(install) {
UnistylesModule *__weak weakSelf = self;
RCTBridge *bridge = self.bridge;
if (bridge == nullptr) {
return @false;
}
registerUnistylesHostObject(bridge, weakSelf);
NSLog(@"Installed Unistyles 🦄!");
return @true;
}
void registerUnistylesHostObject(RCTBridge* bridge, UnistylesModule* weakSelf) {
std::shared_ptr<react::CallInvoker> callInvoker = bridge.jsCallInvoker;
jsi::Runtime* runtime = reinterpret_cast<jsi::Runtime*>(bridge.runtime);
auto unistylesRuntime = std::make_shared<UnistylesRuntime>(*runtime, callInvoker);
[weakSelf.platform makeShared:unistylesRuntime.get()];
auto hostObject = jsi::Object::createFromHostObject(*runtime, unistylesRuntime);
runtime->global().setProperty(*runtime, "__UNISTYLES__", std::move(hostObject));
}
@end
#pragma mark - End