Skip to content

Commit

Permalink
Merge pull request #92 from WalletConnect/delayed-close
Browse files Browse the repository at this point in the history
Delayed close
  • Loading branch information
Dmitry Bespalov authored Dec 3, 2021
2 parents e20eaa0 + 080700e commit f316e6d
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
### Xcode ###
.swiftpm/
.build/
build/
*.pbxuser
Expand Down
7 changes: 7 additions & 0 deletions .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github "krzyzanowskim/CryptoSwift" ~> 1.4.0
github "daltoniam/Starscream" ~> 4.0.0
github "daltoniam/Starscream" ~> 4.0.4
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", .upToNextMinor(from: "1.4.0")),
.package(url: "https://github.com/daltoniam/Starscream.git", .upToNextMinor(from: "4.0.0"))
.package(url: "https://github.com/daltoniam/Starscream.git", .upToNextMinor(from: "4.0.4"))
],
targets: [
.target(
Expand Down
17 changes: 11 additions & 6 deletions Sources/Internal/BridgeTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class Bridge: Transport {
// TODO: if no connection found, then what?
func send(to url: WCURL, text: String) {
dispatchPrecondition(condition: .notOnQueue(syncQueue))
syncQueue.sync { [unowned self] in
syncQueue.sync { [weak self] in
guard let `self` = self else { return }
if let connection = self.findConnection(url: url) {
connection.send(text)
}
Expand All @@ -36,7 +37,8 @@ class Bridge: Transport {
onDisconnect: @escaping ((WCURL, Error?) -> Void),
onTextReceive: @escaping (String, WCURL) -> Void) {
dispatchPrecondition(condition: .notOnQueue(syncQueue))
syncQueue.sync { [unowned self] in
syncQueue.sync { [weak self] in
guard let `self` = self else { return }
var connection: WebSocketConnection
if let existingConnection = self.findConnection(url: url) {
connection = existingConnection
Expand All @@ -58,15 +60,17 @@ class Bridge: Transport {
func isConnected(by url: WCURL) -> Bool {
var connection: WebSocketConnection?
dispatchPrecondition(condition: .notOnQueue(syncQueue))
syncQueue.sync { [unowned self] in
syncQueue.sync { [weak self] in
guard let `self` = self else { return }
connection = self.findConnection(url: url)
}
return connection?.isOpen ?? false
}

func disconnect(from url: WCURL) {
dispatchPrecondition(condition: .notOnQueue(syncQueue))
syncQueue.sync { [unowned self] in
syncQueue.sync { [weak self] in
guard let `self` = self else { return }
if let connection = self.findConnection(url: url) {
connection.close()
}
Expand All @@ -75,7 +79,8 @@ class Bridge: Transport {

private func releaseConnection(by url: WCURL) {
dispatchPrecondition(condition: .notOnQueue(syncQueue))
syncQueue.sync { [unowned self] in
syncQueue.sync { [weak self] in
guard let `self` = self else { return }
if let connection = self.findConnection(url: url) {
self.connections.removeAll { $0 === connection }
}
Expand Down
21 changes: 13 additions & 8 deletions Sources/PublicInterface/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,17 @@ open class Server: WalletConnect {

func add(_ handler: RequestHandler) {
dispatchPrecondition(condition: .notOnQueue(queue))
queue.sync { [unowned self] in
queue.sync { [weak self] in
guard let `self` = self else { return }
guard self.handlers.first(where: { $0 === handler }) == nil else { return }
self.handlers.append(handler)
}
}

func remove(_ handler: RequestHandler) {
dispatchPrecondition(condition: .notOnQueue(queue))
queue.sync { [unowned self] in
queue.sync { [weak self] in
guard let `self` = self else { return }
if let index = self.handlers.firstIndex(where: { $0 === handler }) {
self.handlers.remove(at: index)
}
Expand All @@ -156,7 +158,8 @@ open class Server: WalletConnect {
func find(by request: Request) -> RequestHandler? {
var result: RequestHandler?
dispatchPrecondition(condition: .notOnQueue(queue))
queue.sync { [unowned self] in
queue.sync { [weak self] in
guard let `self` = self else { return }
result = self.handlers.first { $0.canHandle(request: request) }
}
return result
Expand All @@ -178,6 +181,10 @@ extension Server: HandshakeHandlerDelegate {
self.communicator.addOrUpdateSession(updatedSession)
self.communicator.subscribe(on: walletInfo.peerId, url: updatedSession.url)
self.delegate?.server(self, didConnect: updatedSession)
} else {
self.communicator.addOrUpdatePendingDisconnectSession(session)
self.communicator.disconnect(from: session.url)
self.delegate?.server(self, didDisconnect: session)
}
}
}
Expand All @@ -187,11 +194,9 @@ extension Server: UpdateSessionHandlerDelegate {
func handler(_ handler: UpdateSessionHandler, didUpdateSessionByURL url: WCURL, sessionInfo: SessionInfo) {
guard let session = communicator.session(by: url) else { return }
if !sessionInfo.approved {
do {
try disconnect(from: session)
} catch { // session already disconnected
delegate?.server(self, didDisconnect: session)
}
self.communicator.addOrUpdatePendingDisconnectSession(session)
self.communicator.disconnect(from: session.url)
self.delegate?.server(self, didDisconnect: session)
} else {
// we do not add sessions without walletInfo
let walletInfo = session.walletInfo!
Expand Down
4 changes: 2 additions & 2 deletions WalletConnectSwift.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = "WalletConnectSwift"
spec.version = "1.6.1"
spec.version = "1.6.2"
spec.summary = "A delightful way to integrate WalletConnect into your app."
spec.description = <<-DESC
WalletConnect protocol implementation for enabling communication between dapps and
Expand All @@ -17,5 +17,5 @@ Pod::Spec.new do |spec|
spec.source_files = "Sources/**/*.swift"
spec.requires_arc = true
spec.dependency "CryptoSwift", "~> 1.4"
spec.dependency "Starscream", "~> 4.0"
spec.dependency "Starscream", "~> 4.0.4"
end
4 changes: 2 additions & 2 deletions WalletConnectSwift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 1030;
LastUpgradeCheck = 1250;
LastUpgradeCheck = 1310;
ORGANIZATIONNAME = "Gnosis Ltd.";
TargetAttributes = {
0A92784F230BFB2600FDCC0D = {
Expand Down Expand Up @@ -625,7 +625,7 @@
repositoryURL = "https://github.com/daltoniam/Starscream.git";
requirement = {
kind = upToNextMinorVersion;
minimumVersion = 4.0.0;
minimumVersion = 4.0.4;
};
};
/* End XCRemoteSwiftPackageReference section */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"repositoryURL": "https://github.com/krzyzanowskim/CryptoSwift.git",
"state": {
"branch": null,
"revision": "4e31051c63cc0ddf10a25cf5318856c510cf77f4",
"version": "1.4.0"
"revision": "4b0565384d3c4c588af09e660535b2c7c9bf5b39",
"version": "1.4.2"
}
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1250"
LastUpgradeVersion = "1310"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 1 addition & 1 deletion WalletConnectSwift/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.6.1</string>
<string>1.6.2</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
</dict>
Expand Down
2 changes: 1 addition & 1 deletion WalletConnectSwiftTests/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>1.6.1</string>
<string>1.6.2</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
Expand Down

0 comments on commit f316e6d

Please sign in to comment.