diff options
author | ch1p <me@ch1p.com> | 2015-08-14 01:04:22 +0300 |
---|---|---|
committer | ch1p <me@ch1p.com> | 2015-08-14 01:04:22 +0300 |
commit | 8c1a7423a0e526f2896d17be768abeccbeb77ad7 (patch) | |
tree | 67ad777e65ff6b0cca64a27ab5bb8455b575ffae /VKPC/Queue.m |
initial
Diffstat (limited to 'VKPC/Queue.m')
-rw-r--r-- | VKPC/Queue.m | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/VKPC/Queue.m b/VKPC/Queue.m new file mode 100644 index 0000000..d5cdef6 --- /dev/null +++ b/VKPC/Queue.m @@ -0,0 +1,51 @@ +// +// Queue.m +// VKPC +// +// Created by Eugene on 12/3/13. +// Copyright (c) 2013 Eugene Z. All rights reserved. +// + +#import "Queue.h" +#import "NSMutableArray+QueueAdditions.h" + +@implementation Queue + +- (id)init { + if (self = [super init]) { + tasks = [[NSMutableArray alloc] init]; + active = false; + } + return self; +} + +- (void)setHandler:(__strong id<QueueControllerProtocol>)val { + handler = val; +} + +- (void)addTask:(id)task { + [tasks enqueue:task]; + + if (!active) [self process]; +} + +- (void)process { + if (active || ![tasks count]) return; + + active = true; + id task = [tasks dequeue]; + [self passToHandler:task]; +} + +- (void)passToHandler:(__strong id)task { + [handler onQueueTask:task forQueue:self]; +} + +- (void)taskDone { + if (active) { + active = false; + [self process]; + } +} + +@end |