-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #279 from FlowFuse/213-datastore
Extract historical state mgmt into a datastore
- Loading branch information
Showing
8 changed files
with
126 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# State Management | ||
|
||
Dashboard 2.0 conducts state management through the use of a shared server-side data store. It provides four core functions for interaction with the store. | ||
|
||
The store can be injected into a widget using: | ||
|
||
```js | ||
const datastore = require('<path>/<to>/store/index.js') | ||
``` | ||
|
||
|
||
## `datastore.save` | ||
|
||
When a widget receives a message, the default `node.on('input')` handler will store the received message, mapped to the widget's id into the datastore using: | ||
|
||
```js | ||
datastore.save(node.id, msg) | ||
``` | ||
|
||
This will store the latest message received by the widget, which can be retrieved by that same widget on load using: | ||
|
||
## `datastore.get` | ||
|
||
When a widget is initialised, it will attempt to retrieve the latest message from the datastore using: | ||
|
||
```js | ||
datastore.get(node.id) | ||
``` | ||
|
||
This ensures, on refresh of the client, or when new clients connect after data has been geenrated, that the state is presented consistently. | ||
|
||
## `datastore.append` | ||
|
||
With `.append`, we can store multiple messages against the same widget, representing a history of state, rather than a single point reference to the _last_ value only. | ||
|
||
```js | ||
datastore.append(node.id, msg) | ||
``` | ||
|
||
This is used in `ui-chart` to store the history of data points, where each data point could have been an individual message received by the widget. | ||
|
||
## `datastore.clear` | ||
|
||
When a widget is removed from the Editor, we can clear the datastore of any messages stored against that widget using: | ||
|
||
```js | ||
datastore.clear(node.id) | ||
``` | ||
|
||
This ensures that we don't have any stale data in the datastore, and that we don't have any data stored against widgets that no longer exist in the Editor. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const data = {} | ||
|
||
const getters = { | ||
// given a widget id, return the latest msg received | ||
msg (id) { | ||
return data[id] | ||
} | ||
} | ||
|
||
const setters = { | ||
// remove data associated to a given widget | ||
clear (id) { | ||
delete data[id] | ||
}, | ||
// given a widget id, and msg, store that latest value | ||
save (id, msg) { | ||
data[id] = msg | ||
}, | ||
// given a widget id, and msg, store in an array of history of values | ||
// useful for charting widgets | ||
append (id, msg) { | ||
if (!data[id]) { | ||
data[id] = [] | ||
} | ||
data[id].push(msg) | ||
} | ||
} | ||
|
||
module.exports = { | ||
get: getters.msg, | ||
save: setters.save, | ||
append: setters.append, | ||
clear: setters.clear | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters