Skip to content

Commit

Permalink
Merge pull request #1573 from kuitos/master
Browse files Browse the repository at this point in the history
bugfix: allow changing while observable array initialization
  • Loading branch information
mweststrate authored Jun 5, 2018
2 parents 4f6ec32 + e66b1ad commit 405a025
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/types/observablearray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { IListenable, registerListener, hasListeners, notifyListeners } from "./
import { isSpyEnabled, spyReportStart, spyReportEnd } from "../core/spy"
import { declareIterator, makeIterable } from "../utils/iterable"
import { IEnhancer } from "./modifiers"
import { allowStateChangesEnd, allowStateChangesStart } from "../core/action"

const MAX_SPLICE_SIZE = 10000 // See e.g. https://github.com/mobxjs/mobx/issues/859

Expand Down Expand Up @@ -335,7 +336,9 @@ export class ObservableArray<T> extends StubArray {
addHiddenFinalProp(this, "$mobx", adm)

if (initialValues && initialValues.length) {
const prev = allowStateChangesStart(true)
this.spliceWithArray(0, 0, initialValues)
allowStateChangesEnd(prev)
}

if (safariPrototypeSetterInheritanceBug) {
Expand Down
14 changes: 14 additions & 0 deletions test/base/strict-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,20 @@ test("enforceActions 'strict' does not allow changing unobserved observables", (
}
})

test("enforceActions 'strict' should not throw exception while observable array initialization", () => {
try {
mobx.configure({ enforceActions: "strict" })

expect(() => {
const x = mobx.observable({
a: [1, 2]
})
}).not.toThrow(/Since strict-mode is enabled/)
} finally {
mobx.configure({ enforceActions: false })
}
})

test("warn on unsafe reads", function() {
try {
mobx.configure({ computedRequiresReaction: true })
Expand Down

0 comments on commit 405a025

Please sign in to comment.