Skip to content

Commit

Permalink
fix (#3257)
Browse files Browse the repository at this point in the history
  • Loading branch information
urugator authored Jan 5, 2022
1 parent 40ad091 commit 654a201
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/silly-lions-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mobx": patch
---

fix: observable map initialization violates `enforceActions: "always"`
14 changes: 13 additions & 1 deletion packages/mobx/__tests__/v5/base/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import {
computed,
action,
when,
runInAction
runInAction,
configure,
_resetGlobalState
} from "../../../src/mobx.ts"

const map = mobx.observable.map
Expand Down Expand Up @@ -1335,3 +1337,13 @@ test("2346 - subscribe to not yet existing map keys", async () => {
await when(() => events.length > 1)
expect(events).toEqual([42, 84])
})

test('initialization should not violate `enforceActions: "always"` - discussion #3255', async () => {
const consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation(() => {
throw new Error("console.warn called")
})
configure({ enforceActions: "always" })
observable(new Map([["x", "x"]]))
_resetGlobalState()
consoleWarnSpy.mockRestore()
})
7 changes: 5 additions & 2 deletions packages/mobx/src/types/observablemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ import {
isFunction,
UPDATE,
IAtom,
PureSpyEvent
PureSpyEvent,
allowStateChanges
} from "../internal"

export interface IKeyValueMap<V = any> {
Expand Down Expand Up @@ -107,7 +108,9 @@ export class ObservableMap<K = any, V = any>
this.keysAtom_ = createAtom(__DEV__ ? `${this.name_}.keys()` : "ObservableMap.keys()")
this.data_ = new Map()
this.hasMap_ = new Map()
this.merge(initialData)
allowStateChanges(true, () => {
this.merge(initialData)
})
}

private has_(key: K): boolean {
Expand Down

0 comments on commit 654a201

Please sign in to comment.