Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using ts disposable #3841

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sixty-frogs-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mobx": minor
---

add `_action` helper to use with ts disposables
25 changes: 25 additions & 0 deletions docs/actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,21 @@ runInAction(() => {
})
```

<!--using _ = _action();-->

```javascript
import { observable, runInAction } from "mobx"

const state = observable({ value: 0 })

{
using _ = _action();
state.value++
state.value++
}

```

<!--END_DOCUSAURUS_CODE_TABS-->

## Wrapping functions using `action`
Expand Down Expand Up @@ -220,6 +235,16 @@ Usage:
Use this utility to create a temporary action that is immediately invoked. Can be useful in asynchronous processes.
Check out the [above code block](#examples) for an example.

## `_action()`

Usage:

- ` using _ = _action();`

Use this utility to create a temporary action. Can be useful in asynchronous processes.
Behaves the same as `runInAction` but ends action after leaving current block scope.
Check out the [above code block](#examples) for an example.

## Actions and inheritance

Only actions defined **on prototype** can be **overridden** by subclass:
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
],
"resolutions": {
"jest": "^29.5.0",
"typescript": "^5.0.2",
"typescript": "5.2.2",
"recast": "^0.23.1"
},
"repository": {
Expand Down Expand Up @@ -41,8 +41,8 @@
"@types/prop-types": "^15.5.2",
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.0",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"@typescript-eslint/eslint-plugin": "6",
"@typescript-eslint/parser": "^6",
"coveralls": "^3.1.0",
"eslint": "^6.8.0",
"execa": "^4.1.0",
Expand All @@ -68,7 +68,7 @@
"tape": "^5.0.1",
"ts-jest": "^29.0.5",
"tsdx": "^0.14.1",
"typescript": "^5.0.2"
"typescript": "^5.2.2"
},
"husky": {
"hooks": {
Expand Down
61 changes: 61 additions & 0 deletions packages/mobx/__tests__/v5/base/_action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { _action } from "../../../src/internal"
import * as mobx from "../../../src/mobx"


// polyfill Symbbol.dispose for tests
if (typeof Symbol.dispose !== "symbol") {
Object.defineProperty(Symbol, "dispose", {
configurable: false,
enumerable: false,
writable: false,
value: Symbol.for("dispose")
})
}
if (typeof Symbol.asyncDispose !== "symbol"){
Object.defineProperty(Symbol, "asyncDispose", {
configurable: false,
enumerable: false,
writable: false,
value: Symbol.for("asyncDispose")
})
}

test("runInAction", () => {
mobx.configure({ enforceActions: "observed" })
const values = [] as any[]
const events = [] as any[]
const spyDisposer = mobx.spy(ev => {
if (ev.type === "action")
events.push({
name: ev.name,
arguments: ev.arguments
})
})

const observable = mobx.observable.box(0)
const d = mobx.autorun(() => values.push(observable.get()))

{
using _ = _action();
observable.set(observable.get() + 6 * 2)
observable.set(observable.get() - 3) // oops
}
expect(values).toEqual([0, 9])

{
using _ = _action();
observable.set(observable.get() + 5 * 2)
observable.set(observable.get() - 4) // oops
}

expect(values).toEqual([0, 9, 15])
expect(events).toEqual([
{ arguments: [], name: "<unnamed action>" },
{ arguments: [], name: "<unnamed action>" }
])

mobx.configure({ enforceActions: "never" })
spyDisposer()

d()
})
14 changes: 13 additions & 1 deletion packages/mobx/src/api/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import {
isStringish,
createDecoratorAnnotation,
createActionAnnotation,
is20223Decorator
is20223Decorator,
_endAction,
_startAction
} from "../internal"

import type { ClassFieldDecorator, ClassMethodDecorator } from "../types/decorator_fills"
Expand Down Expand Up @@ -105,3 +107,13 @@ export function runInAction<T>(fn: () => T): T {
export function isAction(thing: any) {
return isFunction(thing) && thing.isMobxAction === true
}

export function _action() {
const runInfo = _startAction(DEFAULT_ACTION_NAME, false, this, undefined)

return {
[Symbol.dispose]() {
_endAction(runInfo)
}
}
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"target": "es6",
"module": "esnext",
"moduleResolution": "node",
"lib": ["es6"],
"lib": ["es6", "esnext.disposable"],
"downlevelIteration": true,
"alwaysStrict": true,
"sourceMap": true,
Expand Down