Files
peko-ios/YuMi/Modules/YMRoom/Features/Boom/Models/RoomBoomEventQueue.m
2025-03-25 15:22:01 +08:00

60 lines
1.4 KiB
Objective-C

#import "RoomBoomEventQueue.h"
#import "RoomBoomEvent.h"
@implementation RoomBoomEventQueue
- (instancetype)init {
self = [super init];
if (self) {
_events = [[NSMutableArray alloc] init];
_isProcessing = NO;
}
return self;
}
- (void)enqueueEvent:(id)event {
@synchronized (self.events) {
[self.events addObject:event];
// 如果当前没有在处理事件,则开始处理
if (!self.isProcessing) {
[self processNextEvent];
}
}
}
- (void)processNextEvent {
@synchronized (self.events) {
if (self.events.count == 0) {
self.isProcessing = NO;
return;
}
self.isProcessing = YES;
id event = self.events.firstObject;
[self.events removeObjectAtIndex:0];
if (self.processBlock) {
dispatch_async(dispatch_get_main_queue(), ^{
self.processBlock(event);
});
}
}
}
- (void)clear {
@synchronized (self.events) {
[self.events removeAllObjects];
self.isProcessing = NO;
}
}
- (NSString *)description {
return [NSString stringWithFormat:@"<%@: %p> events count: %lu, isProcessing: %@",
NSStringFromClass([self class]),
self,
(unsigned long)self.events.count,
self.isProcessing ? @"YES" : @"NO"];
}
@end