Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

Commit

Permalink
Serialization v3
Browse files Browse the repository at this point in the history
  • Loading branch information
karkakol committed Jan 15, 2024
1 parent 1e6eccd commit 180f503
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
10 changes: 5 additions & 5 deletions Sources/MembraneRTC/Events/Event.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ public protocol ReceivableEvent {
var type: ReceivableEventType { get }
}

internal struct ReceivableEventBase: Decodable {
public struct ReceivableEventBase: Decodable {
let type: ReceivableEventType
}

public enum Events {
internal static func decodeEvent<T: Decodable>(from data: Data) -> T? {
public static func decodeEvent<T: Decodable>(from data: Data) -> T? {
do {
return try JSONDecoder().decode(T.self, from: data)
} catch {
Expand Down Expand Up @@ -349,9 +349,9 @@ struct OfferDataEvent: ReceivableEvent, Codable {
let data: Data
}

struct TracksAddedEvent: ReceivableEvent, Codable {
struct Data: Codable {
struct TrackData: Codable{
public struct TracksAddedEvent: ReceivableEvent, Codable {
public struct Data: Codable {
public struct TrackData: Codable{
let metadata: Metadata
let simulcastConfig: SimulcastConfig?
}
Expand Down
26 changes: 18 additions & 8 deletions Sources/MembraneRTC/Types/Endpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,59 @@ public struct Endpoint: Codable {
public let type: String
public let metadata: Metadata
public let trackIdToMetadata: [String: Metadata]?
public let tracks: [String: TracksAddedEvent.Data.TrackData]?

public init(id: String, type: String, metadata: Metadata?, trackIdToMetadata: [String: Metadata]?) {
public init(id: String, type: String, metadata: Metadata?, trackIdToMetadata: [String: Metadata]?, tracks: [String: TracksAddedEvent.Data.TrackData]?) {
self.id = id
self.type = type
self.metadata = metadata ?? Metadata()
self.trackIdToMetadata = trackIdToMetadata
self.tracks = tracks
}

public func with(
id: String? = nil, type: String? = nil, metadata: Metadata? = nil, trackIdToMetadata: [String: Metadata]? = nil
id: String? = nil, type: String? = nil, metadata: Metadata? = nil, trackIdToMetadata: [String: Metadata]? = nil, tracks: [String: TracksAddedEvent.Data.TrackData]?
) -> Self {
return Endpoint(
id: id ?? self.id,
type: type ?? self.type,
metadata: metadata ?? self.metadata,
trackIdToMetadata: trackIdToMetadata ?? self.trackIdToMetadata
trackIdToMetadata: trackIdToMetadata ?? self.trackIdToMetadata,
tracks: tracks ?? self.tracks
)
}

public func withTrack(trackId: String, metadata: Metadata?) -> Self {
var newTrackIdToMetadata = self.trackIdToMetadata
newTrackIdToMetadata?[trackId] = metadata ?? Metadata()

var newTracks = self.tracks
let simulcastConfig = newTracks?[trackId]?.simulcastConfig
newTracks?[trackId] = TracksAddedEvent.Data.TrackData(metadata: metadata ?? Metadata(), simulcastConfig: simulcastConfig)

return Endpoint(id: self.id, type: self.type, metadata: self.metadata, trackIdToMetadata: newTrackIdToMetadata)
return Endpoint(id: self.id, type: self.type, metadata: self.metadata, trackIdToMetadata: newTrackIdToMetadata, tracks: newTracks)
}

public func withoutTrack(trackId: String) -> Self {
var newTrackIdToMetadata = self.trackIdToMetadata
newTrackIdToMetadata?.removeValue(forKey: trackId)

var newTracks = self.tracks
newTracks?.removeValue(forKey: trackId)

return Endpoint(id: self.id, type: self.type, metadata: self.metadata, trackIdToMetadata: newTrackIdToMetadata)
return Endpoint(id: self.id, type: self.type, metadata: self.metadata, trackIdToMetadata: newTrackIdToMetadata, tracks: newTracks)
}

enum CodingKeys: String, CodingKey {
case id, type, metadata, trackIdToMetadata
case id, type, metadata, trackIdToMetadata, tracks
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.id = try container.decode(String.self, forKey: .id)
self.type = try container.decode(String.self, forKey: .type)
self.metadata = try container.decodeIfPresent(Metadata.self, forKey: .metadata) ?? Metadata()
self.trackIdToMetadata = try container.decodeIfPresent(
[String: Metadata].self, forKey: .trackIdToMetadata)
self.trackIdToMetadata = try container.decodeIfPresent([String: Metadata].self, forKey: .trackIdToMetadata)
self.tracks = try container.decodeIfPresent([String: TracksAddedEvent.Data.TrackData].self, forKey: .tracks)
}
}

0 comments on commit 180f503

Please sign in to comment.