Skip to content

Commit

Permalink
replaceRevive [nfc]: Add some comments and JSDoc.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbobbe authored and gnprice committed Dec 29, 2020
1 parent 3d600c5 commit 05048c7
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/boot/replaceRevive.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ export const SERIALIZED_TYPE_FIELD_NAME: '__serializedType__' = '__serializedTyp
*/
const SERIALIZED_TYPE_FIELD_NAME_ESCAPED: '__serializedType__value' = '__serializedType__value';

/**
* Custom replacer for inventive data types JSON doesn't handle.
*
* To be passed to `JSON.stringify` as its second argument. New
* replacement logic must also appear in `reviver` so they stay in
* sync.
*/
// Don't make this an arrow function -- we need `this` to be a special
// value; see
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter.
Expand All @@ -43,6 +50,8 @@ const replacer = function replacer(key, value) {
const origValue = this[key];

if (typeof origValue !== 'object' || origValue === null) {
// `origValue` can't be one of our interesting data types, so,
// just return it.
return origValue;
}

Expand Down Expand Up @@ -87,6 +96,8 @@ const replacer = function replacer(key, value) {
'unexpected class',
);

// Ensure that objects with a [SERIALIZED_TYPE_FIELD_NAME] property
// round-trip.
if (SERIALIZED_TYPE_FIELD_NAME in origValue) {
const copy = { ...origValue };
delete copy[SERIALIZED_TYPE_FIELD_NAME];
Expand All @@ -100,6 +111,13 @@ const replacer = function replacer(key, value) {
return origValue;
};

/**
* Custom reviver for inventive data types JSON doesn't handle.
*
* To be passed to `JSON.parse` as its second argument. New
* reviving logic must also appear in `replacer` so they stay in
* sync.
*/
const reviver = function reviver(key, value) {
if (value !== null && typeof value === 'object' && SERIALIZED_TYPE_FIELD_NAME in value) {
const data = value.data;
Expand Down

0 comments on commit 05048c7

Please sign in to comment.