Skip to content

Commit

Permalink
Computed triggering the creation of an observable map invariant failed
Browse files Browse the repository at this point in the history
…#798

Altered the initial map.merge to really be untracked.
  • Loading branch information
mattruby committed Jan 25, 2017
1 parent 629110f commit b26f34d
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions src/types/observablemap.ts
Expand Up @@ -65,7 +65,7 @@ export class ObservableMap<V> implements IInterceptable<IMapWillChange<V>>, ILis

constructor(initialData?: IObservableMapInitialValues<V>, public enhancer: IEnhancer<V> = deepEnhancer, public name = "ObservableMap@" + getNextId()) {
allowStateChanges(true, () => {
this.merge(initialData);
this.untrackedMerge(initialData);
});
}

Expand Down Expand Up @@ -233,14 +233,7 @@ export class ObservableMap<V> implements IInterceptable<IMapWillChange<V>>, ILis
other = other.toJS();
}
runInTransaction(() => {
if (isPlainObject(other))
Object.keys(other).forEach(key => this.set(key, other[key]));
else if (Array.isArray(other))
other.forEach(([key, value]) => this.set(key, value));
else if (isES6Map(other))
other.forEach((value, key) => this.set(key, value));
else if (other !== null && other !== undefined)
fail("Cannot initialize map from " + other);
this.mergeDataByType(other);
});
return this;
}
Expand Down Expand Up @@ -293,6 +286,28 @@ export class ObservableMap<V> implements IInterceptable<IMapWillChange<V>>, ILis
throw new Error(`[mobx.map] Invalid key: '${key}', only strings, numbers and booleans are accepted as key in observable maps.`);
}

private mergeDataByType(other: ObservableMap<V> | IKeyValueMap<V> | any): ObservableMap<V> {
untracked(() => {
if (isPlainObject(other))
Object.keys(other).forEach(key => this.set(key, other[key]));
else if (Array.isArray(other))
other.forEach(([key, value]) => this.set(key, value));
else if (isES6Map(other))
other.forEach((value, key) => this.set(key, value));
else if (other !== null && other !== undefined)
fail("Cannot initialize map from " + other);
});
return this;
}

private untrackedMerge(other: ObservableMap<V> | IKeyValueMap<V> | any): ObservableMap<V> {
if (isObservableMap(other)) {
other = other.toJS();
}
this.mergeDataByType(other);
return this;
}

toString(): string {
return this.name + "[{ " + this.keys().map(key => `${key}: ${"" + this.get(key)}`).join(", ") + " }]";
}
Expand Down

0 comments on commit b26f34d

Please sign in to comment.