You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -114,7 +114,7 @@ Even though you can use **getters** as normal, they do not cache like **derived*
114
114
115
115
### Serializing class values
116
116
117
-
If you have an application that needs to serialize the state, for example to local storage or server side rendering, you can still use class instances with Overmind. This works out of the box except when you add the **toJSON**method to a class. In that case you will need to add a symbol called **SERIALIZE**:
117
+
If you have an application that needs to serialize the state, for example to local storage or server side rendering, you can still use class instances with Overmind. By default you really do not have to do anything, but if you use **Typescript**or you choose to use **toJSON** on your classesOvermind exposes a symbol called **SERIALIZE** that you can attach to your class.
118
118
119
119
```typescript
120
120
import { SERIALIZE } from'overmind'
@@ -178,11 +178,12 @@ Since our user is a class instance we can tell rehydrate what to do, where it is
178
178
import { SERIALIZE } from'overmind'
179
179
180
180
classUser {
181
+
[SERIALIZE]
181
182
constructor() {
182
183
this.username=''
183
184
this.jwt=''
184
185
}
185
-
fromJSON(json) {
186
+
staticfromJSON(json) {
186
187
returnObject.assign(newUser(), json)
187
188
}
188
189
}
@@ -221,7 +222,7 @@ That means the following will behave as expected:
221
222
import { User } from'./models'
222
223
223
224
exportconst state = {
224
-
user: null, //Expecting an instance or null
225
+
user: null, //Can be existing class instance or null
225
226
usersList: [], // Expecting an array of values
226
227
usersDictionary: {} // Expecting a dictionary of values
Note that **rehydrate** gives you full type safety when adding the **SERIALIZE** symbol to your classes. This is a huge benefit as Typescript will yell at you when the state structure changes, related to the rehydration
262
263
{% endhint %}
263
264
264
-
## Naming
265
+
## Statemachines
265
266
266
-
Each value needs to sit behind a name. Naming can be difficult, but we have some help. Even though we eventually do want to consume our application through a user interface we ideally want to avoid naming things specifically related to the environment where we show the user interface. Things like **page**, **tabs**, **modal**etc. are specific to a browser experience, maybe related to a certain size. We want to avoid those names as they should not dictate which elements are to be used with the state, that is up to the user interface to decide later. So here are some generic terms to use instead:
267
+
Very often you get into a situation where you define states as **isLoading**, **hasError**etc. Having these kinds of state can cause **impossible states**. For example:
267
268
269
+
```typescript
270
+
const state = {
271
+
isAuthenticated: true,
272
+
isAuthenticating: true
273
+
}
274
+
```
268
275
276
+
You can not be authenticating and be authenticated at the same time. This kind of logic very often causes bugs in applications. That is why Overmind allows you to define statemachines. It sounds complicated, but is actually very simple.
If your transition runs asynchronously you should return the transition to ensure that the action execution is tracked
276
340
{% endhint %}
277
341
278
-
* page: **mode**
279
-
* tabs: **sections**
280
-
* modal: **editUser.active**
342
+
What is important to realize here is that our logic is separated into **allowable** transitions. That means when we are waiting for the user on **line 4** and some other logic has changed the state to **unauthenticated** in the meantime, the user will not be set, as the **authenticated** transition is now not possible. This is what state machines do. They group logic into states that are allowed to run, preventing invalid logic to run.
343
+
344
+
### Current state
345
+
346
+
The current state is accessed, related to this example, by:
347
+
348
+
```typescript
349
+
state.mode.current
350
+
```
351
+
352
+
### Exit
353
+
354
+
It is also possible to run logic when a transition exits. An example of this is for example if a transition sets up a subscription. This subscription can be disposed when the transition is exited.
That means this operator can handle any type that matches an **isAwesome** property, though will pass the original type through.
389
389
390
-
## Statecharts
390
+
## Statemachine
391
+
392
+
Statemachines exposes a type called **Statemachine**, though it is strictly not necessary to use as it is recommended to separate your machine definition from your state using the factory:
Copy file name to clipboardExpand all lines: faq.md
+4Lines changed: 4 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,5 +8,9 @@ First… try to refresh your app to reconnect. If this does not work make sure t
8
8
9
9
Restart VS Code
10
10
11
+
## My operator actions are not running?
12
+
13
+
Operators are identified with a Symbol. If you happen to use Overmind across packages you might be running two versions of Overmind. The same goes for core package and view package installed out of version sync. Make sure you are only running on package of Overmind by looking into your **node\_modules** folder.
// We do not need zones, we rather use the tracking
88
-
// directive, which gives us a huge optimization
88
+
// directive, which gives us a pretty signifcant performance
89
+
// boost. Note that 3rd party libraries might need ngZone,
90
+
// in which case you can not set it to "noop"
89
91
ngZone: "noop"
90
92
})
91
93
.catch(err=>console.log(err));
@@ -124,7 +126,7 @@ You can now access the **admin** state and actions directly with **state** and *
124
126
125
127
## NgZone
126
128
127
-
Since Overmind knows when your components should update you can safely turn **ngZone** to `"noop"`. Note that other 3rd party libraries may not support this.
129
+
The Overmind **\*track** directive knows when your components should update, and so is much more efficient at change detection than Angular's default NgZone. In order to take advantage of the efficiency provided by the \***track** directive, you _must_ set **ngZone** to "noop". Note that other 3rd party libraries may not support this. If for any reason you can't set **ngZone** to "noop", then the \***track** directive is redundant, and you can safely exclude it from your templates.
0 commit comments