Skip to content

Instantly share code, notes, and snippets.

@tomasharkema
Last active April 25, 2016 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomasharkema/7f1e06808068bf648bcad6cc6a1f59a9 to your computer and use it in GitHub Desktop.
Save tomasharkema/7f1e06808068bf648bcad6cc6a1f59a9 to your computer and use it in GitHub Desktop.
protocol Method {
associatedtype ReturnType
static var name: String { get }
var parameters: [AnyObject]? { get }
}
extension METDDPClient {
func callMethod<T : Method>(method: T) -> Promise<T.ReturnType, MeteorError> {
return callMethodWithNamePromise(T.name, parameters: method.parameters)
}
}
protocol SubscriptionRequest {
static var name: String { get }
var parameters: [AnyObject]? { get }
}
extension METDDPClient {
func addSubscription<T : SubscriptionRequest>(request: T) -> Promise<METSubscription, MeteorError> {
return addSubscriptionPromise(T.name, parameters: request.parameters)
}
}
// Examples
struct RegisterPushToken {
let deviceId: String
let token: String
let tokenType: String
}
extension RegisterPushToken: Method {
typealias ReturnType = Bool
static var name: String { return "RegisterPushToken" }
var parameters: [AnyObject]? {
return [deviceId,
["token": token,
"tokenType": tokenType]]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment