Skip to content

Commit

Permalink
Merge pull request #280 from FlowFuse/219-onload-extraction
Browse files Browse the repository at this point in the history
Improve onLoad & onInput behaviours
  • Loading branch information
joepavitt authored Oct 19, 2023
2 parents 1bc99cb + aeac921 commit 97e6ce6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
3 changes: 2 additions & 1 deletion nodes/config/ui_base.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ module.exports = function (RED) {

const { wNode, widgetEvents } = getWidgetAndConfig(id)
if (!wNode) {
console.log('widget does not exist any more')
return // widget does not exist any more (e.g. deleted from NR and deployed BUT the ui page was not refreshed)
}
async function handler () {
Expand All @@ -387,7 +388,7 @@ module.exports = function (RED) {
if (msg) {
// only emit something if we have something to send
// and only to this connection, not all connected clients
conn.emit('msg-input:' + id, msg)
conn.emit('widget-load:' + id, msg)
}
}
// wrap execution in a try/catch to ensure we don't crash Node-RED
Expand Down
12 changes: 10 additions & 2 deletions ui/src/widgets/data-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@ import { inject, onMounted, onUnmounted } from 'vue'
import { useStore } from 'vuex'

// by convention, composable function names start with "use"
export function useDataTracker (widgetId, onInput) {
export function useDataTracker (widgetId, onInput, onLoad) {
const store = useStore()
const socket = inject('$socket')

// a composable can also hook into its owner component's
// lifecycle to setup and teardown side effects.
onMounted(() => {
if (socket) {
socket.on('widget-load:' + widgetId, (msg) => {
store.commit('data/bind', {
widgetId,
msg
})
if (onLoad) {
onLoad(msg)
}
})
// This will on in msg input for ALL components
socket.on('msg-input:' + widgetId, (msg) => {
// set states if passed into msg
if ('enabled' in msg) {
console.log('setting enabled')
store.commit('ui/widgetState', {
widgetId,
enabled: msg.enabled
Expand Down
8 changes: 7 additions & 1 deletion ui/src/widgets/ui-chart/UIChart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default {
},
created () {
// can't do this in setup as we have custom onInput function
useDataTracker(this.id, this.onMsgInput)
useDataTracker(this.id, this.onMsgInput, this.onLoad)
},
mounted () {
// get a reference to the canvas element
Expand Down Expand Up @@ -88,6 +88,12 @@ export default {
this.chart = shallowRef(chart)
},
methods: {
onLoad (history) {
// we have received a history of data points
// we need to add them to the chart - fortunately,
// it's just the same process as receiving a new msg
this.onMsgInput(history)
},
onMsgInput (msg) {
if (Array.isArray(msg.payload) && !msg.payload.length) {
// clear the chart if msg.payload = [] is received
Expand Down

0 comments on commit 97e6ce6

Please sign in to comment.