forked from zbrateam/Zebra
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
35 changed files
with
930 additions
and
702 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule Plains
updated
from 6ca67a to 32a469
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// JobQueue.swift | ||
// Zebra | ||
// | ||
// Created by Adam Demasi on 19/6/2022. | ||
// Copyright © 2022 Zebra Team. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
class JobQueue<Job: Hashable> { | ||
|
||
typealias QueueProcessor = (Job) -> Void | ||
|
||
let queue: DispatchQueue | ||
let taskLimit: Int | ||
let queueProcessor: QueueProcessor | ||
|
||
private(set) var pendingJobs = [Job]() | ||
private(set) var runningJobs = [Job]() | ||
|
||
init(queue: DispatchQueue, taskLimit: Int, queueProcessor: @escaping QueueProcessor) { | ||
self.queue = queue | ||
self.taskLimit = taskLimit | ||
self.queueProcessor = queueProcessor | ||
} | ||
|
||
private func tick() { | ||
queue.async { | ||
if !self.pendingJobs.isEmpty && self.runningJobs.count < self.taskLimit { | ||
let job = self.pendingJobs.removeFirst() | ||
self.runningJobs.append(job) | ||
self.queueProcessor(job) | ||
self.tick() | ||
} | ||
} | ||
} | ||
|
||
func enqueue(job: Job) { | ||
pendingJobs.append(job) | ||
tick() | ||
} | ||
|
||
func complete(job: Job) { | ||
if let index = runningJobs.firstIndex(of: job) { | ||
runningJobs.remove(at: index) | ||
} | ||
tick() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.