Skip to content
This repository has been archived by the owner on Dec 15, 2018. It is now read-only.

Commit

Permalink
Conditionally import immutable and use either isImmutable or isIterable
Browse files Browse the repository at this point in the history
  • Loading branch information
stefvhuynh authored and tptee committed Dec 13, 2017
1 parent e0fe9c9 commit dee80a7
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion demo/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ app.get('/*', (req, res) => {
const initialState = {};
const initialStateJSON = encode(JSON.stringify(initialState));
// const initialState = Map();
// const initialStateJSON = JSON.stringify(initialState.toJSON()),
// const initialStateJSON = JSON.stringify(initialState.toJSON());

const router = routerForExpress({ routes, request: req });
const store = createStore(
Expand Down
4 changes: 2 additions & 2 deletions src/immutable/components/props-to-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import type { Component } from 'react';

import React from 'react';
import { Iterable } from 'immutable';
import { isImmutable } from '../util/immutable';

type Props = {
[key: string]: any
Expand All @@ -12,7 +12,7 @@ export default (WrappedComponent: Class<Component<*, *, *>>) =>
(wrappedProps: Props) => {
const propsJS = Object.keys(wrappedProps).reduce((props, key) => ({
...props,
[key]: Iterable.isIterable(wrappedProps[key]) ? wrappedProps[key].toJS() : wrappedProps[key]
[key]: isImmutable(wrappedProps[key]) ? wrappedProps[key].toJS() : wrappedProps[key]
}), {});

return (<WrappedComponent {...propsJS} />);
Expand Down
2 changes: 1 addition & 1 deletion src/immutable/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Map as MapType } from 'immutable';
import type { ReducerArgs } from '../reducer';
import type { Location, LocationAction } from '../types';

import { List, Map, fromJS } from 'immutable';
import { List, Map, fromJS } from './util/immutable';

import {
LOCATION_CHANGED,
Expand Down
31 changes: 31 additions & 0 deletions src/immutable/util/immutable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// @flow
/* eslint-disable import/no-mutable-exports, no-empty */
const throwError = (...args) => {
throw new Error(
`immutable.js was not imported. Make sure you have it installed. Was called with ${args}.`
);
};

let immutable;
let Map = throwError;
let List = throwError;
let fromJS = throwError;
let isImmutable = throwError;

try {
immutable = require('immutable');
Map = immutable.Map;
List = immutable.List;
fromJS = immutable.fromJS;
// To account for immutable versions 3.8.x -> 4.x.x
isImmutable = immutable.isImmutable ? immutable.isImmutable : immutable.Iterable.isIterable;
} catch (e) {}

export {
Map,
List,
fromJS,
isImmutable
};

export default immutable;
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import routerForExpress from './environment/express-router';
import routerForHapi from './environment/hapi-router';

import immutableRouterForBrowser from './immutable/environment/browser-router';
import immutableRouterForHash from './immutable/environment/hash-router';
import immutableRouterForExpress from './immutable/environment/express-router';
import immutableRouterForHash from './immutable/environment/hash-router';
import immutableRouterForHapi from './immutable/environment/hapi-router';

import { Link, PersistentQueryLink } from './components/link';
Expand Down

0 comments on commit dee80a7

Please sign in to comment.