Skip to content

Commit

Permalink
use thread safe set
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile committed May 8, 2024
1 parent 8419a84 commit 60b6cac
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion ios/Capacitor/Capacitor/WebViewAssetHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import MobileCoreServices
open class WebViewAssetHandler: NSObject, WKURLSchemeHandler {
private var router: Router
private var serverUrl: URL?
private var pendingTasks: Set<Int> = []
private var pendingTasks = ConcurrentTasks()

public init(router: Router) {
self.router = router
Expand Down Expand Up @@ -554,3 +554,26 @@ open class WebViewAssetHandler: NSObject, WKURLSchemeHandler {
"zip": "application/x-zip-compressed"
]
}

private class ConcurrentTasks {
private var tasks: Set<Int>
private let lock = NSLock()

init() {
tasks = []
}

func contains(_ value: Int) -> Bool {
lock.withLock { tasks.contains(value) }
}

@discardableResult
func remove(_ value: Int) -> Int? {
lock.withLock { tasks.remove(value) }
}

@discardableResult
func insert(_ value: Int) -> (inserted: Bool, memberAfterInsert: Int) {
lock.withLock { tasks.insert(value) }
}
}

0 comments on commit 60b6cac

Please sign in to comment.