Skip to content

Commit

Permalink
patch (RobustWebSocket): allow resuming when seq is nil
Browse files Browse the repository at this point in the history
feat: add NullEncoder property wrapper
chore: clean up logging messages
  • Loading branch information
cryptoAlgorithm committed Oct 15, 2022
1 parent 43e697f commit 65c2202
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
27 changes: 27 additions & 0 deletions Sources/DiscordKitCommon/Utils/NullEncodable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// NullEncoder.swift
// From https://stackoverflow.com/a/62312021/
//
//
// Created by Vincent Kwok on 14/10/22.
//

import Foundation

/// Explicitly include a value even when nil when encoded
@propertyWrapper
public struct NullEncodable<T>: Encodable where T: Encodable {
public let wrappedValue: T?

public init(wrappedValue: T?) {
self.wrappedValue = wrappedValue
}

public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch wrappedValue {
case .some(let value): try container.encode(value)
case .none: try container.encodeNil()
}
}
}
2 changes: 1 addition & 1 deletion Sources/DiscordKitCore/Gateway/GatewayIdentify.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public extension RobustWebSocket {
///
/// - Returns: A `GatewayResume` struct, or nil if the Discord token is
/// not present in the keychain
internal func getResume(seq: Int, sessionID: String) -> GatewayResume? {
internal func getResume(seq: Int?, sessionID: String) -> GatewayResume? {
return GatewayResume(
token: token,
session_id: sessionID,
Expand Down
6 changes: 3 additions & 3 deletions Sources/DiscordKitCore/Gateway/RobustWebSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,12 @@ public class RobustWebSocket: NSObject, ObservableObject {
onHello()
// Start heartbeating and send identify
guard let d = decoded.d as? GatewayHello else { return }
log.debug("[HELLO] heartbeat interval: \(d.heartbeat_interval, privacy: .public)s")
log.debug("[HELLO] heartbeat interval: \(d.heartbeat_interval, privacy: .public)")
startHeartbeating(interval: Double(d.heartbeat_interval) / 1000.0)

// Check if we're attempting to and can resume
if canResume, let sessionID = sessionID, let seq = seq {
log.info("[RESUME] Resuming session \(sessionID), seq: \(seq)")
if canResume, let sessionID = sessionID {
log.info("[RESUME] Resuming session \(sessionID, privacy: .public), seq: \(String(describing: self.seq), privacy: .public)")
guard let resume = getResume(seq: seq, sessionID: sessionID)
else { return }
send(op: .resume, data: resume)
Expand Down
2 changes: 1 addition & 1 deletion Sources/DiscordKitCore/Objects/Gateway/DataStructs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,5 @@ struct GatewayIdentify: OutgoingGatewayData {
public struct GatewayResume: OutgoingGatewayData {
public let token: String
public let session_id: String
public let seq: Int // Last sequence number received
@NullEncodable public var seq: Int? // Last sequence number received
}

0 comments on commit 65c2202

Please sign in to comment.