iOS: add thread-safe handling for JSDispatcher
This commit is contained in:
parent
542f73e0fd
commit
d92ce26248
@ -22,10 +22,19 @@
|
|||||||
@interface DoricJSDispatcher ()
|
@interface DoricJSDispatcher ()
|
||||||
@property(nonatomic, strong) NSMutableArray <DoricAsyncResult *(^)(void)> *blocks;
|
@property(nonatomic, strong) NSMutableArray <DoricAsyncResult *(^)(void)> *blocks;
|
||||||
@property(nonatomic, assign) BOOL consuming;
|
@property(nonatomic, assign) BOOL consuming;
|
||||||
|
@property(nonatomic, strong) dispatch_queue_t syncQueue;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation DoricJSDispatcher
|
@implementation DoricJSDispatcher
|
||||||
|
- (instancetype)init {
|
||||||
|
if (self = [super init]) {
|
||||||
|
_syncQueue = dispatch_queue_create("DoricJSDispatcher", DISPATCH_QUEUE_CONCURRENT);
|
||||||
|
}
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
- (void)dispatch:(DoricAsyncResult *(^)(void))block {
|
- (void)dispatch:(DoricAsyncResult *(^)(void))block {
|
||||||
|
dispatch_barrier_async(self.syncQueue, ^{
|
||||||
if (!self.blocks) {
|
if (!self.blocks) {
|
||||||
self.blocks = [@[block] mutableCopy];
|
self.blocks = [@[block] mutableCopy];
|
||||||
} else {
|
} else {
|
||||||
@ -37,9 +46,12 @@ - (void)dispatch:(DoricAsyncResult *(^)(void))block {
|
|||||||
if (!self.consuming) {
|
if (!self.consuming) {
|
||||||
[self consume];
|
[self consume];
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)consume {
|
- (void)consume {
|
||||||
|
dispatch_barrier_async(self.syncQueue, ^{
|
||||||
DoricAsyncResult *(^block )(void) = self.blocks.lastObject;
|
DoricAsyncResult *(^block )(void) = self.blocks.lastObject;
|
||||||
if (block) {
|
if (block) {
|
||||||
self.consuming = YES;
|
self.consuming = YES;
|
||||||
@ -55,9 +67,12 @@ - (void)consume {
|
|||||||
} else {
|
} else {
|
||||||
self.consuming = NO;
|
self.consuming = NO;
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)clear {
|
- (void)clear {
|
||||||
|
dispatch_barrier_async(self.syncQueue, ^{
|
||||||
[self.blocks removeAllObjects];
|
[self.blocks removeAllObjects];
|
||||||
|
});
|
||||||
}
|
}
|
||||||
@end
|
@end
|
||||||
|
Reference in New Issue
Block a user