Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose methods to objective c #135

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions EventSource/EventSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ public protocol EventSourceProtocol {
/// enought for you to take a decition if you should reconnect or not.
/// - Parameter onOpenCallback: callback
func onComplete(_ onComplete: @escaping ((Int?, Bool?, NSError?) -> Void))

/// Callback called once EventSource has disconnected from server. This can happen for multiple reasons.
/// The server could have requested the disconnection or maybe a network layer error, wrong URL or any other
/// error. The callback receives as parameters the status code of the disconnection, if we should reconnect or not
/// following event source rules and finally the network layer error if any. All this information is more than
/// enought for you to take a decition if you should reconnect or not.
/// - Parameter onOpenCallback: callback
func onCompleteBridged(_ onComplete: @escaping ((NSNumber?, NSNumber?, NSError?) -> Void))

/// This callback is called everytime an event with name "message" or no name is received.
func onMessage(_ onMessageCallback: @escaping ((_ id: String?, _ event: String?, _ data: String?) -> Void))
Expand Down Expand Up @@ -92,6 +100,7 @@ open class EventSource: NSObject, EventSourceProtocol, URLSessionDataDelegate {
private var mainQueue = DispatchQueue.main
private var urlSession: URLSession?

@objc
public init(
url: URL,
headers: [String: String] = [:]
Expand All @@ -106,6 +115,7 @@ open class EventSource: NSObject, EventSourceProtocol, URLSessionDataDelegate {
super.init()
}

@objc
public func connect(lastEventId: String? = nil) {
eventStreamParser = EventStreamParser()
readyState = .connecting
Expand All @@ -115,32 +125,50 @@ open class EventSource: NSObject, EventSourceProtocol, URLSessionDataDelegate {
urlSession?.dataTask(with: url).resume()
}

@objc
public func disconnect() {
readyState = .closed
urlSession?.invalidateAndCancel()
}

@objc
public func onOpen(_ onOpenCallback: @escaping (() -> Void)) {
self.onOpenCallback = onOpenCallback
}

public func onComplete(_ onComplete: @escaping ((Int?, Bool?, NSError?) -> Void)) {
self.onComplete = onComplete
}

@objc
public func onCompleteBridged(_ onComplete: @escaping((NSNumber?, NSNumber?, NSError?) -> Void)) {

self.onComplete = { (statusCode: Int?, shouldReconnect: Bool?, error: NSError?) -> Void in
let status: NSNumber? = statusCode != nil ? NSNumber(value: statusCode!) : nil

let reconnect: NSNumber? = shouldReconnect != nil ? NSNumber(booleanLiteral: shouldReconnect!) : nil

return onComplete(status, reconnect, error)
}
}

@objc
public func onMessage(_ onMessageCallback: @escaping ((_ id: String?, _ event: String?, _ data: String?) -> Void)) {
self.onMessageCallback = onMessageCallback
}

@objc
public func addEventListener(_ event: String,
handler: @escaping ((_ id: String?, _ event: String?, _ data: String?) -> Void)) {
eventListeners[event] = handler
}

@objc
public func removeEventListener(_ event: String) {
eventListeners.removeValue(forKey: event)
}

@objc
public func events() -> [String] {
return Array(eventListeners.keys)
}
Expand Down