Skip to content

Commit

Permalink
Merge pull request #95 from flowforge/before-send
Browse files Browse the repository at this point in the history
Introduce a "beforeSend" event to handle evaluated properties
  • Loading branch information
joepavitt authored Aug 1, 2023
2 parents 55e52c2 + 7f19e92 commit 365ff5b
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 16 deletions.
Binary file modified docs/assets/images/events-architecture.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 21 additions & 4 deletions nodes/config/ui_base.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,19 @@ module.exports = function (RED) {
// store the latest msg passed to node
wNode._msg = msg

// pre-process the msg before running our onInput function
if (widgetEvents?.beforeSend) {
msg = widgetEvents.beforeSend(msg)
}

// run any node-specific handler defined in the Widget's component
if (widgetEvents?.onInput) {
widgetEvents?.onInput(msg, send, done)
} else {
done()
// msg could be null if the beforeSend errors and returns null
if (msg) {
send(msg)
}
}
})

Expand Down Expand Up @@ -334,11 +342,15 @@ module.exports = function (RED) {

console.log('conn:' + conn.id, 'on:widget-change', value)
// TODO: bind this property to whichever chosen, for now use payload
const msg = wNode._msg || {}
let msg = wNode._msg || {}
msg.payload = value

wNode._msg = msg

if (widgetEvents?.beforeSend) {
msg = widgetEvents.beforeSend(msg)
}

// simulate Node-RED node receiving an input
wNode.send(msg)
}
Expand All @@ -360,13 +372,18 @@ module.exports = function (RED) {
if (widgetEvents?.onAction) {
if (!ui.events.change[widget.id]) {
ui.ioServer.on('connection', function (conn) {
conn.on('widget-action:' + widget.id, (evt) => {
conn.on('widget-action:' + widget.id, (msg) => {
// ensure we have latest instance of the widget's node
const wNode = RED.nodes.getNode(widgetNode.id)

console.log('conn:' + conn.id, 'on:widget-action:' + widget.id)

if (widgetEvents?.beforeSend) {
msg = widgetEvents.beforeSend(msg)
}

// simulate Node-RED node receiving an input as to trigger on('input)
wNode.send(evt)
wNode.send(msg)
})
})
ui.events.action[widget.id] = true
Expand Down
12 changes: 8 additions & 4 deletions nodes/widgets/ui_button.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ module.exports = function (RED) {

const evts = {
onAction: true,
onInput: function (msg, send) {
var error = null
beforeSend: function (msg) {
let error = null

// retrieve the payload we're sending from this button
let payloadType = config.payloadType
Expand Down Expand Up @@ -46,8 +46,12 @@ module.exports = function (RED) {
if (!error) {
const topic = RED.util.evaluateNodeProperty(config.topic, config.topicType || 'str', node, msg)
msg.payload = payload
msg.topic = topic
send(msg)
if (topic) {
msg.topic = topic
}
return msg
} else {
return null
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions nodes/widgets/ui_dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ module.exports = function (RED) {
const group = RED.nodes.getNode(config.group)

const evts = {
onChange: true,
onInput: function (msg, send) {
send(msg)
}
onChange: true
}

// inform the dashboard UI that we are adding this node
Expand Down
5 changes: 1 addition & 4 deletions nodes/widgets/ui_slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ module.exports = function (RED) {
}

const evts = {
onChange: true,
onInput: function (msg, send) {
send(msg)
}
onChange: true
}

// inform the dashboard UI that we are adding this node
Expand Down
1 change: 1 addition & 0 deletions nodes/widgets/ui_switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = function (RED) {

const evts = {
// runs on UI interaction
// value = true | false from the ui-switch
onChange: function (value) {
// ensure we have latest instance of the widget's node
const wNode = RED.nodes.getNode(node.id)
Expand Down
1 change: 1 addition & 0 deletions ui/src/widgets/ui-dropdown/UIDropdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export default {
methods: {
onChange () {
console.log('dropdown changed')
// ensure our data binding with vuex store is updated
this.$store.commit('data/bind', {
widgetId: this.id,
data: this.value
Expand Down

0 comments on commit 365ff5b

Please sign in to comment.