replaceRevive: Use switch/case for what's interesting in replacer.#4362
Merged
Conversation
gnprice
reviewed
Dec 29, 2020
Member
gnprice
left a comment
There was a problem hiding this comment.
Thanks @chrisbobbe ! All LGTM and ready to merge, modulo a small comment below.
| data: FallbackAvatarURL.serialize(value), | ||
| [SERIALIZED_TYPE_FIELD_NAME]: 'FallbackAvatarURL', | ||
| }; | ||
| case (Immutable.Map.prototype: $FlowFixMe): |
Member
There was a problem hiding this comment.
This bit isn't NFC because instanceof Immutable.Map would accept an Immutable.OrderedMap (the one subclass Immutable has for that class), and this won't.
... Which is a fix to a latent bug! We don't currently use OrderedMap, but if we did, the old code would cheerfully serialize it, and then deserialize it as a plain Immutable.Map.
Collaborator
Author
There was a problem hiding this comment.
Ah, right, good catch—I'll make a note in the commit message and remove the [nfc] mark.
chrisbobbe
added a commit
to chrisbobbe/zulip-mobile
that referenced
this pull request
Dec 29, 2020
As Greg suggests [1]. It isn't quite NFC, as Greg points out [2]. It fixes a latent bug: before this, if we had used an `Immutable.OrderedMap` anywhere, the replacer would have cheerfully serialized it with the `ImmutableMap` identifier, and it would get deserialized as a plain `Immutable.Map` instead of an `Immutable.OrderedMap`. [1] zulip#4348 (comment) [2] zulip#4362 (comment)
7be177a to
dbc002d
Compare
Collaborator
Author
|
Thanks for the review! I just pushed a new revision with that commit-message change. |
replacer.replacer.
Greg reports that this is equivalent [1], and it seems to work just fine. [1] zulip#4348 (comment)
…aces. We know that these classes don't have `.toJSON` methods -- after all, we wrote them -- but this brings us closer to refactoring this logic into a simpler switch/case, which we'll do soon.
One of the `invariant`s just above this code ensures that `value` and `origValue` will be identical (===). (The only reason they would ever differ is that `value` is the result of calling `origValue`'s `toJSON` method, if present. That invariant would throw if `toJSON` were present.) So, we can choose between them. Might as well use `origValue`. That way, most of the code in this function uses `origValue`, which clears the way for a nice upcoming refactor where we use a switch/case instead of lots of `if` / `else if`s. It also helps isolate `value` as something that's useful mainly for saving computation in a particular way, as we do in the `Immutable.Map` case (see bf8837c for more on that).
This won't stay here; soon, we'll make it an even earlier return. But we'll do so in separate commits, so we have room to explain more.
This is NFC because there's no loss to the set of possible errors the `invariant`s were catching. In particular, the check we're bringing upwards neatly replaces the check that establishes whether the `invariant`s ran (or indeed were possible to express).
Not totally NFC because of the potential for a notable performance gain (we now avoid doing some unnecessary checks). This will allow us to refactor our many `if` / `else if` cases into a simpler switch/case on `Object.getPrototypeOf(origValue)`, by letting us call `Object.getPrototypeOf(origValue)` in the first place. (It fails on `null` and `undefined`.) This is NFC because `origValue` being non-`null` and having `typeof` 'object' is a necessary condition for any of the `if` / `else if` conditions to be true.
As Greg suggests [1]. It isn't quite NFC, as Greg points out [2]. It fixes a latent bug: before this, if we had used an `Immutable.OrderedMap` anywhere, the replacer would have cheerfully serialized it with the `ImmutableMap` identifier, and it would get deserialized as a plain `Immutable.Map` instead of an `Immutable.OrderedMap`. [1] zulip#4348 (comment) [2] zulip#4362 (comment)
dbc002d to
05048c7
Compare
Member
|
Thanks -- merged! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
As Greg suggests, at
#4348 (comment).
Also add some comments / jsdoc.