Skip to content

Commit

Permalink
Merge pull request #607 from FlowFuse/588-ui-switch-client
Browse files Browse the repository at this point in the history
onChange: Ensure custom handlers have context of any general changes made to the msg. object
  • Loading branch information
joepavitt authored Feb 23, 2024
2 parents 1e78708 + e1a1c62 commit dfaa4ed
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
10 changes: 6 additions & 4 deletions docs/contributing/guides/registration.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,14 @@ Similar to `onAction`, when used as a boolean, this flag will trigger the defaul
Alternatively, you can override this default behaviour by providing a custom `onChange` function. An example of this is in the `ui-switch` node which needs to do `node.status` updates to in order for the Node-RED Editor to reflect it's latest status:

```js
onChange: async function (value) {
/**
* Handle the input from the widget
* @param {object} msg - the last known msg received (prior to this new value)
* @param {boolean} value - the updated value sent by the widget
*/
onChange: async function (msg, value) {
// ensure we have latest instance of the widget's node
const wNode = RED.nodes.getNode(node.id)
const msg = wNode._msg || {}

node.status({
fill: value ? 'green' : 'red',
Expand All @@ -119,8 +123,6 @@ onChange: async function (value) {
const off = RED.util.evaluateNodeProperty(config.offvalue, config.offvalueType, wNode)
msg.payload = value ? on : off

wNode._msg = msg

// simulate Node-RED node receiving an input
wNode.send(msg)
}
Expand Down
4 changes: 2 additions & 2 deletions nodes/config/ui_base.js
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ module.exports = function (RED) {

msg = addConnectionCredentials(RED, msg, conn, n)

async function defaultHandler (value) {
async function defaultHandler (msg, value) {
if (typeof (value) === 'object' && value !== null && hasProperty(value, 'payload')) {
msg.payload = value.payload
} else {
Expand All @@ -582,7 +582,7 @@ module.exports = function (RED) {
// Most of the time, we can just use this default handler,
// but sometimes a node needs to do something specific (e.g. ui-switch)
const handler = typeof (widgetEvents.onChange) === 'function' ? widgetEvents.onChange : defaultHandler
await handler(value)
await handler(msg, value)
} catch (error) {
console.log(error)
let errorHandler = typeof (widgetEvents.onError) === 'function' ? widgetEvents.onError : null
Expand Down
3 changes: 1 addition & 2 deletions nodes/widgets/ui_switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ module.exports = function (RED) {
const evts = {
// runs on UI interaction
// value = true | false from the ui-switch
onChange: async function (value) {
onChange: async function (msg, value) {
// ensure we have latest instance of the widget's node
const wNode = RED.nodes.getNode(node.id)
const msg = datastore.get(node.id) || {}

node.status({
fill: value ? 'green' : 'red',
Expand Down

0 comments on commit dfaa4ed

Please sign in to comment.