Skip to content

Commit

Permalink
fix(ios): when canceling upload remove it, so it won't be resumed
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanno Gödecke committed Mar 22, 2021
1 parent 68f2221 commit 6a71206
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions ios/BetterTusClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let eventOnFailure = "onFailure"

@objc(BetterTusClient)
public class BetterTusClient: RCTEventEmitter, TUSDelegate {

@objc(initialize:resolver:rejecter:)
func initialize(endpoint: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
let config = TUSConfig(withUploadURLString: endpoint, andSessionConfig: URLSessionConfiguration.default)
Expand All @@ -21,7 +21,7 @@ public class BetterTusClient: RCTEventEmitter, TUSDelegate {
upload.metadata = metadata
TUSClient.shared.createOrResume(forUpload: upload, withCustomHeaders: headers)
}

@objc(getStateForUploadById:resolver:rejecter:)
func getStateForUploadById(withId: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
let upload = TUSClient.shared.currentUploads?.first(where: { (_upload) -> Bool in
Expand All @@ -32,7 +32,7 @@ public class BetterTusClient: RCTEventEmitter, TUSDelegate {
resolve(nil)
return
}

switch status {
case .paused, .canceled:
resolve("CANCELLED")
Expand All @@ -50,20 +50,22 @@ public class BetterTusClient: RCTEventEmitter, TUSDelegate {
resolve("ENQUEUED")
}
}

@objc(resumeAll:rejecter:)
func resumeAll(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
TUSClient.shared.resumeAll()
resolve(nil)
}

@objc(cancel:)
func cancel(uploadId: String) {
let upload = TUSClient.shared.currentUploads?.first(where: { (_upload) -> Bool in
return _upload.id == uploadId
})
if (upload != nil) {
TUSClient.shared.cancel(forUpload: upload!)
let index = TUSClient.shared.currentUploads?.firstIndex(of: upload!)
TUSClient.shared.currentUploads?.remove(at: index!);
}
}

Expand Down

0 comments on commit 6a71206

Please sign in to comment.