Skip to content

Instantly share code, notes, and snippets.

@bannzai
Last active September 25, 2019 16:27
Show Gist options
  • Save bannzai/901b9b576dc097a63060980ca88e4e00 to your computer and use it in GitHub Desktop.
Save bannzai/901b9b576dc097a63060980ca88e4e00 to your computer and use it in GitHub Desktop.
import Foundation
// Library A start
public typealias MediaSourceConnection = Any
public struct MediaEvent<T> {
init<T>(_ t: T) { }
}
public protocol MediaSink: AnyObject {
associatedtype InputMedia
func didConnect<X: MediaSourceConnection>(connection: X)
func close()
func send<X: MediaSourceConnection>(event: MediaEvent<InputMedia>,
connection: X) -> Bool
}
// Library A end
// My Framework B start
internal protocol SyncronizedMediaSink: MediaSink { }
internal protocol MediaSinkProxy {
associatedtype ProxyInputMedia
var queue: DispatchQueue { get }
func syncDidConnect<X: MediaSourceConnection>(connection: X)
func syncClose()
func syncSend<X: MediaSourceConnection>(event: MediaEvent<ProxyInputMedia>, connection: X) -> Bool
}
public protocol MediaSinkInterface {
associatedtype I: MediaSink
func interface() -> I
}
internal extension SyncronizedMediaSink where Self: MediaSinkProxy, Self.ProxyInputMedia == Self.InputMedia {
func syncDidConnect<X>(connection: X) {
queue.sync {
didConnect(connection: connection)
}
}
func syncClose() {
queue.sync {
close()
}
}
func syncSend<X>(event: MediaEvent<InputMedia>, connection: X) -> Bool {
queue.sync {
send(event: event, connection: connection)
}
}
}
public class VideoDisplaySink: MediaSinkProxy {
public typealias InputMedia = Int
typealias ProxyInputMedia = InputMedia
internal let queue: DispatchQueue = .main
}
extension VideoDisplaySink: SyncronizedMediaSink {
public func didConnect<X>(connection: X) {
// do something
}
public func close() {
// do something
}
public func send<X>(event: MediaEvent<Int>, connection: X) -> Bool {
// do something
return false
}
}
extension VideoDisplaySink: MediaSinkInterface {
public func interface() -> some MediaSink {
return self
}
}
func a<T: MediaSink>(_ a: T) {
}
// call
VideoDisplaySink().syncClose()
// pass
a(VideoDisplaySink())
// My Framework B end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment