Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
urugator committed Oct 28, 2023
1 parent aac2142 commit f55cdb0
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 70 deletions.
2 changes: 1 addition & 1 deletion packages/mobx-react/__tests__/observer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,7 @@ test(`Component react's to observable changes in componenDidMount #3691`, () =>
expect(container).toHaveTextContent("1")
unmount()
})
// TODO

test(`Observable changes in componenWillUnmount don't cause any warnings or errors`, () => {
const consoleErrorSpy = jest.spyOn(console, "error").mockImplementation(() => {})
const consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation(() => {})
Expand Down
6 changes: 3 additions & 3 deletions packages/mobx/__tests__/v4/base/extras.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ test("spy 1", function () {
lines.push(line)
})

a.set(4)
m._allowStateChanges(true, () => a.set(4))
stop()
a.set(5)
m._allowStateChanges(true, () => a.set(5))
expect(stripTrackerOutput(lines)).toMatchSnapshot()
})

Expand Down Expand Up @@ -277,7 +277,7 @@ test("onBecome(Un)Observed simple", () => {
expect(events.length).toBe(0) // nothing happened yet
x.get()
expect(events.length).toBe(0) // nothing happened yet
x.set(4)
m._allowStateChanges(true, () => x.set(4))
expect(events.length).toBe(0) // nothing happened yet

const d5 = mobx.reaction(
Expand Down
49 changes: 30 additions & 19 deletions packages/mobx/__tests__/v4/base/tojs.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ test("json1", function () {
})
.join(", ")
})

todos[1].title = "improve coverage" // prints: write blog, improve coverage
mobx._allowStateChanges(true, () => {
todos[1].title = "improve coverage" // prints: write blog, improve coverage
})
expect(output).toBe("write blog, improve coverage")
todos.push({ title: "take a nap" }) // prints: write blog, improve coverage, take a nap
mobx._allowStateChanges(true, () => {
todos.push({ title: "take a nap" }) // prints: write blog, improve coverage, take a nap
})

expect(output).toBe("write blog, improve coverage, take a nap")
})

