Skip to content

Commit

Permalink
update lib for Swift 3
Browse files Browse the repository at this point in the history
  • Loading branch information
almassapargali committed Sep 21, 2016
1 parent 275a059 commit 9cc6407
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 116 deletions.
2 changes: 1 addition & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "daltoniam/Starscream" ~> 1.1.2
github "daltoniam/Starscream" ~> 2.0.0
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "daltoniam/Starscream" "1.1.1"
github "daltoniam/Starscream" "2.0.0"
12 changes: 11 additions & 1 deletion PhoenixWebSocket.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,12 @@
B6FA23A01C63661300AF0893 /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0720;
LastUpgradeCheck = 0800;
ORGANIZATIONNAME = "Almas Sapargali";
TargetAttributes = {
B6FA23A81C63661400AF0893 = {
CreatedOnToolsVersion = 7.2;
LastSwiftMigration = 0800;
};
};
};
Expand Down Expand Up @@ -169,8 +170,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
Expand Down Expand Up @@ -217,8 +220,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
Expand All @@ -238,6 +243,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
VERSIONING_SYSTEM = "apple-generic";
Expand All @@ -248,6 +254,7 @@
B6FA23B21C63661400AF0893 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -263,12 +270,14 @@
PRODUCT_BUNDLE_IDENTIFIER = com.almassapargali.PhoenixWebSocket;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
};
name = Debug;
};
B6FA23B31C63661400AF0893 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -284,6 +293,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.almassapargali.PhoenixWebSocket;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0720"
LastUpgradeVersion = "0800"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
32 changes: 16 additions & 16 deletions PhoenixWebSocket/Channel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,33 @@ public class Channel {
didSet { onStatus?(status) }
}

private var onStatus: (Status -> ())?
fileprivate var onStatus: ((Status) -> ())?

/// Payload to send when joining channel.
public var joinPayload: Message.JSON?

private var bindings = [Binding]()
fileprivate var bindings = [Binding]()

public init(topic: String, onStatusChange: (Status -> ())? = nil) {
public init(topic: String, onStatusChange: ((Status) -> ())? = nil) {
self.topic = topic
onStatus = onStatusChange
status = .Disconnected(nil)
status = .disconnected(nil)
}

/// This will override any previous `onStatusChange` calls
/// or callback given to initializer.
public func onStatusChange(callback: Status -> ()) -> Self {
public func onStatusChange(_ callback: @escaping (Status) -> ()) -> Self {
onStatus = callback
return self
}

public func on(event: String, callback: Message -> ()) -> Self {
public func on(_ event: String, callback: @escaping (Message) -> ()) -> Self {
bindings.append(Binding(event: event, callback: callback))
return self
}

// call this when message recieved for this channel
func recieved(message: Message) {
func recieved(_ message: Message) {
// just in case, should never happen
guard message.topic == topic else { return }

Expand All @@ -51,28 +51,28 @@ public class Channel {

struct Binding {
let event: String
let callback: Message -> ()
let callback: (Message) -> ()
}

public enum Status {
case Joining
case joining

/// When channel successfully joined, contains server response.
case Joined(Message.JSON)
case joined(Message.JSON)

/// When joining rejected by server (i.e. server replied with `{:error, response}`),
/// contains error reason and response dic.
case Rejected(String, Message.JSON)
case rejected(String, Message.JSON)

/// Joining failed by transport related errors.
case JoinFailed(SendError)
case joinFailed(SendError)

/// Disconnected from server, contains error if any.
case Disconnected(NSError?)
case disconnected(NSError?)

func isJoined() -> Bool {
switch self {
case .Joined(_): return true
case .joined(_): return true
default: return false
}
}
Expand All @@ -81,11 +81,11 @@ public class Channel {

extension Channel: Equatable { }

@warn_unused_result

public func ==(lhs: Channel, rhs: Channel) -> Bool {
return lhs.topic == rhs.topic
}

extension Channel: Hashable {
public var hashValue: Int { return topic.hashValue }
}
}
19 changes: 10 additions & 9 deletions PhoenixWebSocket/Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,21 @@ public struct Message {
// broadcasted messages doesn't have ref
let ref: String?

func toJson() throws -> NSData {
let dic = ["topic": topic, "event": event, "payload": payload, "ref": ref ?? ""]
return try NSJSONSerialization.dataWithJSONObject(dic, options: NSJSONWritingOptions())
func toJson() throws -> Data {
let dic = ["topic": topic, "event": event, "payload": payload, "ref": ref ?? ""] as [String : Any]
return try JSONSerialization.data(withJSONObject: dic, options: JSONSerialization.WritingOptions())
}

init(_ event: String, topic: String, payload: JSON, ref: String = NSUUID().UUIDString) {
init(_ event: String, topic: String, payload: JSON, ref: String = UUID().uuidString) {
(self.topic, self.event, self.payload, self.ref) = (topic, event, payload, ref)
}

init?(data: NSData) {
let jsonObject = try? NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions())
init?(data: Data) {
let jsonObject = try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions())
guard let json = jsonObject as? JSON,
topic = json["topic"] as? String, event = json["event"] as? String,
payload = json["payload"] as? JSON
let topic = json["topic"] as? String,
let event = json["event"] as? String,
let payload = json["payload"] as? JSON
else { return nil }
(self.topic, self.event, self.payload) = (topic, event, payload)
ref = json["ref"] as? String
Expand All @@ -44,4 +45,4 @@ extension Message: CustomStringConvertible {
let type = ref == nil ? "Broadcast" : "Reply"
return "\(type) Message[topic: \(topic), event: \(event), payload: \(payload)]"
}
}
}
32 changes: 16 additions & 16 deletions PhoenixWebSocket/Response.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,52 +8,52 @@

import Foundation

public enum ResponseError: ErrorType {
public enum ResponseError: Error {
/// `status` or `response` key is missing.
case InvalidFormat
case invalidFormat

/// Missing `reason` key on error response.
case MissingReason
case missingReason
}

extension ResponseError: CustomStringConvertible {
public var description: String {
switch self {
case .InvalidFormat: return "Invalid response format."
case .MissingReason: return "Error response is missing reason."
case .invalidFormat: return "Invalid response format."
case .missingReason: return "Error response is missing reason."
}
}
}

public enum Response {
case Ok(Message.JSON)
case ok(Message.JSON)
/// Error responses assumed to have `reason` key with `String` value
///
/// Tuple containing of `reason` value and `response` dic
case Error(String, Message.JSON)
case error(String, Message.JSON)

public static func fromPayload(payload: Message.JSON) throws -> Response {
guard let status = payload["status"] as? String where ["ok", "error"].contains(status),
public static func fromPayload(_ payload: Message.JSON) throws -> Response {
guard let status = payload["status"] as? String, ["ok", "error"].contains(status),
let response = payload["response"] as? Message.JSON else {
throw ResponseError.InvalidFormat
throw ResponseError.invalidFormat
}

if status == "ok" { return .Ok(response) }
if status == "ok" { return .ok(response) }

// only error statuses pass here
if let reason = response["reason"] as? String {
return .Error(reason, response)
return .error(reason, response)
} else {
throw ResponseError.MissingReason
throw ResponseError.missingReason
}
}
}

extension Response: CustomStringConvertible {
public var description: String {
switch self {
case .Ok(let response): return "Response.Ok: \(response)"
case .Error(_, let response): return "Response.Error: \(response)"
case .ok(let response): return "Response.Ok: \(response)"
case .error(_, let response): return "Response.Error: \(response)"
}
}
}
}
Loading

0 comments on commit 9cc6407

Please sign in to comment.