Typesafe Event.type
This commit is contained in:
parent
3a5d8ea4f0
commit
374fa73dbb
|
@ -129,13 +129,13 @@ final class LiveViewController: UIViewController {
|
|||
if publish.isSelected {
|
||||
UIApplication.shared.isIdleTimerDisabled = false
|
||||
rtmpConnection.close()
|
||||
rtmpConnection.removeEventListener(Event.RTMP_STATUS, selector: #selector(rtmpStatusHandler), observer: self)
|
||||
rtmpConnection.removeEventListener(Event.IO_ERROR, selector: #selector(rtmpErrorHandler), observer: self)
|
||||
rtmpConnection.removeEventListener(.rtmpStatus, selector: #selector(rtmpStatusHandler), observer: self)
|
||||
rtmpConnection.removeEventListener(.ioError, selector: #selector(rtmpErrorHandler), observer: self)
|
||||
publish.setTitle("●", for: [])
|
||||
} else {
|
||||
UIApplication.shared.isIdleTimerDisabled = true
|
||||
rtmpConnection.addEventListener(Event.RTMP_STATUS, selector: #selector(rtmpStatusHandler), observer: self)
|
||||
rtmpConnection.addEventListener(Event.IO_ERROR, selector: #selector(rtmpErrorHandler), observer: self)
|
||||
rtmpConnection.addEventListener(.rtmpStatus, selector: #selector(rtmpStatusHandler), observer: self)
|
||||
rtmpConnection.addEventListener(.ioError, selector: #selector(rtmpErrorHandler), observer: self)
|
||||
rtmpConnection.connect(Preference.defaultInstance.uri!)
|
||||
publish.setTitle("■", for: [])
|
||||
}
|
||||
|
@ -168,7 +168,7 @@ final class LiveViewController: UIViewController {
|
|||
@objc
|
||||
private func rtmpErrorHandler(_ notification: Notification) {
|
||||
let e = Event.from(notification)
|
||||
print("rtmpErrorHandler: \(e.description)")
|
||||
print("rtmpErrorHandler: \(e)")
|
||||
|
||||
DispatchQueue.main.async {
|
||||
self.rtmpConnection.connect(Preference.defaultInstance.uri!)
|
||||
|
|
|
@ -14,11 +14,11 @@ public class RTMPBroadcaster: RTMPConnection {
|
|||
|
||||
override public init() {
|
||||
super.init()
|
||||
addEventListener(Event.RTMP_STATUS, selector: #selector(rtmpStatusEvent), observer: self)
|
||||
addEventListener(.rtmpStatus, selector: #selector(rtmpStatusEvent), observer: self)
|
||||
}
|
||||
|
||||
deinit {
|
||||
removeEventListener(Event.RTMP_STATUS, selector: #selector(rtmpStatusEvent), observer: self)
|
||||
removeEventListener(.rtmpStatus, selector: #selector(rtmpStatusEvent), observer: self)
|
||||
}
|
||||
|
||||
override public func connect(_ command: String, arguments: Any?...) {
|
||||
|
|
|
@ -64,7 +64,7 @@ final class MainViewController: NSViewController {
|
|||
segmentedControl.isEnabled = false
|
||||
switch segmentedControl.selectedSegment {
|
||||
case 0:
|
||||
rtmpConnection.addEventListener(Event.RTMP_STATUS, selector: #selector(rtmpStatusHandler), observer: self)
|
||||
rtmpConnection.addEventListener(.rtmpStatus, selector: #selector(rtmpStatusHandler), observer: self)
|
||||
rtmpConnection.connect(urlField.stringValue)
|
||||
case 1:
|
||||
httpStream.publish("hello")
|
||||
|
@ -80,7 +80,7 @@ final class MainViewController: NSViewController {
|
|||
segmentedControl.isEnabled = true
|
||||
switch segmentedControl.selectedSegment {
|
||||
case 0:
|
||||
rtmpConnection.removeEventListener(Event.RTMP_STATUS, selector: #selector(rtmpStatusHandler), observer: self)
|
||||
rtmpConnection.removeEventListener(.rtmpStatus, selector: #selector(rtmpStatusHandler), observer: self)
|
||||
rtmpConnection.close()
|
||||
case 1:
|
||||
httpService.removeHTTPStream(httpStream)
|
||||
|
|
|
@ -10,7 +10,7 @@ final class ViewController: UIViewController {
|
|||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
rtmpStream = RTMPStream(connection: rtmpConnection)
|
||||
rtmpConnection.addEventListener(Event.RTMP_STATUS, selector: #selector(rtmpStatusHandler), observer: self)
|
||||
rtmpConnection.addEventListener(.rtmpStatus, selector: #selector(rtmpStatusHandler), observer: self)
|
||||
rtmpConnection.connect(Preference.defaultInstance.uri!)
|
||||
}
|
||||
|
||||
|
|
|
@ -245,13 +245,13 @@ open class RTMPConnection: EventDispatcher {
|
|||
|
||||
override public init() {
|
||||
super.init()
|
||||
addEventListener(Event.RTMP_STATUS, selector: #selector(on(status:)))
|
||||
addEventListener(.rtmpStatus, selector: #selector(on(status:)))
|
||||
}
|
||||
|
||||
deinit {
|
||||
timer = nil
|
||||
streams.removeAll()
|
||||
removeEventListener(Event.RTMP_STATUS, selector: #selector(on(status:)))
|
||||
removeEventListener(.rtmpStatus, selector: #selector(on(status:)))
|
||||
}
|
||||
|
||||
open func call(_ commandName: String, responder: Responder?, arguments: Any?...) {
|
||||
|
|
|
@ -337,7 +337,7 @@ final class RTMPCommandMessage: RTMPMessage {
|
|||
case "close":
|
||||
connection.close(isDisconnected: true)
|
||||
default:
|
||||
connection.dispatch(Event.RTMP_STATUS, bubbles: false, data: arguments.first)
|
||||
connection.dispatch(.rtmpStatus, bubbles: false, data: arguments.first)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
@ -138,7 +138,7 @@ open class RTMPSharedObject: EventDispatcher {
|
|||
close()
|
||||
}
|
||||
self.rtmpConnection = rtmpConnection
|
||||
rtmpConnection.addEventListener(Event.RTMP_STATUS, selector: #selector(rtmpStatusHandler), observer: self)
|
||||
rtmpConnection.addEventListener(.rtmpStatus, selector: #selector(rtmpStatusHandler), observer: self)
|
||||
if rtmpConnection.connected {
|
||||
timestamp = rtmpConnection.socket.timestamp
|
||||
rtmpConnection.socket.doOutput(chunk: createChunk([RTMPSharedObjectEvent(type: .use)]), locked: nil)
|
||||
|
@ -152,7 +152,7 @@ open class RTMPSharedObject: EventDispatcher {
|
|||
|
||||
open func close() {
|
||||
data.removeAll(keepingCapacity: false)
|
||||
rtmpConnection?.removeEventListener(Event.RTMP_STATUS, selector: #selector(rtmpStatusHandler), observer: self)
|
||||
rtmpConnection?.removeEventListener(.rtmpStatus, selector: #selector(rtmpStatusHandler), observer: self)
|
||||
rtmpConnection?.socket.doOutput(chunk: createChunk([RTMPSharedObjectEvent(type: .release)]), locked: nil)
|
||||
rtmpConnection = nil
|
||||
}
|
||||
|
@ -189,7 +189,7 @@ open class RTMPSharedObject: EventDispatcher {
|
|||
}
|
||||
changeList.append(change)
|
||||
}
|
||||
dispatch(Event.SYNC, bubbles: false, data: changeList)
|
||||
dispatch(.sync, bubbles: false, data: changeList)
|
||||
}
|
||||
|
||||
func createChunk(_ events: [RTMPSharedObjectEvent]) -> RTMPChunk {
|
||||
|
|
|
@ -92,7 +92,7 @@ final class RTMPSocket: NetSocket, RTMPSocketCompatible {
|
|||
if isDisconnected {
|
||||
let data: ASObject = (readyState == .handshakeDone) ?
|
||||
RTMPConnection.Code.connectClosed.data("") : RTMPConnection.Code.connectFailed.data("")
|
||||
events.append(Event(type: Event.RTMP_STATUS, bubbles: false, data: data))
|
||||
events.append(Event(type: .rtmpStatus, bubbles: false, data: data))
|
||||
}
|
||||
readyState = .closing
|
||||
super.deinitConnection(isDisconnected: isDisconnected)
|
||||
|
@ -100,7 +100,7 @@ final class RTMPSocket: NetSocket, RTMPSocketCompatible {
|
|||
|
||||
override func didTimeout() {
|
||||
deinitConnection(isDisconnected: false)
|
||||
delegate?.dispatch(Event.IO_ERROR, bubbles: false, data: nil)
|
||||
delegate?.dispatch(.ioError, bubbles: false, data: nil)
|
||||
logger.warn("connection timedout")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -39,7 +39,7 @@ extension RTMPSocketCompatible {
|
|||
|
||||
func didTimeout() {
|
||||
deinitConnection(isDisconnected: false)
|
||||
delegate?.dispatch(Event.IO_ERROR, bubbles: false, data: nil)
|
||||
delegate?.dispatch(.ioError, bubbles: false, data: nil)
|
||||
logger.warn("connection timedout")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -358,7 +358,7 @@ open class RTMPStream: NetStream {
|
|||
self.rtmpConnection = connection
|
||||
super.init()
|
||||
dispatcher = EventDispatcher(target: self)
|
||||
rtmpConnection.addEventListener(Event.RTMP_STATUS, selector: #selector(on(status:)), observer: self)
|
||||
rtmpConnection.addEventListener(.rtmpStatus, selector: #selector(on(status:)), observer: self)
|
||||
if rtmpConnection.connected {
|
||||
rtmpConnection.createStream(self)
|
||||
}
|
||||
|
@ -366,7 +366,7 @@ open class RTMPStream: NetStream {
|
|||
|
||||
deinit {
|
||||
mixer.stopRunning()
|
||||
rtmpConnection.removeEventListener(Event.RTMP_STATUS, selector: #selector(on(status:)), observer: self)
|
||||
rtmpConnection.removeEventListener(.rtmpStatus, selector: #selector(on(status:)), observer: self)
|
||||
}
|
||||
|
||||
open func play(_ arguments: Any?...) {
|
||||
|
@ -612,16 +612,16 @@ extension RTMPStream {
|
|||
|
||||
extension RTMPStream: IEventDispatcher {
|
||||
// MARK: IEventDispatcher
|
||||
public func addEventListener(_ type: String, selector: Selector, observer: AnyObject? = nil, useCapture: Bool = false) {
|
||||
public func addEventListener(_ type: Event.Name, selector: Selector, observer: AnyObject? = nil, useCapture: Bool = false) {
|
||||
dispatcher.addEventListener(type, selector: selector, observer: observer, useCapture: useCapture)
|
||||
}
|
||||
public func removeEventListener(_ type: String, selector: Selector, observer: AnyObject? = nil, useCapture: Bool = false) {
|
||||
public func removeEventListener(_ type: Event.Name, selector: Selector, observer: AnyObject? = nil, useCapture: Bool = false) {
|
||||
dispatcher.removeEventListener(type, selector: selector, observer: observer, useCapture: useCapture)
|
||||
}
|
||||
public func dispatch(event: Event) {
|
||||
dispatcher.dispatch(event: event)
|
||||
}
|
||||
public func dispatch(_ type: String, bubbles: Bool, data: Any?) {
|
||||
public func dispatch(_ type: Event.Name, bubbles: Bool, data: Any?) {
|
||||
dispatcher.dispatch(type, bubbles: bubbles, data: data)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -113,7 +113,7 @@ final class RTMPTSocket: NSObject, RTMPSocketCompatible {
|
|||
if isDisconnected {
|
||||
let data: ASObject = (readyState == .handshakeDone) ?
|
||||
RTMPConnection.Code.connectClosed.data("") : RTMPConnection.Code.connectFailed.data("")
|
||||
events.append(Event(type: Event.RTMP_STATUS, bubbles: false, data: data))
|
||||
events.append(Event(type: .rtmpStatus, bubbles: false, data: data))
|
||||
}
|
||||
guard let connectionID: String = connectionID else {
|
||||
return
|
||||
|
|
|
@ -4,10 +4,10 @@ import Foundation
|
|||
flash.events.IEventDispatcher for Swift
|
||||
*/
|
||||
public protocol IEventDispatcher: class {
|
||||
func addEventListener(_ type: String, selector: Selector, observer: AnyObject?, useCapture: Bool)
|
||||
func removeEventListener(_ type: String, selector: Selector, observer: AnyObject?, useCapture: Bool)
|
||||
func addEventListener(_ type: Event.Name, selector: Selector, observer: AnyObject?, useCapture: Bool)
|
||||
func removeEventListener(_ type: Event.Name, selector: Selector, observer: AnyObject?, useCapture: Bool)
|
||||
func dispatch(event: Event)
|
||||
func dispatch(_ type: String, bubbles: Bool, data: Any?)
|
||||
func dispatch(_ type: Event.Name, bubbles: Bool, data: Any?)
|
||||
}
|
||||
|
||||
public enum EventPhase: UInt8 {
|
||||
|
@ -22,26 +22,41 @@ public enum EventPhase: UInt8 {
|
|||
flash.events.Event for Swift
|
||||
*/
|
||||
open class Event {
|
||||
public static let SYNC: String = "sync"
|
||||
public static let EVENT: String = "event"
|
||||
public static let IO_ERROR: String = "ioError"
|
||||
public static let RTMP_STATUS: String = "rtmpStatus"
|
||||
public struct Name: RawRepresentable, ExpressibleByStringLiteral {
|
||||
public typealias RawValue = String
|
||||
public typealias StringLiteralType = String
|
||||
|
||||
public static let sync: Name = "sync"
|
||||
public static let event: Name = "event"
|
||||
public static let ioError: Name = "ioError"
|
||||
public static let rtmpStatus: Name = "rtmpStatus"
|
||||
|
||||
public let rawValue: String
|
||||
|
||||
public init(rawValue: String) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
public init(stringLiteral value: String) {
|
||||
self.rawValue = value
|
||||
}
|
||||
}
|
||||
|
||||
public static func from(_ notification: Notification) -> Event {
|
||||
guard
|
||||
let userInfo: [AnyHashable: Any] = notification.userInfo,
|
||||
let event: Event = userInfo["event"] as? Event else {
|
||||
return Event(type: Event.EVENT)
|
||||
return Event(type: .event)
|
||||
}
|
||||
return event
|
||||
}
|
||||
|
||||
open fileprivate(set) var type: String
|
||||
open fileprivate(set) var type: Name
|
||||
open fileprivate(set) var bubbles: Bool
|
||||
open fileprivate(set) var data: Any?
|
||||
open fileprivate(set) var target: AnyObject?
|
||||
|
||||
public init(type: String, bubbles: Bool = false, data: Any? = nil) {
|
||||
public init(type: Name, bubbles: Bool = false, data: Any? = nil) {
|
||||
self.type = type
|
||||
self.bubbles = bubbles
|
||||
self.data = data
|
||||
|
@ -73,15 +88,15 @@ open class EventDispatcher: IEventDispatcher {
|
|||
target = nil
|
||||
}
|
||||
|
||||
public func addEventListener(_ type: String, selector: Selector, observer: AnyObject? = nil, useCapture: Bool = false) {
|
||||
public func addEventListener(_ type: Event.Name, selector: Selector, observer: AnyObject? = nil, useCapture: Bool = false) {
|
||||
NotificationCenter.default.addObserver(
|
||||
observer ?? target ?? self, selector: selector, name: Notification.Name(rawValue: "\(type)/\(useCapture)"), object: target ?? self
|
||||
observer ?? target ?? self, selector: selector, name: Notification.Name(rawValue: "\(type.rawValue)/\(useCapture)"), object: target ?? self
|
||||
)
|
||||
}
|
||||
|
||||
public func removeEventListener(_ type: String, selector: Selector, observer: AnyObject? = nil, useCapture: Bool = false) {
|
||||
public func removeEventListener(_ type: Event.Name, selector: Selector, observer: AnyObject? = nil, useCapture: Bool = false) {
|
||||
NotificationCenter.default.removeObserver(
|
||||
observer ?? target ?? self, name: Notification.Name(rawValue: "\(type)/\(useCapture)"), object: target ?? self
|
||||
observer ?? target ?? self, name: Notification.Name(rawValue: "\(type.rawValue)/\(useCapture)"), object: target ?? self
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -93,7 +108,7 @@ open class EventDispatcher: IEventDispatcher {
|
|||
event.target = nil
|
||||
}
|
||||
|
||||
public func dispatch(_ type: String, bubbles: Bool, data: Any?) {
|
||||
public func dispatch(_ type: Event.Name, bubbles: Bool, data: Any?) {
|
||||
dispatch(event: Event(type: type, bubbles: bubbles, data: data))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue