Skip to content

Commit

Permalink
Merged #1361
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Mar 5, 2018
1 parent 4c30db4 commit 8c63aad
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ All the random notes that should make up a nice changelog:
* Dropped already deprecated toplevel `map` function
* Killed the already deprecated modifiers `asFlat` etc. If you war still using this, see the MobX 2 -> 3 migration notes.
* `autorun` now accepts a scheduler function to allow improved performance for tasks such as rendering to canvas
* Observable maps now fully implement the map interface. See [#1361](https://github.com/mobxjs/mobx/pull/1361) by [Marc Fallows](https://github.com/marcfallows)

# 3.6.1

Expand Down
12 changes: 10 additions & 2 deletions src/types/observablemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
invariant,
isES6Map,
getMapLikeKeys,
fail
fail,
addHiddenFinalProp
} from "../utils/utils"
import {
IInterceptable,
Expand Down Expand Up @@ -78,7 +79,8 @@ export class ObservableMap<K, V>
interceptors
changeListeners
dehancer: any;
[Symbol.iterator]
[Symbol.iterator];
[Symbol.toStringTag]

constructor(
initialData?: IObservableMapInitialValues<K, V>,
Expand Down Expand Up @@ -374,6 +376,12 @@ declareIterator(ObservableMap.prototype, function() {
return this.entries()
})

addHiddenFinalProp(
ObservableMap.prototype,
typeof Symbol !== "undefined" ? Symbol.toStringTag : "@@toStringTag" as any,
"Map"
)

/* 'var' fixes small-build issue */
export var isObservableMap = createInstanceofPredicate("ObservableMap", ObservableMap) as (
thing: any
Expand Down
6 changes: 6 additions & 0 deletions test/base/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -678,3 +678,9 @@ test("maps.values, keys and maps.entries are iterables", () => {
expect(Array.from(x.values())).toEqual([1, 2])
expect(Array.from(x.keys())).toEqual(["x", "y"])
})

test("toStringTag", () => {
const x = mobx.observable.map({ x: 1, y: 2 })
expect(x[Symbol.toStringTag]).toBe("Map")
expect(Object.prototype.toString.call(x)).toBe("[object Map]")
})
6 changes: 6 additions & 0 deletions test/base/typescript-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1499,3 +1499,9 @@ test.skip("actions are reassignable", () => {
a.m2 = () => {}
expect(isAction(a.m2)).toBe(true)
})

test("map should structurally match ES6 Map", () => {
// Including this line strictly for type checking.
const m: Map<string, number> = mobx.observable.map({ a: 1, b: 2 })
expect(true).toBe(true)
})

0 comments on commit 8c63aad

Please sign in to comment.