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

WIP: Feature/separate actions 84 #99

Merged
merged 8 commits into from
Apr 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 60 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ const Box = types.model("Box",{
x: 0,
y: 0,

// computed prop
// computed prop / views
get width() {
return this.name.length * 15
},

// action
}
}, {
// actions
move(dx, dy) {
this.x += dx
this.y += dy
Expand All @@ -91,7 +91,8 @@ const Box = types.model("Box",{

const BoxStore = types.model("BoxStore",{
boxes: types.map(Box),
selection: types.reference("boxes/name"),
selection: types.reference("boxes/name")
}, {
addBox(name, x, y) {
const box = Box.create({ id: uuid(), name, x, y })
this.boxes.put(box)
Expand Down Expand Up @@ -158,6 +159,14 @@ Useful methods:

It is not necessary to express all logic around models as actions. For example it is not possible to define constructors on models. Rather, it is recommended to create stateless utility methods that operate on your models. It is recommended to keep models self-contained and to do orchestration around models in utilities around it.

## Views

TODO

Views versus actions

Exception: `"Invariant failed: Side effects like changing state are not allowed at this point."` indicates that a view function tries to modifies a model. This is only allowed in actions.

## Protecting the state tree

By default it is allowed to both directly modify a model or through an action.
Expand All @@ -167,7 +176,8 @@ To disable modifying data in the tree without action, simple call `protect(model

```javascript
const Todo = types.model({
done: false,
done: false
}, {
toggle() {
this.done = !this.done
}
Expand Down Expand Up @@ -318,9 +328,9 @@ The result of this function is the return value of the callbacks, or the origina

**Parameters**

- `value`
- `asNodeCb`
- `asPrimitiveCb`
- `value`
- `asNodeCb`
- `asPrimitiveCb`

## ComplexType

Expand Down Expand Up @@ -396,7 +406,7 @@ Example of a logging middleware:
**Parameters**

- `target` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** model to intercept actions on
- `middleware`
- `middleware`

Returns **IDisposer** function to remove the middleware

Expand All @@ -411,7 +421,7 @@ Patches can be used to deep observe a model tree.
**Parameters**

- `target` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** the model instance from which to receive patches
- `callback`
- `callback`

Returns **IDisposer** function to remove the listener

Expand All @@ -423,8 +433,8 @@ Applies a JSON-patch to the given model instance or bails out if the patch could

**Parameters**

- `target` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
- `patch` **IJsonPatch**
- `target` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
- `patch` **IJsonPatch**

## applyPatches

Expand All @@ -434,8 +444,8 @@ Applies a number of JSON patches in a single MobX transaction

**Parameters**

- `target` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
- `patches` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<IJsonPatch>**
- `target` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
- `patches` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<IJsonPatch>**

## applyActions

Expand All @@ -447,9 +457,9 @@ Does not return any value

**Parameters**

- `target` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
- `actions` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<IActionCall>**
- `options` **\[IActionCallOptions]**
- `target` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
- `actions` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<IActionCall>**
- `options` **\[IActionCallOptions]**

## protect

Expand All @@ -462,7 +472,7 @@ To disable modifying data in the tree without action, simple call `protect(model

**Parameters**

- `target`
- `target`

**Examples**

Expand All @@ -489,7 +499,7 @@ Returns true if the object is in protected mode, @see protect

**Parameters**

- `target`
- `target`

## applySnapshot

Expand All @@ -499,8 +509,8 @@ Applies a snapshot to a given model instances. Patch and snapshot listeners will

**Parameters**

- `target` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
- `snapshot` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
- `target` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
- `snapshot` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**

## hasParent

Expand All @@ -510,10 +520,10 @@ Given a model instance, returns `true` if the object has a parent, that is, is p

**Parameters**

- `target` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
- `target` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
- `depth` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** = 1, how far should we look upward?

Returns **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**
Returns **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**

## getPath

Expand All @@ -523,9 +533,9 @@ Returns the path of the given object in the model tree

**Parameters**

- `target` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
- `target` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**

Returns **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)**
Returns **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)**

## getPathParts

Expand All @@ -535,9 +545,9 @@ Returns the path of the given object as unescaped string array

**Parameters**

- `target` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
- `target` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**

Returns **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)>**
Returns **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)>**

## isRoot

Expand All @@ -547,9 +557,9 @@ Returns true if the given object is the root of a model tree

**Parameters**

- `target` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
- `target` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**

Returns **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**
Returns **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**

## resolve

Expand All @@ -559,32 +569,32 @@ Resolves a path relatively to a given object.

**Parameters**

- `target` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
- `target` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
- `path` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** escaped json path

Returns **Any**
Returns **Any**

## tryResolve

[lib/core/mst-operations.js:282-287](https://github.com/mweststrate/mobx-state-tree/blob/5b0f27f3e1deaceb2dce1246995518c4917f46a7/lib/core/mst-operations.js#L282-L287 "Source code on GitHub")

**Parameters**

- `target` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
- `path` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)**
- `target` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
- `path` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)**

Returns **Any**
Returns **Any**

## clone

[lib/core/mst-operations.js:301-310](https://github.com/mweststrate/mobx-state-tree/blob/5b0f27f3e1deaceb2dce1246995518c4917f46a7/lib/core/mst-operations.js#L301-L310 "Source code on GitHub")

**Parameters**

- `source` **T**
- `keepEnvironment`
- `source` **T**
- `keepEnvironment`

Returns **T**
Returns **T**

## detach

Expand All @@ -594,7 +604,7 @@ Removes a model element from the state tree, and let it live on as a new state t

**Parameters**

- `thing`
- `thing`

## destroy

Expand All @@ -604,7 +614,7 @@ Removes a model element from the state tree, and mark it as end-of-life; the ele

**Parameters**

- `thing`
- `thing`

## applyAction

Expand All @@ -615,9 +625,9 @@ Returns the value of the last actoin

**Parameters**

- `target` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
- `action` **IActionCall**
- `options` **\[IActionCallOptions]**
- `target` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
- `action` **IActionCall**
- `options` **\[IActionCallOptions]**

## escapeJsonPath

Expand All @@ -628,7 +638,7 @@ escape slashes and backslashes

**Parameters**

- `str`
- `str`

## unescapeJsonPath

Expand All @@ -638,7 +648,7 @@ unescape slashes and backslashes

**Parameters**

- `str`
- `str`

# FAQ

Expand Down Expand Up @@ -683,13 +693,17 @@ So far this might look a lot like an immutable state tree as found for example i

## TypeScript & MST

TypeScript support is best effort, as not all patterns can be expressed in TypeScript. But except for assigning snapshots to properties we got pretty close! As MST uses the latest fancy typescript features it is recommended to use TypeScript 2.3 or higher, with `noImplicitThis` and `strictNullChecks` enabled.

When using models, you write interface along with it's property types that will be used to perform type checks at runtime.
What about compile time? You can use TypeScript interfaces indeed to perform those checks, but that would require writing again all the properties and their actions!

Good news? You don't need to write it twice! Using the `typeof` operator of TypeScript over the `.Type` property of a MST Type, will result in a valid TypeScript Type!

```typescript
const Todo = types.model({
title: types.string,
title: types.string
}, {
setTitle(v: string) {
this.title = v
}
Expand Down
31 changes: 31 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
# 0.4.0

**BREAKING** `types.model` no requires 2 parameters to define a model. The first parameter defines the properties, derived values and view functions. The second argment is used to define the actions. For example:

```javascript
const Todo = types.model("Todo", {
done: types.boolean,
toggle() {
this.done = !this.done
}
})
```

Now should be defined as:

```javascript
const Todo = types.model(
"Todo",
{
done: types.boolean,
},
{
toggle() {
this.done = !this.done
}
}
)
```

It is still possible to define functions on the first object. However, those functions are not considered to be actions, but views. They are not allowed to modify values, but instead should produce a new value themselves.

# 0.3.3

* Introduced lifecycle hooks `afterCreate`, `afterAttach`, `beforeDetach`, `beforeDestroy`, implements #76
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"nyc": "^10.0.0",
"tape": "^4.6.0",
"tslint": "^3.15.1",
"typescript": "2.2.2",
"typescript": "next",
"webpack": "^1.13.1",
"webpack-fail-plugin": "^1.0.6"
},
Expand All @@ -66,4 +66,4 @@
"lib/**/*.js"
]
}
}
}
2 changes: 1 addition & 1 deletion src/core/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function runMiddleWares(node: MSTAdministration, baseCall: IRawActionCall): any
export function createActionInvoker(name: string, fn: Function) {
const action = mobxAction(name, fn)

const actionInvoker = function () {
const actionInvoker = function (this: IMSTNode) {
const adm = getMSTAdministration(this)
adm.assertAlive()
if (adm.isRunningAction()) {
Expand Down
Loading