|
| 1 | +import Foundation |
| 2 | + |
| 3 | +// Marker structs for type safety (structs work with == constraints in extensions) |
| 4 | +public struct _Root {} |
| 5 | +public struct _Database {} |
| 6 | +public struct _Collection {} |
| 7 | +public struct _Document {} |
| 8 | +public struct _TablesDB {} |
| 9 | +public struct _Table {} |
| 10 | +public struct _Row {} |
| 11 | +public struct _Bucket {} |
| 12 | +public struct _File {} |
| 13 | +public struct _Func {} |
| 14 | +public struct _Execution {} |
| 15 | +public struct _Team {} |
| 16 | +public struct _Membership {} |
| 17 | +public struct _Resolved {} |
| 18 | + |
| 19 | +// Helper for normalizing IDs |
| 20 | +private func normalize(_ id: String) -> String { |
| 21 | + let trimmed = id.trimmingCharacters(in: .whitespacesAndNewlines) |
| 22 | + return trimmed.isEmpty ? "*" : trimmed |
| 23 | +} |
| 24 | + |
| 25 | +/// Channel class with generic type parameter for type-safe method chaining |
| 26 | +public class RealtimeChannel<T> { |
| 27 | + private let segments: [String] |
| 28 | + |
| 29 | + internal init(_ segments: [String]) { |
| 30 | + self.segments = segments |
| 31 | + } |
| 32 | + |
| 33 | + /// Internal helper to transition to next state with segment and ID |
| 34 | + internal func next<N>(_ segment: String, _ id: String = "*") -> RealtimeChannel<N> { |
| 35 | + return RealtimeChannel<N>(segments + [segment, normalize(id)]) |
| 36 | + } |
| 37 | + |
| 38 | + /// Internal helper for terminal actions (no ID segment) |
| 39 | + internal func resolve(_ action: String) -> RealtimeChannel<_Resolved> { |
| 40 | + return RealtimeChannel<_Resolved>(segments + [action]) |
| 41 | + } |
| 42 | + |
| 43 | + /// Convert channel to string representation |
| 44 | + public func toString() -> String { |
| 45 | + return segments.joined(separator: ".") |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +/// Non-generic Channel enum for static factory methods |
| 50 | +public enum Channel { |
| 51 | + |
| 52 | + // MARK: - Root Factories |
| 53 | + |
| 54 | + public static func database(_ id: String = "*") -> RealtimeChannel<_Database> { |
| 55 | + return RealtimeChannel<_Database>(["databases", normalize(id)]) |
| 56 | + } |
| 57 | + |
| 58 | + public static func tablesdb(_ id: String = "*") -> RealtimeChannel<_TablesDB> { |
| 59 | + return RealtimeChannel<_TablesDB>(["tablesdb", normalize(id)]) |
| 60 | + } |
| 61 | + |
| 62 | + public static func bucket(_ id: String = "*") -> RealtimeChannel<_Bucket> { |
| 63 | + return RealtimeChannel<_Bucket>(["buckets", normalize(id)]) |
| 64 | + } |
| 65 | + |
| 66 | + public static func function(_ id: String = "*") -> RealtimeChannel<_Func> { |
| 67 | + return RealtimeChannel<_Func>(["functions", normalize(id)]) |
| 68 | + } |
| 69 | + |
| 70 | + public static func team(_ id: String = "*") -> RealtimeChannel<_Team> { |
| 71 | + return RealtimeChannel<_Team>(["teams", normalize(id)]) |
| 72 | + } |
| 73 | + |
| 74 | + public static func membership(_ id: String = "*") -> RealtimeChannel<_Membership> { |
| 75 | + return RealtimeChannel<_Membership>(["memberships", normalize(id)]) |
| 76 | + } |
| 77 | + |
| 78 | + public static func account(_ userId: String = "") -> String { |
| 79 | + let id = normalize(userId) |
| 80 | + return id == "*" ? "account" : "account.\(id)" |
| 81 | + } |
| 82 | + |
| 83 | + // Global events |
| 84 | + public static func documents() -> String { "documents" } |
| 85 | + public static func rows() -> String { "rows" } |
| 86 | + public static func files() -> String { "files" } |
| 87 | + public static func executions() -> String { "executions" } |
| 88 | + public static func teams() -> String { "teams" } |
| 89 | +} |
| 90 | + |
| 91 | +// MARK: - DATABASE ROUTE |
| 92 | +// Protocol extensions restricted by receiver type |
| 93 | + |
| 94 | +/// Only available on RealtimeChannel<_Database> |
| 95 | +extension RealtimeChannel where T == _Database { |
| 96 | + public func collection(_ id: String = "*") -> RealtimeChannel<_Collection> { |
| 97 | + return self.next("collections", id) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +/// Only available on RealtimeChannel<_Collection> |
| 102 | +extension RealtimeChannel where T == _Collection { |
| 103 | + public func document(_ id: String = "*") -> RealtimeChannel<_Document> { |
| 104 | + return self.next("documents", id) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// MARK: - TABLESDB ROUTE |
| 109 | + |
| 110 | +/// Only available on RealtimeChannel<_TablesDB> |
| 111 | +extension RealtimeChannel where T == _TablesDB { |
| 112 | + public func table(_ id: String = "*") -> RealtimeChannel<_Table> { |
| 113 | + return self.next("tables", id) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +/// Only available on RealtimeChannel<_Table> |
| 118 | +extension RealtimeChannel where T == _Table { |
| 119 | + public func row(_ id: String = "*") -> RealtimeChannel<_Row> { |
| 120 | + return self.next("rows", id) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +// MARK: - BUCKET ROUTE |
| 125 | + |
| 126 | +/// Only available on RealtimeChannel<_Bucket> |
| 127 | +extension RealtimeChannel where T == _Bucket { |
| 128 | + public func file(_ id: String = "*") -> RealtimeChannel<_File> { |
| 129 | + return self.next("files", id) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +// MARK: - FUNCTION ROUTE |
| 134 | + |
| 135 | +/// Only available on RealtimeChannel<_Func> |
| 136 | +extension RealtimeChannel where T == _Func { |
| 137 | + public func execution(_ id: String = "*") -> RealtimeChannel<_Execution> { |
| 138 | + return self.next("executions", id) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +// MARK: - TERMINAL ACTIONS |
| 143 | +// Restricted to actionable types (_Document, _Row, _File, _Execution, _Team, _Membership) |
| 144 | + |
| 145 | +/// Only available on RealtimeChannel<_Document> |
| 146 | +extension RealtimeChannel where T == _Document { |
| 147 | + public func create() -> RealtimeChannel<_Resolved> { |
| 148 | + return self.resolve("create") |
| 149 | + } |
| 150 | + |
| 151 | + public func update() -> RealtimeChannel<_Resolved> { |
| 152 | + return self.resolve("update") |
| 153 | + } |
| 154 | + |
| 155 | + public func delete() -> RealtimeChannel<_Resolved> { |
| 156 | + return self.resolve("delete") |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +/// Only available on RealtimeChannel<_Row> |
| 161 | +extension RealtimeChannel where T == _Row { |
| 162 | + public func create() -> RealtimeChannel<_Resolved> { |
| 163 | + return self.resolve("create") |
| 164 | + } |
| 165 | + |
| 166 | + public func update() -> RealtimeChannel<_Resolved> { |
| 167 | + return self.resolve("update") |
| 168 | + } |
| 169 | + |
| 170 | + public func delete() -> RealtimeChannel<_Resolved> { |
| 171 | + return self.resolve("delete") |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +/// Only available on RealtimeChannel<_File> |
| 176 | +extension RealtimeChannel where T == _File { |
| 177 | + public func create() -> RealtimeChannel<_Resolved> { |
| 178 | + return self.resolve("create") |
| 179 | + } |
| 180 | + |
| 181 | + public func update() -> RealtimeChannel<_Resolved> { |
| 182 | + return self.resolve("update") |
| 183 | + } |
| 184 | + |
| 185 | + public func delete() -> RealtimeChannel<_Resolved> { |
| 186 | + return self.resolve("delete") |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +/// Only available on RealtimeChannel<_Execution> |
| 191 | +extension RealtimeChannel where T == _Execution { |
| 192 | + public func create() -> RealtimeChannel<_Resolved> { |
| 193 | + return self.resolve("create") |
| 194 | + } |
| 195 | + |
| 196 | + public func update() -> RealtimeChannel<_Resolved> { |
| 197 | + return self.resolve("update") |
| 198 | + } |
| 199 | + |
| 200 | + public func delete() -> RealtimeChannel<_Resolved> { |
| 201 | + return self.resolve("delete") |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +/// Only available on RealtimeChannel<_Team> |
| 206 | +extension RealtimeChannel where T == _Team { |
| 207 | + public func create() -> RealtimeChannel<_Resolved> { |
| 208 | + return self.resolve("create") |
| 209 | + } |
| 210 | + |
| 211 | + public func update() -> RealtimeChannel<_Resolved> { |
| 212 | + return self.resolve("update") |
| 213 | + } |
| 214 | + |
| 215 | + public func delete() -> RealtimeChannel<_Resolved> { |
| 216 | + return self.resolve("delete") |
| 217 | + } |
| 218 | +} |
| 219 | + |
| 220 | +/// Only available on RealtimeChannel<_Membership> |
| 221 | +extension RealtimeChannel where T == _Membership { |
| 222 | + public func create() -> RealtimeChannel<_Resolved> { |
| 223 | + return self.resolve("create") |
| 224 | + } |
| 225 | + |
| 226 | + public func update() -> RealtimeChannel<_Resolved> { |
| 227 | + return self.resolve("update") |
| 228 | + } |
| 229 | + |
| 230 | + public func delete() -> RealtimeChannel<_Resolved> { |
| 231 | + return self.resolve("delete") |
| 232 | + } |
| 233 | +} |
| 234 | + |
| 235 | +// MARK: - Protocol for backward compatibility |
| 236 | + |
| 237 | +/// Protocol for channel values that can be converted to strings |
| 238 | +public protocol ChannelValue { |
| 239 | + func toString() -> String |
| 240 | +} |
| 241 | + |
| 242 | +// Make String conform to ChannelValue |
| 243 | +extension String: ChannelValue { |
| 244 | + public func toString() -> String { |
| 245 | + return self |
| 246 | + } |
| 247 | +} |
| 248 | + |
| 249 | +// Make RealtimeChannel conform to ChannelValue |
| 250 | +extension RealtimeChannel: ChannelValue {} |
0 commit comments