<commit_before>//
//  SwampSession.swift
//

import Foundation
import SwiftyJSON

// MARK: Call callbacks
public typealias CallCallback = (_ details: [String: Any], _ results: [Any]?, _ kwResults: [String: Any]?) -> Void
public typealias ErrorCallCallback = (_ details: [String: Any], _ error: String, _ args: [Any]?, _ kwargs: [String: Any]?) -> Void

// MARK: Callee callbacks
// For now callee is irrelevant
//public typealias RegisterCallback = (registration: Registration) -> Void
//public typealias ErrorRegisterCallback = (details: [String: AnyObject], error: String) -> Void
//public typealias SwampProc = (args: [AnyObject]?, kwargs: [String: AnyObject]?) -> AnyObject
//public typealias UnregisterCallback = () -> Void
//public typealias ErrorUnregsiterCallback = (details: [String: AnyObject], error: String) -> Void

// MARK: Subscribe callbacks
public typealias SubscribeCallback = (_ subscription: Subscription) -> Void
public typealias ErrorSubscribeCallback = (_ details: [String: Any], _ error: String) -> Void
public typealias EventCallback = (_ details: [String: Any], _ results: [Any]?, _ kwResults: [String: Any]?) -> Void
public typealias UnsubscribeCallback = () -> Void
public typealias ErrorUnsubscribeCallback = (_ details: [String: Any], _ error: String) -> Void

// MARK: Publish callbacks
public typealias PublishCallback = () -> Void
public typealias ErrorPublishCallback = (_ details: [String: Any], _ error: String) -> Void

// TODO: Expose only an interface (like Cancellable) to user
open class Subscription {
    fileprivate let session: SwampSession
    internal let subscription: Int
    internal let eventCallback: EventCallback
    fileprivate var isActive: Bool = true
    
    internal init(session: SwampSession, subscription: Int, onEvent: @escaping EventCallback) {
        self.session = session
        self.subscription = subscription
        self.eventCallback = onEvent
    }

    internal func invalidate() {
        self.isActive = false
    }

    open func cancel(_ onSuccess: @escaping UnsubscribeCallback, onError: @escaping ErrorUnsubscribeCallback) {
        if !self.isActive {
            onError([:], "Subscription already inactive.")
        }
        self.session.unsubscribe(self.subscription, onSuccess: onSuccess, onError: onError)
    }
}

// For now callee is irrelevant
//public class Registration {
//    private let session: SwampSession
//}

public protocol SwampSessionDelegate {
    func swampSessionHandleChallenge(_ authMethod: String, extra: [String: Any]) -> String
    func swampSessionConnected(_ session: SwampSession, sessionId: Int)
    func swampSessionEnded(_ error: Error?, _ reason: String?)
}<commit_msg>make subscription id public<commit_after>//
//  SwampSession.swift
//

import Foundation
import SwiftyJSON

// MARK: Call callbacks
public typealias CallCallback = (_ details: [String: Any], _ results: [Any]?, _ kwResults: [String: Any]?) -> Void
public typealias ErrorCallCallback = (_ details: [String: Any], _ error: String, _ args: [Any]?, _ kwargs: [String: Any]?) -> Void

// MARK: Callee callbacks
// For now callee is irrelevant
//public typealias RegisterCallback = (registration: Registration) -> Void
//public typealias ErrorRegisterCallback = (details: [String: AnyObject], error: String) -> Void
//public typealias SwampProc = (args: [AnyObject]?, kwargs: [String: AnyObject]?) -> AnyObject
//public typealias UnregisterCallback = () -> Void
//public typealias ErrorUnregsiterCallback = (details: [String: AnyObject], error: String) -> Void

// MARK: Subscribe callbacks
public typealias SubscribeCallback = (_ subscription: Subscription) -> Void
public typealias ErrorSubscribeCallback = (_ details: [String: Any], _ error: String) -> Void
public typealias EventCallback = (_ details: [String: Any], _ results: [Any]?, _ kwResults: [String: Any]?) -> Void
public typealias UnsubscribeCallback = () -> Void
public typealias ErrorUnsubscribeCallback = (_ details: [String: Any], _ error: String) -> Void

// MARK: Publish callbacks
public typealias PublishCallback = () -> Void
public typealias ErrorPublishCallback = (_ details: [String: Any], _ error: String) -> Void

// TODO: Expose only an interface (like Cancellable) to user
open class Subscription {
    fileprivate let session: SwampSession
    let subscription: Int
    internal let eventCallback: EventCallback
    fileprivate var isActive: Bool = true
    
    internal init(session: SwampSession, subscription: Int, onEvent: @escaping EventCallback) {
        self.session = session
        self.subscription = subscription
        self.eventCallback = onEvent
    }

    internal func invalidate() {
        self.isActive = false
    }

    open func cancel(_ onSuccess: @escaping UnsubscribeCallback, onError: @escaping ErrorUnsubscribeCallback) {
        if !self.isActive {
            onError([:], "Subscription already inactive.")
        }
        self.session.unsubscribe(self.subscription, onSuccess: onSuccess, onError: onError)
    }
}

// For now callee is irrelevant
//public class Registration {
//    private let session: SwampSession
//}

public protocol SwampSessionDelegate {
    func swampSessionHandleChallenge(_ authMethod: String, extra: [String: Any]) -> String
    func swampSessionConnected(_ session: SwampSession, sessionId: Int)
    func swampSessionEnded(_ error: Error?, _ reason: String?)
}
{'fraction_non_alphanumeric': 0.10878039725264525, 'fraction_numerical': 0.0, 'mean_word_length': 4.165867689357622, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1646902', 'n_tokens_mistral': 1479, 'n_tokens_neox': 1429, 'n_words': 486}