-
Notifications
You must be signed in to change notification settings - Fork 34
Updating for server compatibility with fixes for reply network messages #11
Changes from 1 commit
25ccae9
8ad759e
00e8e7e
78d3ab7
a544793
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,12 +17,16 @@ class ScopeFetchReplyMessage: ReplyMessage { | |
return ScopeFetchReplyMessage.messageType | ||
} | ||
|
||
let success: Bool | ||
let scopeIndex: UInt? | ||
let error: NSError? | ||
let response: AnyObject? | ||
|
||
init(index: UInt, replyTo: UInt, scopeIndex: UInt?, error: NSError? = nil) { | ||
init(index: UInt, replyTo: UInt, success: Bool, scopeIndex: UInt?, error: NSError?, response: AnyObject?) { | ||
self.success = success | ||
self.scopeIndex = scopeIndex | ||
self.error = error | ||
self.response = response | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you need a response? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So people can do stuff like session.on('fetch', function(name, params, callback) {
// Verify fetching the scope
if (name !== scope.name) {
return callback(new Error('No such scope'), {try: ["ShapesDemo"]});
}
callback(null, scope);
}); And pass application specific responses for failed and/or success messages There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmm I'm gonna remove this |
||
super.init(index: index, replyTo: replyTo) | ||
} | ||
|
||
|
@@ -33,16 +37,26 @@ class ScopeFetchReplyMessage: ReplyMessage { | |
override class func unserialize(dictionary: [String: AnyObject]) -> NetworkMessage? { | ||
var index = dictionary["index"] as? UInt | ||
var replyTo = dictionary["replyTo"] as? UInt | ||
var success = dictionary["success"] as? Bool | ||
var scopeIndex = dictionary["scopeIndex"] as? UInt | ||
|
||
var error: NSError? | ||
if let serializedError = dictionary["error"] as? [String: AnyObject] { | ||
error = errorFromDictionary(.SyncFragmentApplyError, serializedError) | ||
} | ||
|
||
if index == nil || replyTo == nil || (scopeIndex == nil && error == nil) { | ||
var response: AnyObject? = dictionary["response"] | ||
|
||
if index == nil || replyTo == nil || success == nil || (scopeIndex == nil && error == nil) { | ||
return nil | ||
} else { | ||
return ScopeFetchReplyMessage(index: index!, replyTo: replyTo!, scopeIndex: scopeIndex, error: error) | ||
return ScopeFetchReplyMessage( | ||
index: index!, | ||
replyTo: replyTo!, | ||
success: success!, | ||
scopeIndex: scopeIndex, | ||
error: error, | ||
response: response) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,7 +92,7 @@ public class Session { | |
switch message { | ||
case let scopeStateMessage as ScopeStateMessage: | ||
if let scope = scopes[scopeStateMessage.scopeIndex] { | ||
if let rootModel = scope.rootModel { | ||
if let definiteRootModelObject = scope.root { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd keep this as "let rootModel = ..." (or rootModelObject). The convention seems to call for using the definite prefix when you're making a local optional and actual variable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Btw, if we're not accessing rootModel after this, why don't we just do "if scope.root != nil"? |
||
scope.startApplyingRemote() | ||
scope.applyRootFragment(scopeStateMessage.rootFragment, additionalFragments: scopeStateMessage.syncFragments) | ||
scope.endApplyingRemote() | ||
|
@@ -104,7 +104,7 @@ public class Session { | |
} | ||
case let scopeSyncMessage as ScopeSyncMessage: | ||
if let scope = scopes[scopeSyncMessage.scopeIndex] { | ||
if let rootModel = scope.rootModel { | ||
if let definiteRootModelObject = scope.root { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, let's keep it as rootModel |
||
if scopeSyncMessage.syncFragments.count > 0 { | ||
scope.startApplyingRemote() | ||
scope.applySyncFragments(scopeSyncMessage.syncFragments) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -235,15 +235,15 @@ public class SyncFragment: Equatable { | |
} | ||
|
||
func applyChangesToScope(scope: Scope, applyDefaults: Bool = false) { | ||
if (scope.rootModel == nil) { | ||
if scope.root == nil { | ||
return | ||
} | ||
|
||
switch type { | ||
case .Root: | ||
if let definiteRootModel = scope.rootModel { | ||
applyPropertiesToModelObject(definiteRootModel, scope: scope, applyDefaults: applyDefaults) | ||
scope.updateUUIDForModel(definiteRootModel, uuid: self.objectUUID) | ||
if let definiteRootModelObject = scope.root { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we rename this, too? |
||
applyPropertiesToModelObject(definiteRootModelObject, scope: scope, applyDefaults: applyDefaults) | ||
scope.updateUUIDForModel(definiteRootModelObject, uuid: self.objectUUID) | ||
} | ||
case .Change: | ||
if let modelObject = scope.getObjectById(objectUUID) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ import UIKit | |
import Jetstream | ||
|
||
class ShapesDemoViewController: UIViewController, NSURLConnectionDataDelegate { | ||
var scope = Scope(name: "ShapesDemo") | ||
var scope = Scope(name: "Canvas") | ||
var canvas = Canvas() | ||
|
||
var client: Client? | ||
|
@@ -23,8 +23,8 @@ class ShapesDemoViewController: UIViewController, NSURLConnectionDataDelegate { | |
|
||
let tapRecognizer = UITapGestureRecognizer(target: self, action: Selector("handleTap:")) | ||
self.view.addGestureRecognizer(tapRecognizer) | ||
canvas.setScopeAndMakeRootModel(scope) | ||
|
||
scope.root = canvas | ||
canvas.observeCollectionAdd(self, key: "shapes") { (element: Shape) in | ||
let shapeView = ShapeView(shape: element) | ||
self.view.addSubview(shapeView) | ||
|
@@ -47,15 +47,20 @@ class ShapesDemoViewController: UIViewController, NSURLConnectionDataDelegate { | |
let transportAdapter = WebsocketTransportAdapter(options: WebsocketConnectionOptions(url: NSURL(string: "ws://" + host + ":3000")!)) | ||
client = Client(transportAdapter: transportAdapter) | ||
client?.connect() | ||
client?.onSession.listenOnce(self) { (session) in | ||
self.session = session | ||
let scope = self.scope | ||
session.fetch(scope) { (error) in | ||
if error != nil { | ||
self.alertError("Error fetching scope", message: "\(error)") | ||
} else { | ||
self.hideLoader() | ||
} | ||
client?.onSession.listenOnce(self) { [weak self] session in | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can do a unowned self here (and then you don't need to do the let definite dance). Signals guarantee that the listener is still alive when it fires. |
||
if let definiteSelf = self { | ||
definiteSelf.sessionDidStart(session) | ||
} | ||
} | ||
} | ||
|
||
func sessionDidStart(session: Session) { | ||
self.session = session | ||
session.fetch(scope) { (error) in | ||
if error != nil { | ||
self.alertError("Error fetching scope", message: "\(error)") | ||
} else { | ||
self.hideLoader() | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I was thinking that the absence of an error means that it was successful (wiki also updated with this)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it's just weird that SessionCreateReplyMessage has success and this does not
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Acording to the Wiki it doesn't;) Maybe remove to success flag from SessionCreateReplyMessage, too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure