feat(docs): Add _actions operator.

This commit is contained in:
Gervwyk 2021-05-17 15:42:49 +02:00
parent 94db71d2ac
commit d5583d200a
5 changed files with 135 additions and 4 deletions

View File

@ -125,14 +125,15 @@ _ref:
- `actions: actionObjects[]`: The list of [Lowdefy action objects](https://docs.lowdefy.com/events-and-actions) which will be evaluated by the Lowdefy engine.
- `history: object[]`: A list of objects logging the event calls and responses.
- `blockId: string`: The block id from which the event was called.
- `endTimestamp: datetime`: Timestamp for when the event was completed.
- `event: object`: The event object passed to the event.
- `eventName: string`: The event name which which triggerEvent was called.
- `success: boolean`: True if all actions for the event executed without throwing any errors.
- `timestamp: datetime`: Timestamp for when the event was completed.
- `responses: object[]`: The list of action responses.
- `actionId: string`: The id of the triggered action.
- `actionType: string`: The type of action called.
- `startTimestamp: datetime`: Timestamp for when the event was started.
- `responses: object[]`: The list of action responses, where the object key is equal to the action id.
- `type: string`: The type of action called.
- `error: Error: If the action throw an error.
- `index: number: Index of the action in the event array.
- `response: any`: The returned result of the action.
- `skipped: boolean`: True if the action was skipped.
- `methods: object`: All application methods built into Lowdefy, available for the block.

View File

@ -83,6 +83,9 @@ _ref:
params:
# ...
```
# The actions object
When an event is triggered each completed action writes its response to the actions object under the action id object key. Thus all following actions in a event action list has access to the response of all preceding actions in the same event list through the [`_actions`](/_actions) operator.
# The event object
@ -111,6 +114,7 @@ _ref:
The following actions can be used:
- [`CallMethod`](/CallMethod) - Call a method defined by another block.
- [`JsAction`](/JsAction) - Call a custom JavaScript function as an action.
- [`Link`](/Link) - Link to another page.
- [`Message`](/Message) - Show a message to the user.
- [`Notification`](/Notification) - Show a notification to the user.

View File

@ -527,6 +527,9 @@
title: Operators
icon: ToolOutlined
links:
- id: _actions
type: MenuLink
pageId: _actions
- id: _and
type: MenuLink
pageId: _and

View File

@ -0,0 +1,122 @@
# Copyright 2020-2021 Lowdefy, Inc
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
_ref:
path: templates/operators.yaml.njk
transformer: templates/operatorsMethodTransformer.js
vars:
pageId: _actions
pageTitle: _actions
filePath: operators/_actions.yaml
types: |
```
(key: string): any
(all: boolean): any
(arguments: {
all?: boolean,
key?: string,
default?: any,
}): any
```
description: |
The `_actions` operator returns the response value for a preceding action in the same event list.
The action response object has the following structure:
```yaml
error: Error,
index: number,
response: any,
skipped: boolean,
type: string,
```
arguments: |
###### string
If the `_actions` operator is called with a string equal to a preceding action id in the same event list, the action response object returned. If a string is passed which does not match preceding action id in the same event list, `null` is returned. Dot notation is supported.
###### boolean
If the `_actions` operator is called with boolean argument `true`, an object with all the preceding action id responses in the same event list is returned.
examples: |
##### Using a action response:
```yaml
_actions: my_action.response
```
Returns: The response returned by the action.
##### Setting a action response to `state`:
```yaml
id: refresh
type: Button
events:
onClick:
- id: get_fresh_data
type: Request
skip:
_state: should_not_fetch
params: get_data
- id: set_data
type: SetState
params:
did_not_fetch_data:
_actions: get_fresh_data.skipped
```
##### Setting a returned [`JsAction`](/JsAction) response to `state`:
First register a custom JavaScript action: `getNormalizedEigenvector`
```yaml
# file: lowdefy.yaml
lowdefy: '3.15.0'
app:
html:
appendHead: |
<script type="text/javascript">
const getNormalizedEigenvector = (context, ...args) => {
const vectorLength = Math.sqrt(args.reduce((acc, curVal) => Math.pow(curVal, 2) + acc), 0);
return args.map(val => val / vectorLength );
}
window.lowdefy.registerJsAction('getNormalizedEigenvector', getNormalizedEigenvector);
</script>
```
Then, calculate the vector and update state.
```yaml
# onClick event on a block.
id: calculate
type: Button
events:
onClick:
- id: get_normalized_eigenvector
type: JsAction
params:
name: getNormalizedEigenvector
args:
- 1
- 5
- -12
- 7
- 4
- id: update_state
type: SetState
params:
normal_eigenvector:
_actions: get_normalized_eigenvector.response
```
In this example, state will now be equal to:
```yaml
normal_eigenvector:
- 0.06523280730534423
- 0.3261640365267211
- -0.7827936876641306
- 0.45662965113740955
- 0.2609312292213769
```

View File

@ -140,6 +140,7 @@
- _ref: actions/SetState.yaml
- _ref: actions/Validate.yaml
- _ref: operators/_actions.yaml
- _ref: operators/_and.yaml
- _ref: operators/_args.yaml
- _ref: operators/_array.yaml