-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathEzyEventEmitter.mm
45 lines (38 loc) · 1.08 KB
/
EzyEventEmitter.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
//
// EzyEventEmitter.m
// hello-swift
//
// Created by Dung Ta Van on 10/30/18.
// Copyright © 2018 Young Monkeys. All rights reserved.
//
#import "EzyEventEmitter.h"
@implementation EzyEventEmitter {
NSDictionary<NSDictionary*, EzyEventListenerBlock> *_eventListener;
}
+ (instancetype)getInstance {
static EzyEventEmitter *sInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sInstance = [[EzyEventEmitter alloc] init];
});
return sInstance;
}
- (instancetype)init
{
self = [super init];
if (self) {
_eventListener = [NSMutableDictionary dictionary];
}
return self;
}
- (void)sendEventWithName:(NSString *)name body:(NSDictionary *)body {
EzyEventListenerBlock listener = [_eventListener valueForKey:name];
if(listener)
listener(body);
else
[NSException raise:NSInvalidArgumentException format:@"has no lister with event name: %@", name];
}
- (void)setEventListener:(NSString *)name listener:(EzyEventListenerBlock)listener {
[_eventListener setValue:listener forKey:name];
}
@end