aboutsummaryrefslogtreecommitdiff
path: root/VKPC/Queue.m
blob: d5cdef6833aaf6a934f1afee782c46cab61269b9 (plain)
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
46
47
48
49
50
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