States
You can use states to store user data
What are states?
States allow you to store user data (for example, a newly entered nickname in a scope). The states also store data about the current scopes (state.__currentScope
). The data is stored locally until the project is reloaded, but you can also use your state getter to persist the data in your database
Using states
To use state in the controller/scope, use @GetState
property decorator. The state type is your own object, so you can use interfaces to describe the state type. Then you can use this.state
expression in your handlers to get/set current user state
Default state value
You can set a default state value per user if it has no state. In the main.ts
file, call the .setDefaultValue
stateStore method
.setDefaultValue method takes argument: default value (object)
Custom state getter
You can set your own state getter (e.g. with database work). In the main.ts
file, call the .setCustomGetter
stateStore method and pass your custom method as argument. It's a function (it can be async) that takes some arguments (below hint). This function is called when a handler is processing an update and state is required in its controller
.setCustomGetter method takes argument: your custom getter (method) you want to set. It takes these arguments. This function should return a state (object)
Your custom method can take arguments:
User ID
Params from the handler
Default value
Custom state setter
You can set your own state getter (e.g. to save the new state to the db). In the main.ts
file, call the .setCustomSetter
stateStore method and pass your custom method as argument. It's a function (it can be async) that takes some arguments (below hint). This function is called when the state changes
.setCustomSetter method takes argument: your custom setter (method) you want to set. It takes these arguments
Your custom method can take arguments:
User ID
The name of the changed state property
The value of the changed state property
Params from the handler
Last updated