Skip to content
This repository was archived by the owner on Mar 27, 2018. It is now read-only.

Commit e9d695f

Browse files
author
Ian Wensink
committed
fix(entity-mapper): refactor entityComponents state
Before, the components were saved in a Map with key-value storage with the key being a Symbol for the component. This caused problems when two completely different components were the same at top level (e.g. when using a HOC). Now, the components are saved in an array which is searched wand matched on uuid & mapper (same logic as hnContext)
1 parent 41470ac commit e9d695f

File tree

1 file changed

+31
-30
lines changed

1 file changed

+31
-30
lines changed

src/components/EntityMapper.js

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import getNested from 'get-nested';
44
import site from '../utils/site';
55

66
class EntityMapper extends Component {
7-
static entityComponents = new Map();
7+
static entityComponents = [];
88

99
static contextTypes = {
1010
hnContext: PropTypes.object,
@@ -15,7 +15,7 @@ class EntityMapper extends Component {
1515
* @param uuid
1616
* @param mapper
1717
* @param asyncMapper
18-
* @returns Symbol
18+
* @returns void
1919
*/
2020
static async assureComponent({ uuid, mapper, asyncMapper }) {
2121
// This gets the entity from the site, based on the uuid.
@@ -39,20 +39,19 @@ class EntityMapper extends Component {
3939
}
4040

4141
// Make sure there is an entityComponent.
42-
if (!entityComponent) {
43-
return null;
44-
}
42+
if (!entityComponent) return;
4543

4644
// If it has a .default (ES6+), use that.
4745
if (entityComponent.default) {
4846
entityComponent = entityComponent.default;
4947
}
5048

5149
// Store the entityComponent globally, so it can be rendered sync.
52-
const entityComponentSymbol = Symbol.for(entityComponent);
53-
EntityMapper.entityComponents.set(entityComponentSymbol, entityComponent);
54-
55-
return entityComponentSymbol;
50+
EntityMapper.entityComponents.push({
51+
uuid,
52+
mapper,
53+
component: entityComponent,
54+
});
5655
}
5756

5857
static getBundle = entity =>
@@ -65,7 +64,6 @@ class EntityMapper extends Component {
6564
super(props);
6665

6766
this.state = {
68-
entityComponentSymbol: null,
6967
entityProps: props.entityProps,
7068
ready: false,
7169
uuid: props.uuid,
@@ -82,7 +80,7 @@ class EntityMapper extends Component {
8280
const { mapper, asyncMapper } = this.props;
8381
const { uuid, entityProps } = this.state;
8482
this.context.hnContext.state.entities.push({
85-
component: await this.loadComponent({
83+
componentState: await this.loadComponent({
8684
asyncMapper,
8785
entityProps,
8886
mapper,
@@ -103,7 +101,7 @@ class EntityMapper extends Component {
103101
() =>
104102
this.context.hnContext.state.entities.find(
105103
e => e.mapper === mapper && e.uuid === uuid,
106-
).component,
104+
).componentState,
107105
);
108106

109107
if (state) {
@@ -124,17 +122,25 @@ class EntityMapper extends Component {
124122
}
125123

126124
async loadComponent({ uuid, mapper, asyncMapper, entityProps }) {
127-
this.setState({ ready: false });
128-
const entityComponentSymbol = await EntityMapper.assureComponent({
129-
asyncMapper,
130-
mapper,
131-
uuid,
132-
});
125+
// Check if component for combination of mapper + uuid already was loaded
126+
const entityComponent = EntityMapper.entityComponents.find(c => c.mapper === mapper && c.uuid === uuid);
127+
128+
// If component isn't loaded yet, go load it
129+
if(!entityComponent) {
130+
this.setState({ ready: false });
131+
132+
await EntityMapper.assureComponent({
133+
asyncMapper,
134+
mapper,
135+
uuid,
136+
});
137+
}
133138

134139
const newState = {
135140
...this.state,
136-
...{ uuid, entityComponentSymbol, ready: true, entityProps },
141+
...{ uuid, ready: true, entityProps },
137142
};
143+
138144
this.setState(newState);
139145

140146
return newState;
@@ -145,22 +151,17 @@ class EntityMapper extends Component {
145151
}
146152

147153
render() {
148-
const { index } = this.props;
149-
const { uuid, entityComponentSymbol, entityProps } = this.state;
154+
const { index, mapper } = this.props;
155+
const { uuid, entityProps } = this.state;
150156

151157
const entity = site.getData(uuid);
152158

153-
if (!entity) {
154-
return null;
155-
}
159+
if (!entity) return null;
156160

157-
const EntityComponent = EntityMapper.entityComponents.get(
158-
entityComponentSymbol,
159-
);
161+
const EntityComponent = getNested(() =>
162+
EntityMapper.entityComponents.find(c => c.uuid === uuid && c.mapper === mapper).component);
160163

161-
if (!EntityComponent) {
162-
return null;
163-
}
164+
if (!EntityComponent) return null;
164165

165166
return (
166167
<EntityComponent

0 commit comments

Comments
 (0)