Skip to content

Commit

Permalink
Expose Subscription properties (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbaker6 authored Jan 25, 2021
1 parent 39f20d2 commit 1db0674
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 86 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# Parse-Swift Changelog

### main
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.1.0...main)
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.1.1...main)
* _Contributing to this repo? Add info about your change here to be included in next release_

### 1.1.1
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.1.0...1.1.1)

__Fixes__
- Expose `ParseLiveQuery` subscription properties ([#66](https://github.com/parse-community/Parse-Swift/pull/66)), thanks to [Corey Baker](https://github.com/cbaker6).

### 1.1.0
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.0.2...1.1.0)

Expand Down
2 changes: 1 addition & 1 deletion ParseSwift.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "ParseSwift"
s.version = "1.1.0"
s.version = "1.1.1"
s.summary = "Parse Pure Swift SDK"
s.homepage = "https://github.com/parse-community/Parse-Swift"
s.authors = {
Expand Down
26 changes: 18 additions & 8 deletions ParseSwift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@
705A9A3025991C1400B3547F /* Fileable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 705A9A2E25991C1400B3547F /* Fileable.swift */; };
705A9A3125991C1400B3547F /* Fileable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 705A9A2E25991C1400B3547F /* Fileable.swift */; };
705A9A3225991C1400B3547F /* Fileable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 705A9A2E25991C1400B3547F /* Fileable.swift */; };
705D950825BE4C08003EF6F8 /* SubscriptionCallback.swift in Sources */ = {isa = PBXBuildFile; fileRef = 705D950725BE4C08003EF6F8 /* SubscriptionCallback.swift */; };
705D950925BE4C08003EF6F8 /* SubscriptionCallback.swift in Sources */ = {isa = PBXBuildFile; fileRef = 705D950725BE4C08003EF6F8 /* SubscriptionCallback.swift */; };
705D950A25BE4C08003EF6F8 /* SubscriptionCallback.swift in Sources */ = {isa = PBXBuildFile; fileRef = 705D950725BE4C08003EF6F8 /* SubscriptionCallback.swift */; };
705D950B25BE4C08003EF6F8 /* SubscriptionCallback.swift in Sources */ = {isa = PBXBuildFile; fileRef = 705D950725BE4C08003EF6F8 /* SubscriptionCallback.swift */; };
70647E8E259E3375004C1004 /* LocallyIdentifiable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 70647E8D259E3375004C1004 /* LocallyIdentifiable.swift */; };
70647E8F259E3375004C1004 /* LocallyIdentifiable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 70647E8D259E3375004C1004 /* LocallyIdentifiable.swift */; };
70647E90259E3375004C1004 /* LocallyIdentifiable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 70647E8D259E3375004C1004 /* LocallyIdentifiable.swift */; };
Expand Down Expand Up @@ -469,6 +473,7 @@
705727882593FF8000F0ADD5 /* ParseFileTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ParseFileTests.swift; sourceTree = "<group>"; };
705A99F8259807F900B3547F /* ParseFileManagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ParseFileManagerTests.swift; sourceTree = "<group>"; };
705A9A2E25991C1400B3547F /* Fileable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Fileable.swift; sourceTree = "<group>"; };
705D950725BE4C08003EF6F8 /* SubscriptionCallback.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SubscriptionCallback.swift; sourceTree = "<group>"; };
70647E8D259E3375004C1004 /* LocallyIdentifiable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocallyIdentifiable.swift; sourceTree = "<group>"; };
70647E9B259E3A9A004C1004 /* ParseType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ParseType.swift; sourceTree = "<group>"; };
707A3BF025B0A4F0000D215C /* ParseAuthentication.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ParseAuthentication.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -825,6 +830,7 @@
700395A225A119430052CB31 /* Operations.swift */,
7003960825A184EF0052CB31 /* ParseLiveQuery.swift */,
700395B925A1470F0052CB31 /* Subscription.swift */,
705D950725BE4C08003EF6F8 /* SubscriptionCallback.swift */,
700395DE25A147C40052CB31 /* Protocols */,
);
path = LiveQuery;
Expand Down Expand Up @@ -1437,6 +1443,7 @@
707A3C2025B14BD0000D215C /* ParseApple.swift in Sources */,
F97B462224D9C6F200F4A88B /* ParseKeyValueStore.swift in Sources */,
F97B45E624D9C6F200F4A88B /* Query.swift in Sources */,
705D950825BE4C08003EF6F8 /* SubscriptionCallback.swift in Sources */,
70C5509225B4A99100B5DBC2 /* AddRelation.swift in Sources */,
708D035225215F9B00646C70 /* Deletable.swift in Sources */,
F97B466424D9C88600F4A88B /* SecureStorage.swift in Sources */,
Expand Down Expand Up @@ -1548,6 +1555,7 @@
707A3C2125B14BD0000D215C /* ParseApple.swift in Sources */,
F97B462324D9C6F200F4A88B /* ParseKeyValueStore.swift in Sources */,
F97B45E724D9C6F200F4A88B /* Query.swift in Sources */,
705D950925BE4C08003EF6F8 /* SubscriptionCallback.swift in Sources */,
70C5509325B4A99100B5DBC2 /* AddRelation.swift in Sources */,
708D035325215F9B00646C70 /* Deletable.swift in Sources */,
F97B466524D9C88600F4A88B /* SecureStorage.swift in Sources */,
Expand Down Expand Up @@ -1705,6 +1713,7 @@
707A3C2325B14BD0000D215C /* ParseApple.swift in Sources */,
F97B462124D9C6F200F4A88B /* ParseStorage.swift in Sources */,
F97B466724D9C88600F4A88B /* SecureStorage.swift in Sources */,
705D950B25BE4C08003EF6F8 /* SubscriptionCallback.swift in Sources */,
70C5509525B4A99100B5DBC2 /* AddRelation.swift in Sources */,
708D035525215F9B00646C70 /* Deletable.swift in Sources */,
70110D55250680140091CC1D /* ParseConstants.swift in Sources */,
Expand Down Expand Up @@ -1779,6 +1788,7 @@
707A3C2225B14BD0000D215C /* ParseApple.swift in Sources */,
F97B462024D9C6F200F4A88B /* ParseStorage.swift in Sources */,
F97B466624D9C88600F4A88B /* SecureStorage.swift in Sources */,
705D950A25BE4C08003EF6F8 /* SubscriptionCallback.swift in Sources */,
70C5509425B4A99100B5DBC2 /* AddRelation.swift in Sources */,
708D035425215F9B00646C70 /* Deletable.swift in Sources */,
70110D54250680140091CC1D /* ParseConstants.swift in Sources */,
Expand Down Expand Up @@ -2045,7 +2055,7 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MARKETING_VERSION = 1.1.0;
MARKETING_VERSION = 1.1.1;
PRODUCT_BUNDLE_IDENTIFIER = com.parse.ParseSwift;
PRODUCT_NAME = ParseSwift;
SKIP_INSTALL = YES;
Expand All @@ -2067,7 +2077,7 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MARKETING_VERSION = 1.1.0;
MARKETING_VERSION = 1.1.1;
PRODUCT_BUNDLE_IDENTIFIER = com.parse.ParseSwift;
PRODUCT_NAME = ParseSwift;
SKIP_INSTALL = YES;
Expand Down Expand Up @@ -2131,7 +2141,7 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.13;
MARKETING_VERSION = 1.1.0;
MARKETING_VERSION = 1.1.1;
PRODUCT_BUNDLE_IDENTIFIER = com.parse.ParseSwift;
PRODUCT_NAME = ParseSwift;
SDKROOT = macosx;
Expand All @@ -2155,7 +2165,7 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.13;
MARKETING_VERSION = 1.1.0;
MARKETING_VERSION = 1.1.1;
PRODUCT_BUNDLE_IDENTIFIER = com.parse.ParseSwift;
PRODUCT_NAME = ParseSwift;
SDKROOT = macosx;
Expand Down Expand Up @@ -2300,7 +2310,7 @@
INFOPLIST_FILE = "ParseSwift-watchOS/Info.plist";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MARKETING_VERSION = 1.1.0;
MARKETING_VERSION = 1.1.1;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = "com.parse.ParseSwift-watchOS";
Expand Down Expand Up @@ -2328,7 +2338,7 @@
INFOPLIST_FILE = "ParseSwift-watchOS/Info.plist";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MARKETING_VERSION = 1.1.0;
MARKETING_VERSION = 1.1.1;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = "com.parse.ParseSwift-watchOS";
PRODUCT_NAME = ParseSwift;
Expand All @@ -2354,7 +2364,7 @@
INFOPLIST_FILE = "ParseSwift-tvOS/Info.plist";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MARKETING_VERSION = 1.1.0;
MARKETING_VERSION = 1.1.1;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = "com.parse.ParseSwift-tvOS";
Expand All @@ -2381,7 +2391,7 @@
INFOPLIST_FILE = "ParseSwift-tvOS/Info.plist";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MARKETING_VERSION = 1.1.0;
MARKETING_VERSION = 1.1.1;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = "com.parse.ParseSwift-tvOS";
PRODUCT_NAME = ParseSwift;
Expand Down
2 changes: 1 addition & 1 deletion Scripts/jazzy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ bundle exec jazzy \
--author_url http://parseplatform.org \
--github_url https://github.com/parse-community/Parse-Swift \
--root-url http://parseplatform.org/Parse-Swift/api/ \
--module-version 1.1.0 \
--module-version 1.1.1 \
--theme fullwidth \
--skip-undocumented \
--output ./docs/api \
Expand Down
81 changes: 6 additions & 75 deletions Sources/ParseSwift/LiveQuery/Subscription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ open class Subscription<T: ParseObject>: ParseSubscription, ObservableObject {
//The ParseObject
public typealias Object = T

/// Notifies there's a new event related to a specific query.
private (set) var event: (query: Query<T>, event: Event<T>)? {
/// Updates and notifies when there's a new event related to a specific query.
public internal(set) var event: (query: Query<T>, event: Event<T>)? {
willSet {
if newValue != nil {
subscribed = nil
Expand All @@ -80,8 +80,8 @@ open class Subscription<T: ParseObject>: ParseSubscription, ObservableObject {
}
}

/// Notifies when a subscription request has been fulfilled and if it is new.
private (set) var subscribed: (query: Query<T>, isNew: Bool)? {
/// Updates and notifies when a subscription request has been fulfilled and if it is new.
public internal(set) var subscribed: (query: Query<T>, isNew: Bool)? {
willSet {
if newValue != nil {
unsubscribed = nil
Expand All @@ -91,8 +91,8 @@ open class Subscription<T: ParseObject>: ParseSubscription, ObservableObject {
}
}

/// Notifies when an unsubscribe request has been fulfilled.
private (set) var unsubscribed: Query<T>? {
/// Updates and notifies when an unsubscribe request has been fulfilled.
public internal(set) var unsubscribed: Query<T>? {
willSet {
if newValue != nil {
subscribed = nil
Expand Down Expand Up @@ -131,75 +131,6 @@ open class Subscription<T: ParseObject>: ParseSubscription, ObservableObject {
}
#endif

/**
A default implementation of the `ParseSubscription` protocol using closures for callbacks.
*/
open class SubscriptionCallback<T: ParseObject>: ParseSubscription {
//The query subscribed to.
public var query: Query<T>
//The ParseObject
public typealias Object = T
fileprivate var eventHandlers: [(Query<T>, Event<T>) -> Void] = []
fileprivate var subscribeHandlers: [(Query<T>, Bool) -> Void] = []
fileprivate var unsubscribeHandlers: [(Query<T>) -> Void] = []

/**
Creates a new subscription that can be used to handle updates.
*/
public init(query: Query<T>) {
self.query = query
}

/**
Register a callback for when an event occurs.
- parameter handler: The callback to register.
- returns: The same subscription, for easy chaining.
*/
@discardableResult open func handleEvent(_ handler: @escaping (Query<T>,
Event<T>) -> Void) -> SubscriptionCallback {
eventHandlers.append(handler)
return self
}

/**
Register a callback for when a client succesfully subscribes to a query.
- parameter handler: The callback to register.
- returns: The same subscription, for easy chaining.
*/
@discardableResult open func handleSubscribe(_ handler: @escaping (Query<T>,
Bool) -> Void) -> SubscriptionCallback {
subscribeHandlers.append(handler)
return self
}

/**
Register a callback for when a query has been unsubscribed.
- parameter handler: The callback to register.
- returns: The same subscription, for easy chaining.
*/
@discardableResult open func handleUnsubscribe(_ handler: @escaping (Query<T>) -> Void) -> SubscriptionCallback {
unsubscribeHandlers.append(handler)
return self
}

open func didReceive(_ eventData: Data) throws {
// Need to decode the event with respect to the `ParseObject`.
let eventMessage = try ParseCoding.jsonDecoder().decode(EventResponse<T>.self, from: eventData)
guard let event = Event(event: eventMessage) else {
throw ParseError(code: .unknownError, message: "ParseLiveQuery Error: couldn't create event.")
}
eventHandlers.forEach { $0(query, event) }
}

open func didSubscribe(_ new: Bool) {
subscribeHandlers.forEach { $0(query, new) }
}

open func didUnsubscribe() {
unsubscribeHandlers.forEach { $0(query) }
}
}

extension SubscriptionCallback {

/**
Expand Down
78 changes: 78 additions & 0 deletions Sources/ParseSwift/LiveQuery/SubscriptionCallback.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//
// SubscriptionCallback.swift
// ParseSwift
//
// Created by Corey Baker on 1/24/21.
// Copyright © 2021 Parse Community. All rights reserved.
//

import Foundation

/**
A default implementation of the `ParseSubscription` protocol using closures for callbacks.
*/
open class SubscriptionCallback<T: ParseObject>: ParseSubscription {
//The query subscribed to.
public var query: Query<T>
//The ParseObject
public typealias Object = T
fileprivate var eventHandlers: [(Query<T>, Event<T>) -> Void] = []
fileprivate var subscribeHandlers: [(Query<T>, Bool) -> Void] = []
fileprivate var unsubscribeHandlers: [(Query<T>) -> Void] = []

/**
Creates a new subscription that can be used to handle updates.
*/
public init(query: Query<T>) {
self.query = query
}

/**
Register a callback for when an event occurs.
- parameter handler: The callback to register.
- returns: The same subscription, for easy chaining.
*/
@discardableResult open func handleEvent(_ handler: @escaping (Query<T>,
Event<T>) -> Void) -> SubscriptionCallback {
eventHandlers.append(handler)
return self
}

/**
Register a callback for when a client succesfully subscribes to a query.
- parameter handler: The callback to register.
- returns: The same subscription, for easy chaining.
*/
@discardableResult open func handleSubscribe(_ handler: @escaping (Query<T>,
Bool) -> Void) -> SubscriptionCallback {
subscribeHandlers.append(handler)
return self
}

/**
Register a callback for when a query has been unsubscribed.
- parameter handler: The callback to register.
- returns: The same subscription, for easy chaining.
*/
@discardableResult open func handleUnsubscribe(_ handler: @escaping (Query<T>) -> Void) -> SubscriptionCallback {
unsubscribeHandlers.append(handler)
return self
}

open func didReceive(_ eventData: Data) throws {
// Need to decode the event with respect to the `ParseObject`.
let eventMessage = try ParseCoding.jsonDecoder().decode(EventResponse<T>.self, from: eventData)
guard let event = Event(event: eventMessage) else {
throw ParseError(code: .unknownError, message: "ParseLiveQuery Error: couldn't create event.")
}
eventHandlers.forEach { $0(query, event) }
}

open func didSubscribe(_ new: Bool) {
subscribeHandlers.forEach { $0(query, new) }
}

open func didUnsubscribe() {
unsubscribeHandlers.forEach { $0(query) }
}
}

0 comments on commit 1db0674

Please sign in to comment.