Skip to content

Commit e66b1ad

Browse files
committed
bug: allow changing while observable array initialization
1 parent 4f6ec32 commit e66b1ad

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

src/types/observablearray.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { IListenable, registerListener, hasListeners, notifyListeners } from "./
2323
import { isSpyEnabled, spyReportStart, spyReportEnd } from "../core/spy"
2424
import { declareIterator, makeIterable } from "../utils/iterable"
2525
import { IEnhancer } from "./modifiers"
26+
import { allowStateChangesEnd, allowStateChangesStart } from "../core/action"
2627

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

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

337338
if (initialValues && initialValues.length) {
339+
const prev = allowStateChangesStart(true)
338340
this.spliceWithArray(0, 0, initialValues)
341+
allowStateChangesEnd(prev)
339342
}
340343

341344
if (safariPrototypeSetterInheritanceBug) {

test/base/strict-mode.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,20 @@ test("enforceActions 'strict' does not allow changing unobserved observables", (
210210
}
211211
})
212212

213+
test("enforceActions 'strict' should not throw exception while observable array initialization", () => {
214+
try {
215+
mobx.configure({ enforceActions: "strict" })
216+
217+
expect(() => {
218+
const x = mobx.observable({
219+
a: [1, 2]
220+
})
221+
}).not.toThrow(/Since strict-mode is enabled/)
222+
} finally {
223+
mobx.configure({ enforceActions: false })
224+
}
225+
})
226+
213227
test("warn on unsafe reads", function() {
214228
try {
215229
mobx.configure({ computedRequiresReaction: true })

0 commit comments

Comments
 (0)