ably-js icon indicating copy to clipboard operation
ably-js copied to clipboard

Refactor: use explicit preserialization methods rather than overriding toJSON()

Open mattheworiordan opened this issue 9 years ago • 12 comments
trafficstars

See https://jsbin.ably.io/ebemoj/4/edit

Why are the members returned in the get method missing all the attributes of Presence member such as connectionId. This is sorely needed for anyone trying to work out if they are present or not upon reconnection.

┆Issue is synchronized with this Jira Task by Unito

mattheworiordan avatar Jul 12 '16 23:07 mattheworiordan

@mattheworiordan they're not, they're just hidden from the default toJSON(), see See https://jsbin.ably.io/ebemoj/6/ that uses toString() instead. See past discussion at https://github.com/ably/ably-js/issues/44

SimonWoolf avatar Jul 13 '16 11:07 SimonWoolf

(FWIW I wouldn't be against making internal library use use a custom method rather than toJSON, so end-users' JSON.stringify() calls aren't affected, but Paddy prefers this way)

SimonWoolf avatar Jul 13 '16 12:07 SimonWoolf

The only reason for having a toJSON() is because that's how you control what gets packed when sending on a connection (both with JSON and msgpack transports). Since we have custom handling for data (ie base64-encoding) then we need to do this. I suspect the current toJSON() methods omit those fields because they're not required to be sent but I'm fine with adding them.

paddybyers avatar Jul 13 '16 12:07 paddybyers

that's how you control what gets packed when sending on a connection (both with JSON and msgpack transports). Since we have custom handling for data (ie base64-encoding) then we need to do this.

Do we really, though? Why can't we just do it explicitly, rather than overriding toJSON? Surely what fields we need to send to Ably shouldn't have to be linked to what the users sees with a stringify?

Code speaks louder than words, so quick-and-dirty suggestion: https://github.com/ably/ably-js/compare/prepareforsending?expand=1

(I know you said you're fine with re-adding them, but a year ago I was asked to remove them, and don't really want to get into an infinite loop :smile: )

SimonWoolf avatar Jul 13 '16 12:07 SimonWoolf

Of course the reason for using the toJSON() hack was to avoid having to write code not only for the types in question but also each container type that might embed them.

paddybyers avatar Jul 13 '16 13:07 paddybyers

I like your change @SimonWoolf, overload toJSON is a bit hacky, it's the kind of thing I'd expect in my code not that of @paddybyers :)

BTW. I don't like the name prepareForSending given it's not preparing but instead bundling and generating a new payload, so perhaps packageForSending or toAblyPayload() etc?

mattheworiordan avatar Jul 13 '16 14:07 mattheworiordan

TBH I don't feel very strongly about this; if Paddy believes there's value in the toJSON way, I'm ok with that. I'll revert https://github.com/ably/ably-js/pull/117/commits/c7081323613c2242c64c2024dccdbfbbf4dcbbe4

SimonWoolf avatar Jul 18 '16 21:07 SimonWoolf

@paddybyers WDYT?

mattheworiordan avatar Jul 18 '16 21:07 mattheworiordan

I agree toJSON(), whilst theoretically the most efficient, comes with some baggage and constraints, and it only just works because of the hack to detect whether it was invoked via JSON.stringify() or msgpack.

So I'm not opposed to changing it.

As for naming, in realtime for the same functionality we have pack() and unpack(), which would be fine, or serialise()/deserialise(), transportEncode() etc.

paddybyers avatar Jul 18 '16 21:07 paddybyers

Is this covered in 0.9 now @SimonWoolf?

mattheworiordan avatar Nov 16 '16 12:11 mattheworiordan

Nope, as of now it's still using the toJSON hack. (There was some issue with the prepareforsending approach, can't remember what now, that made it not mergeable as-is, then other things took priority). Would still be nice to refactor that at some point, so leaving this issue open. Don't think it's high priority though

SimonWoolf avatar Nov 16 '16 17:11 SimonWoolf

Don't think it's high priority though

Ok, thanks for clarifying

mattheworiordan avatar Nov 16 '16 19:11 mattheworiordan