Skip to content

Commit

Permalink
redux: Scaffolding for custom replace/revive logic.
Browse files Browse the repository at this point in the history
This sets up how we'd like to plug in custom replacers and revivers,
but doesn't add any yet.

We can use the '__serializedType__' string freely because we're
using `zulip/remotedev-serialize@5f9f759a4`, which contains a fix
for proper round-tripping of that string, to avoid collisions when
that string occurs as a key representing data, and the unplanned
behavior such collisions would cause.
  • Loading branch information
Chris Bobbe committed May 5, 2020
1 parent 6ed2ee8 commit e834f33
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/boot/store.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* @flow strict-local */
import { applyMiddleware, compose, createStore } from 'redux';
import type { Store } from 'redux';
import Immutable from 'immutable';
import * as Serialize from 'remotedev-serialize';
import { persistStore, autoRehydrate } from '../third/redux-persist';
import type { Config } from '../third/redux-persist';

Expand Down Expand Up @@ -152,10 +154,38 @@ const migrations: { [string]: (GlobalState) => GlobalState } = {
}),
};

/**
* A special identifier used by `remotedev-serialize`.
*
* Use this in the custom replacer and reviver, below, to make it
* easier to be consistent between them and avoid costly typos.
*/
const SERIALIZED_TYPE_FIELD_NAME: '__serializedType__' = '__serializedType__';

const customReplacer = (key, value, defaultReplacer) =>
// Scaffolding for the next commit, where we replace/revive ZulipVersion
defaultReplacer(key, value);

const customReviver = (key, value, defaultReviver) => {
if (value !== null && typeof value === 'object' && SERIALIZED_TYPE_FIELD_NAME in value) {
// Scaffolding for the next commit, where we replace/revive ZulipVersion
const data = value.data; // eslint-disable-line no-unused-vars
switch (value[SERIALIZED_TYPE_FIELD_NAME]) {
default:
// Fall back to defaultReviver, below
}
}
return defaultReviver(key, value);
};

const { stringify, parse } = Serialize.immutable(Immutable, null, customReplacer, customReviver);

const reduxPersistConfig: Config = {
whitelist: [...storeKeys, ...cacheKeys],
// $FlowFixMe: https://github.com/rt2zz/redux-persist/issues/823
storage: ZulipAsyncStorage,
serialize: stringify,
deserialize: parse,
};

const store: Store<GlobalState, Action> = createStore(
Expand Down

0 comments on commit e834f33

Please sign in to comment.