-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUtils.swift
More file actions
48 lines (40 loc) · 1.33 KB
/
Utils.swift
File metadata and controls
48 lines (40 loc) · 1.33 KB
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
import PromiseKit
/**
Commonly used functionality when promisifying a delegate pattern
*/
internal class PromiseProxy<T>: NSObject, CancellableTask {
var isCancelled = false
internal let (promise, seal) = Promise<T>.pending();
private var retainCycle: PromiseProxy?
override init() {
super.init()
// Create a retain cycle
self.retainCycle = self
// And ensure we break it when the promise is resolved
_ = promise.ensure { self.retainCycle = nil ; self.promise.setCancellableTask(nil) }
promise.setCancellableTask(self)
}
/// These functions ensure we only resolve the promise once
internal func fulfill(_ value: T) {
guard self.promise.isResolved == false else { return }
seal.fulfill(value)
}
internal func reject(_ error: Error) {
guard self.promise.isResolved == false else { return }
seal.reject(error)
}
/// Cancel helper
internal func cancel() {
isCancelled = true
self.reject(PMKError.cancelled)
}
}
/**
Different ways to scan.
*/
public enum ScanInterval {
// Return after our first item with an optional time limit
case returnFirst(timeout: TimeInterval?)
// Scan for this duration before returning all
case returnAll(interval: TimeInterval)
}