-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathdata.js
More file actions
115 lines (103 loc) · 3.2 KB
/
data.js
File metadata and controls
115 lines (103 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const data = {}
const config = {
RED: null
}
/**
* Checks if a Client/Socket ID has been assigned to this message,
* and whether the node type is being scoped to a specific client.
* If so, do not store this in our centralised datastore
* @param {*} msg
* @returns
*/
function canSaveInStore (base, node, msg) {
// gets a list of node types that allow for client configuration/limits
const constrained = base.acceptsClientConfig
const checks = []
if (constrained.includes(node.type)) {
// core check
if (msg._client?.socketId) {
// we are in a node type that allows for definition of specific clients,
// and a client has been defined
checks.push(false)
}
// plugin checks
// loop over plugins and check if any have defined a custom isValidConnection function
// if so, use that to determine if the connection is valid
for (const plugin of config.RED.plugins.getByType('node-red-dashboard-2')) {
if (plugin.hooks?.onCanSaveInStore) {
checks.push(plugin.hooks.onCanSaveInStore(msg))
}
}
}
return checks.length === 0 || !checks.includes(false)
}
// Strip msg of properties that are not needed for storage
function stripMsg (msg) {
const newMsg = config.RED.util.cloneMessage(msg)
// don't need to store ui_updates in the datastore, as this is handled in statestore
delete newMsg.ui_update
return newMsg
}
const getters = {
RED () {
return config.RED
},
// given a widget id, return the latest msg received
msg (id) {
return config.RED.util.cloneMessage(data[id])
}
}
const setters = {
// map the instance of Node-RED to this module
setConfig (RED) {
config.RED = RED
},
// remove data associated to a given widget
clear (id) {
delete data[id]
},
/**
*
* @param {*} base - the ui-base node associated with this widget
* @param {*} node - the UI node for which we are storing data
* @param {*} msg - the msg to be stored
*/
save (base, node, msg) {
if (Array.isArray(msg)) {
/// need to check msg by msg
const filtered = []
for (const m of msg) {
if (canSaveInStore(base, node, m)) {
filtered.push(config.RED.util.cloneMessage(m))
}
}
data[node.id] = filtered
} else {
if (canSaveInStore(base, node, msg)) {
const newMsg = stripMsg(msg)
data[node.id] = {
...data[node.id],
...newMsg
}
}
}
},
// given a widget id, and msg, store in an array of history of values
// useful for charting widgets
append (base, node, msg) {
if (canSaveInStore(base, node, msg)) {
if (!data[node.id]) {
data[node.id] = []
}
data[node.id].push(config.RED.util.cloneMessage(msg))
}
}
}
module.exports = {
get: getters.msg,
RED: getters.RED,
setConfig: setters.setConfig,
save: setters.save,
append: setters.append,
clear: setters.clear
}