Expand Down Expand Up @@ -86,10 +90,12 @@ test("json2", function () {
true
)

o.todos[0].details.url = "boe"
o.todos[1].details.url = "ba"
o.todos[0].tags[0] = "reactjs"
o.todos[1].tags.push("pff")
mobx._allowStateChanges(true, () => {
o.todos[0].details.url = "boe"
o.todos[1].details.url = "ba"
o.todos[0].tags[0] = "reactjs"
o.todos[1].tags.push("pff")
})

expect(mobx.toJS(o)).toEqual({
todos: [
Expand Down Expand Up @@ -117,12 +123,14 @@ test("json2", function () {
ab = []
tb = []

o.todos.push(
mobx.observable({
title: "test",
tags: ["x"]
})
)
mobx._allowStateChanges(true, () => {
o.todos.push(
mobx.observable({
title: "test",
tags: ["x"]
})
)
})

expect(mobx.toJS(o)).toEqual({
todos: [
Expand Down Expand Up @@ -151,13 +159,16 @@ test("json2", function () {
ab = []
tb = []

o.todos[1] = mobx.observable({
title: "clean the attic",
tags: ["needs sabbatical"],
details: {
url: "booking.com"
}
mobx._allowStateChanges(true, () => {
o.todos[1] = mobx.observable({
title: "clean the attic",
tags: ["needs sabbatical"],
details: {
url: "booking.com"
}
})
})

expect(JSON.parse(JSON.stringify(o))).toEqual({
todos: [
{
Expand Down
3 changes: 1 addition & 2 deletions packages/mobx/__tests__/v4/mobx4.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { configure } from "../../src/mobx"

configure({
useProxies: "never",
enforceActions: "never"
useProxies: "never"
})

export * from "../../src/mobx"
7 changes: 5 additions & 2 deletions packages/mobx/__tests__/v5/base/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,9 @@ test("auto action can be used to update and is batched", () => {
d()
})

test("auto action should not update state from inside a derivation", async () => {
test("auto action does not act as action in regards to enforceActions inside derivation", async () => {
mobx.configure({ enforceActions: "observed" })

const a = mobx.observable(1)

const d = mobx.autorun(() => a.get()) // observe
Expand All @@ -612,6 +614,7 @@ test("auto action should not update state from inside a derivation", async () =>
await mobx.when(() => {
expect(
utils.grabConsole(() => {
console.log(mobx._getGlobalState())
double()
})
).toMatchInlineSnapshot(
Expand All @@ -622,7 +625,7 @@ test("auto action should not update state from inside a derivation", async () =>
d()
})

test("auto action should not update state from inside a derivation", async () => {
test("auto action acts as action when outside derivation", async () => {
const a = mobx.observable(1)

const d = mobx.autorun(() => a.get()) // observe
Expand Down
6 changes: 3 additions & 3 deletions packages/mobx/__tests__/v5/base/extras.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ test("spy 1", function () {
lines.push(line)
})

a.set(4)
m._allowStateChanges(true, () => a.set(4))
stop()
a.set(5)
m._allowStateChanges(true, () => a.set(5))
expect(stripTrackerOutput(lines)).toMatchSnapshot()
})

Expand Down Expand Up @@ -333,7 +333,7 @@ test("onBecome(Un)Observed simple", () => {
expect(events.length).toBe(0) // nothing happened yet
x.get()
expect(events.length).toBe(0) // nothing happened yet
x.set(4)
m._allowStateChanges(true, () => x.set(4))
expect(events.length).toBe(0) // nothing happened yet

const d5 = mobx.reaction(
Expand Down
17 changes: 8 additions & 9 deletions packages/mobx/__tests__/v5/base/strict-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,21 +180,20 @@ test("strict mode checks", function () {
const x = mobx.observable.box(3)
const d = mobx.autorun(() => x.get())

mobx._allowStateChanges(false, function () {
x.get()
})

mobx._allowStateChanges(true, function () {
x.set(7)
})

expect(
utils.grabConsole(function () {
// enforceActions are "never", therefore it doesn't matter whether state changes are allowed or not
mobx._allowStateChanges(false, function () {
x.set(4)
})
mobx._allowStateChanges(false, function () {
x.get()
})
mobx._allowStateChanges(true, function () {
x.set(7)
})
})
).toMatch(/Side effects like changing state are not allowed at this point/)
).toMatch("")

mobx._resetGlobalState()
d()
Expand Down
49 changes: 30 additions & 19 deletions packages/mobx/__tests__/v5/base/tojs.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ test("json1", function () {
})
.join(", ")
})

todos[1].title = "improve coverage" // prints: write blog, improve coverage
mobx._allowStateChanges(true, () => {
todos[1].title = "improve coverage" // prints: write blog, improve coverage
})
expect(output).toBe("write blog, improve coverage")
todos.push({ title: "take a nap" }) // prints: write blog, improve coverage, take a nap
mobx._allowStateChanges(true, () => {
todos.push({ title: "take a nap" }) // prints: write blog, improve coverage, take a nap
})

expect(output).toBe("write blog, improve coverage, take a nap")
})

Expand Down Expand Up @@ -85,10 +89,12 @@ test("json2", function () {
true
)

o.todos[0].details.url = "boe"
o.todos[1].details.url = "ba"
o.todos[0].tags[0] = "reactjs"
o.todos[1].tags.push("pff")
mobx._allowStateChanges(true, () => {
o.todos[0].details.url = "boe"
o.todos[1].details.url = "ba"
o.todos[0].tags[0] = "reactjs"
o.todos[1].tags.push("pff")
})

expect(mobx.toJS(o)).toEqual({
todos: [
Expand Down Expand Up @@ -116,12 +122,14 @@ test("json2", function () {
ab = []
tb = []

o.todos.push(
mobx.observable({
title: "test",
tags: ["x"]
})
)
mobx._allowStateChanges(true, () => {
o.todos.push(
mobx.observable({
title: "test",
tags: ["x"]
})
)
})

expect(mobx.toJS(o)).toEqual({
todos: [
Expand Down Expand Up @@ -150,13 +158,16 @@ test("json2", function () {
ab = []
tb = []

o.todos[1] = mobx.observable({
title: "clean the attic",
tags: ["needs sabbatical"],
details: {
url: "booking.com"
}
mobx._allowStateChanges(true, () => {
o.todos[1] = mobx.observable({
title: "clean the attic",
tags: ["needs sabbatical"],
details: {
url: "booking.com"
}
})
})

expect(JSON.parse(JSON.stringify(o))).toEqual({
todos: [
{
Expand Down
14 changes: 8 additions & 6 deletions packages/mobx/__tests__/v5/base/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe("trace", () => {
)
expectedLogCalls.push(["[mobx.trace] 'autorun' tracing enabled"])

mobx.transaction(() => {
mobx.runInAction(() => {
x.firstname = "John"
expectedLogCalls.push([
"[mobx.trace] 'x.fullname' is invalidated due to a change in: 'x.firstname'"
Expand Down Expand Up @@ -97,14 +97,14 @@ describe("trace", () => {
)
expectedLogCalls.push(["[mobx.trace] 'autorun' tracing enabled"])

mobx.transaction(() => {
mobx.runInAction(() => {
x.foo = 1
expectedLogCalls.push([
"[mobx.trace] 'x.fooIsGreaterThan5' is invalidated due to a change in: 'x.foo'"
])
})

mobx.transaction(() => {
mobx.runInAction(() => {
x.foo = 6
expectedLogCalls.push([
"[mobx.trace] 'x.fooIsGreaterThan5' is invalidated due to a change in: 'x.foo'"
Expand Down Expand Up @@ -141,8 +141,10 @@ describe("trace", () => {
mobx.autorun(() => {
x.fullname
})
expect(() => {
x.firstname += "!"
}).not.toThrow("Unexpected identifier")
expect(
mobx.action(() => {
x.firstname += "!"
})
).not.toThrow("Unexpected identifier")
})
})
9 changes: 6 additions & 3 deletions packages/mobx/src/api/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ export function configure(options: {
globalState.verifyProxies = true
}
if (enforceActions !== undefined) {
const ea = enforceActions === ALWAYS ? ALWAYS : enforceActions === OBSERVED
globalState.enforceActions = ea
globalState.allowStateChanges = ea === true || ea === ALWAYS ? false : true
globalState.enforceActions =
enforceActions === ALWAYS ? ALWAYS : enforceActions === OBSERVED
// TODO
// const ea = enforceActions === ALWAYS ? ALWAYS : enforceActions === OBSERVED
// globalState.enforceActions = ea
// globalState.allowStateChanges = ea === true || ea === ALWAYS ? false : true
}
;[
"computedRequiresReaction",
Expand Down
4 changes: 3 additions & 1 deletion packages/mobx/src/core/derivation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,10 @@ export function checkIfStateModificationsAreAllowed(atom: IAtom) {
// Should not be possible to change observed state outside strict mode, except during initialization, see #563
if (
!globalState.allowStateChanges &&
(hasObservers || globalState.enforceActions === "always")
(globalState.enforceActions === "always" ||
(globalState.enforceActions === true && hasObservers))
) {
// TODO the else part never occurs ATM
console.warn(
"[MobX] " +
(globalState.enforceActions
Expand Down
4 changes: 2 additions & 2 deletions packages/mobx/src/core/globalstate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const persistentKeys: (keyof MobXGlobals)[] = [
"computedRequiresReaction",
"reactionRequiresObservable",
"observableRequiresReaction",
"allowStateReads",
"disableErrorBoundaries",
"runId",
"UNCHANGED",
Expand Down Expand Up @@ -229,5 +228,6 @@ export function resetGlobalState() {
globalState[key] = defaultGlobals[key]
}
}
globalState.allowStateChanges = !globalState.enforceActions
// TODO del
//globalState.allowStateChanges = !globalState.enforceActions
}

0 comments on commit f55cdb0

Please sign in to comment.