@@ -56,10 +56,85 @@ private func == <T>(lhs: Event<T>, rhs: Event<T>) -> Bool {
5656 }
5757}
5858
59+ #if !os(Linux)
5960/**
60- A default implementation of the `ParseSubscription` protocol, using closures for callbacks.
61+ A default implementation of the `ParseSubscription` protocol. Suitable for `ObjectObserved`
62+ as the subscription can be used as a SwiftUI publisher. Meaning it can serve
63+ indepedently as a ViewModel in MVVM.
6164 */
62- open class Subscription < T: ParseObject > : ParseSubscription {
65+ @available ( macOS 10 . 15 , iOS 13 . 0 , watchOS 6 . 0 , tvOS 13 . 0 , * )
66+ open class Subscription < T: ParseObject > : ParseSubscription , ObservableObject {
67+ //The query subscribed to.
68+ public var query : Query < T >
69+ //The ParseObject
70+ public typealias Object = T
71+
72+ /// Notifies there's a new event related to a specific query.
73+ private ( set) var event : ( query: Query < T > , event: Event < T > ) ? {
74+ willSet {
75+ if newValue != nil {
76+ subscribed = nil
77+ unsubscribed = nil
78+ objectWillChange. send ( )
79+ }
80+ }
81+ }
82+
83+ /// Notifies when a subscription request has been fulfilled and if it is new.
84+ private ( set) var subscribed : ( query: Query < T > , isNew: Bool ) ? {
85+ willSet {
86+ if newValue != nil {
87+ unsubscribed = nil
88+ event = nil
89+ objectWillChange. send ( )
90+ }
91+ }
92+ }
93+
94+ /// Notifies when an unsubscribe request has been fulfilled.
95+ private ( set) var unsubscribed : Query < T > ? {
96+ willSet {
97+ if newValue != nil {
98+ subscribed = nil
99+ event = nil
100+ objectWillChange. send ( )
101+ }
102+ }
103+ }
104+
105+ /**
106+ Creates a new subscription that can be used to handle updates.
107+ */
108+ public init ( query: Query < T > ) {
109+ self . query = query
110+ self . subscribed = nil
111+ self . event = nil
112+ self . unsubscribed = nil
113+ }
114+
115+ open func didReceive( _ eventData: Data ) throws {
116+ // Need to decode the event with respect to the `ParseObject`.
117+ let eventMessage = try ParseCoding . jsonDecoder ( ) . decode ( EventResponse< T> . self , from: eventData)
118+ guard let event = Event ( event: eventMessage) else {
119+ throw ParseError ( code: . unknownError, message: " ParseLiveQuery Error: couldn't create event. " )
120+ }
121+ self . event = ( query, event)
122+ }
123+
124+ open func didSubscribe( _ new: Bool ) {
125+ self . subscribed = ( query, new)
126+ }
127+
128+ open func didUnsubscribe( ) {
129+ self . unsubscribed = query
130+ }
131+ }
132+ #endif
133+
134+ /**
135+ A default implementation of the `ParseSubscription` protocol using closures for callbacks.
136+ */
137+ open class SubscriptionCallback < T: ParseObject > : ParseSubscription {
63138 //The query subscribed to.
64139 public var query : Query < T >
65140 //The ParseObject
@@ -80,7 +155,8 @@ open class Subscription<T: ParseObject>: ParseSubscription {
80155 - parameter handler: The callback to register.
81156 - returns: The same subscription, for easy chaining.
82157 */
83- @discardableResult open func handleEvent( _ handler: @escaping ( Query < T > , Event < T > ) -> Void ) -> Subscription {
158+ @discardableResult open func handleEvent( _ handler: @escaping ( Query < T > ,
159+ Event < T > ) -> Void ) -> SubscriptionCallback {
84160 eventHandlers. append ( handler)
85161 return self
86162 }
@@ -90,7 +166,8 @@ open class Subscription<T: ParseObject>: ParseSubscription {
90166 - parameter handler: The callback to register.
91167 - returns: The same subscription, for easy chaining.
92168 */
93- @discardableResult open func handleSubscribe( _ handler: @escaping ( Query < T > , Bool ) -> Void ) -> Subscription {
169+ @discardableResult open func handleSubscribe( _ handler: @escaping ( Query < T > ,
170+ Bool ) -> Void ) -> SubscriptionCallback {
94171 subscribeHandlers. append ( handler)
95172 return self
96173 }
@@ -100,7 +177,7 @@ open class Subscription<T: ParseObject>: ParseSubscription {
100177 - parameter handler: The callback to register.
101178 - returns: The same subscription, for easy chaining.
102179 */
103- @discardableResult open func handleUnsubscribe( _ handler: @escaping ( Query < T > ) -> Void ) -> Subscription {
180+ @discardableResult open func handleUnsubscribe( _ handler: @escaping ( Query < T > ) -> Void ) -> SubscriptionCallback {
104181 unsubscribeHandlers. append ( handler)
105182 return self
106183 }
@@ -123,7 +200,7 @@ open class Subscription<T: ParseObject>: ParseSubscription {
123200 }
124201}
125202
126- extension Subscription {
203+ extension SubscriptionCallback {
127204
128205 /**
129206 Register a callback for when an event occurs of a specific type
@@ -136,7 +213,7 @@ extension Subscription {
136213 - returns: The same subscription, for easy chaining.
137214 */
138215 @discardableResult public func handle( _ eventType: @escaping ( T ) -> Event < T > ,
139- _ handler: @escaping ( Query < T > , T ) -> Void ) -> Subscription {
216+ _ handler: @escaping ( Query < T > , T ) -> Void ) -> SubscriptionCallback {
140217 return handleEvent { query, event in
141218 switch event {
142219 case . entered( let obj) where eventType ( obj) == event: handler ( query, obj)
0 commit comments