Skip to content

Commit

Permalink
'Cancel' for PromiseKit -- provides the ability to cancel promises an…
Browse files Browse the repository at this point in the history
…d promise chains
  • Loading branch information
dougzilla32 committed Oct 7, 2018
1 parent 5f9fda8 commit 3efa640
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
github "mxcl/PromiseKit" ~> 6.0
#github "mxcl/PromiseKit" ~> 6.0
github "dougzilla32/PromiseKit" "CoreCancel"
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "mxcl/PromiseKit" "6.4.1"
github "dougzilla32/PromiseKit" "087b3cf470890ff9ea841212e2f3e285fecf3988"
44 changes: 41 additions & 3 deletions Sources/HealthKit+Promise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ public extension HKStatisticsQuery {

public extension HKAnchoredObjectQuery {
static func promise(type: HKSampleType, predicate: NSPredicate? = nil, anchor: HKQueryAnchor? = nil, limit: Int = HKObjectQueryNoLimit, healthStore: HKHealthStore = .init()) -> Promise<([HKSample], [HKDeletedObject], HKQueryAnchor)> {
return Promise { seal in
let query = HKAnchoredObjectQuery(type: type, predicate: predicate, anchor: anchor, limit: limit) {
var query: HKAnchoredObjectQuery!
var reject: ((Error) -> Void)!

let promise = Promise<([HKSample], [HKDeletedObject], HKQueryAnchor)> { seal in
reject = seal.reject
query = HKAnchoredObjectQuery(type: type, predicate: predicate, anchor: anchor, limit: limit) {
if let a = $1, let b = $2, let c = $3 {
seal.fulfill((a, b, c))
} else if let e = $4 {
Expand All @@ -44,13 +48,16 @@ public extension HKAnchoredObjectQuery {
}
healthStore.execute(query)
}

promise.setCancellableTask(LongRunningQuery(healthStore: healthStore, query: query), reject: reject)
return promise
}

}

public extension HKStatisticsCollectionQuery {
func promise(healthStore: HKHealthStore = .init()) -> Promise<HKStatisticsCollection> {
return Promise { seal in
return Promise(cancellableTask: LongRunningQuery(healthStore: healthStore, query: self)) { seal in
initialResultsHandler = {
seal.resolve($1, $2)
}
Expand All @@ -69,3 +76,34 @@ public extension HKSampleQuery {
}
}
}

class LongRunningQuery: CancellableTask {
let healthStore: HKHealthStore
let query: HKQuery

init(healthStore: HKHealthStore, query: HKQuery) {
self.healthStore = healthStore
self.query = query
}

func cancel() {
healthStore.stop(query)
isCancelled = true
}

var isCancelled = false
}

//////////////////////////////////////////////////////////// Cancellable wrappers

public extension HKAnchoredObjectQuery {
static func cancellablePromise(type: HKSampleType, predicate: NSPredicate? = nil, anchor: HKQueryAnchor? = nil, limit: Int = HKObjectQueryNoLimit, healthStore: HKHealthStore = .init()) -> CancellablePromise<([HKSample], [HKDeletedObject], HKQueryAnchor)> {
return cancellable(promise(type: type, predicate: predicate, anchor: anchor, limit: limit, healthStore: healthStore))
}
}

public extension HKStatisticsCollectionQuery {
func cancellablePromise(healthStore: HKHealthStore = .init()) -> CancellablePromise<HKStatisticsCollection> {
return cancellable(promise(healthStore: healthStore))
}
}

0 comments on commit 3efa640

Please sign in to comment.