2020-01-09 09:54:08 +08:00
|
|
|
/*
|
|
|
|
* Copyright [2019] [Doric.Pub]
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
//
|
|
|
|
// Created by pengfei.zhou on 2020/1/8.
|
|
|
|
//
|
|
|
|
|
2021-02-01 18:10:11 +08:00
|
|
|
#import <DoricCore/Doric.h>
|
2020-01-09 09:54:08 +08:00
|
|
|
#import "DoricNotificationPlugin.h"
|
|
|
|
|
2020-01-09 11:50:49 +08:00
|
|
|
@interface DoricNotificationPlugin ()
|
|
|
|
|
2021-07-09 15:58:25 +08:00
|
|
|
@property(nonatomic, strong) NSMutableDictionary<NSString *, id> *observers;
|
2021-08-24 10:32:32 +08:00
|
|
|
@property(nonatomic, strong) dispatch_queue_t syncQueue;
|
2021-07-09 15:58:25 +08:00
|
|
|
|
2020-01-09 11:50:49 +08:00
|
|
|
@end
|
2020-01-09 09:54:08 +08:00
|
|
|
|
|
|
|
@implementation DoricNotificationPlugin
|
|
|
|
|
2021-08-24 10:32:32 +08:00
|
|
|
- (NSMutableDictionary<NSString *, id> *)observers {
|
2020-01-09 11:50:49 +08:00
|
|
|
if (!_observers) {
|
2021-07-09 15:58:25 +08:00
|
|
|
_observers = [NSMutableDictionary new];
|
2020-01-09 11:50:49 +08:00
|
|
|
}
|
|
|
|
return _observers;
|
|
|
|
}
|
|
|
|
|
2021-08-24 10:32:32 +08:00
|
|
|
- (dispatch_queue_t)syncQueue {
|
|
|
|
if (!_syncQueue) {
|
|
|
|
_syncQueue = dispatch_queue_create("pub.doric.plugin.notification", DISPATCH_QUEUE_CONCURRENT);
|
2021-07-09 15:58:25 +08:00
|
|
|
}
|
2021-08-24 10:32:32 +08:00
|
|
|
return _syncQueue;
|
2021-07-09 15:58:25 +08:00
|
|
|
}
|
|
|
|
|
2020-01-09 09:54:08 +08:00
|
|
|
- (void)publish:(NSDictionary *)dic withPromise:(DoricPromise *)promise {
|
2021-02-01 18:10:11 +08:00
|
|
|
NSString *biz = [dic optString:@"biz"];
|
|
|
|
NSString *name = [dic optString:@"name"];
|
2020-01-09 11:50:49 +08:00
|
|
|
if (biz) {
|
|
|
|
name = [NSString stringWithFormat:@"__doric__%@#%@", biz, name];
|
|
|
|
}
|
2021-02-01 18:10:11 +08:00
|
|
|
NSString *data = [dic optString:@"data"];
|
2020-01-09 09:54:08 +08:00
|
|
|
NSDictionary *dataDic = nil;
|
|
|
|
if (data) {
|
|
|
|
NSData *jsonData = [data dataUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
NSError *err;
|
|
|
|
dataDic = [NSJSONSerialization JSONObjectWithData:jsonData
|
|
|
|
options:NSJSONReadingMutableContainers
|
|
|
|
error:&err];
|
|
|
|
}
|
2020-01-09 11:50:49 +08:00
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:name object:nil userInfo:dataDic];
|
|
|
|
[promise resolve:nil];
|
2020-01-09 09:54:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)subscribe:(NSDictionary *)dic withPromise:(DoricPromise *)promise {
|
2021-02-01 18:10:11 +08:00
|
|
|
NSString *biz = [dic optString:@"biz"];
|
|
|
|
NSString *name = [dic optString:@"name"];
|
2020-01-09 11:50:49 +08:00
|
|
|
if (biz) {
|
|
|
|
name = [NSString stringWithFormat:@"__doric__%@#%@", biz, name];
|
|
|
|
}
|
2021-02-01 18:10:11 +08:00
|
|
|
NSString *callbackId = [dic optString:@"callback"];
|
2020-01-09 11:50:49 +08:00
|
|
|
__weak typeof(self) _self = self;
|
|
|
|
id observer = [[NSNotificationCenter defaultCenter]
|
|
|
|
addObserverForName:name
|
|
|
|
object:nil
|
|
|
|
queue:[NSOperationQueue mainQueue]
|
|
|
|
usingBlock:^(NSNotification *note) {
|
|
|
|
__strong typeof(_self) self = _self;
|
|
|
|
DoricPromise *currentPromise = [[DoricPromise alloc] initWithContext:self.doricContext callbackId:callbackId];
|
|
|
|
[currentPromise resolve:note.userInfo];
|
|
|
|
}];
|
2021-08-24 10:32:32 +08:00
|
|
|
|
|
|
|
dispatch_barrier_async(self.syncQueue, ^{
|
|
|
|
self.observers[callbackId] = observer;
|
2021-07-09 15:58:25 +08:00
|
|
|
});
|
2020-01-09 11:50:49 +08:00
|
|
|
[promise resolve:callbackId];
|
|
|
|
}
|
2020-01-09 09:54:08 +08:00
|
|
|
|
2020-01-09 11:50:49 +08:00
|
|
|
- (void)unsubscribe:(NSString *)subscribeId withPromise:(DoricPromise *)promise {
|
2021-08-24 10:32:32 +08:00
|
|
|
dispatch_barrier_async(self.syncQueue, ^{
|
|
|
|
id observer = self.observers[subscribeId];
|
2020-01-09 11:50:49 +08:00
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:observer];
|
2021-08-19 11:43:49 +08:00
|
|
|
[self.observers removeObjectForKey:subscribeId];
|
|
|
|
[promise resolve:nil];
|
|
|
|
});
|
2020-01-09 09:54:08 +08:00
|
|
|
}
|
|
|
|
|
2020-01-09 11:50:49 +08:00
|
|
|
- (void)dealloc {
|
2021-08-19 11:43:49 +08:00
|
|
|
NSArray *values = [self.observers allValues];
|
2021-07-09 15:58:25 +08:00
|
|
|
[values enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {
|
2020-01-09 11:50:49 +08:00
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:obj];
|
|
|
|
}];
|
|
|
|
}
|
2020-01-09 09:54:08 +08:00
|
|
|
|
2020-01-09 11:50:49 +08:00
|
|
|
@end
|