Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

state.js 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Store to manage any dynamic properties set
  2. const state = {}
  3. const config = {
  4. RED: null
  5. }
  6. /**
  7. * Checks if a Client/Socket ID has been assigned to this message,
  8. * If so, do not store this in our centralised datastore
  9. * @param {*} msg
  10. * @returns
  11. */
  12. function canSaveInStore (base, node, msg) {
  13. const checks = []
  14. if (msg) {
  15. // core check
  16. if (base.acceptsClientConfig.includes(node.type)) {
  17. // we are in a node type that allows for definition of specific clients,
  18. if (msg._client?.socketId) {
  19. // and a client has been defined
  20. checks.push(false)
  21. } else {
  22. // whilst this node does allow for constraints, none were defined in this message
  23. checks.push(true)
  24. }
  25. }
  26. // plugin checks
  27. // loop over plugins and check if any have defined a custom isValidConnection function
  28. // if so, use that to determine if the connection is valid
  29. for (const plugin of config.RED.plugins.getByType('node-red-dashboard-2')) {
  30. if (plugin.hooks?.onCanSaveInStore) {
  31. checks.push(plugin.hooks.onCanSaveInStore(msg))
  32. }
  33. }
  34. }
  35. return checks.length === 0 || !checks.includes(false)
  36. }
  37. const getters = {
  38. RED () {
  39. return config.RED
  40. },
  41. // given a widget id, return all dynamically set properties
  42. all (id) {
  43. return state[id]
  44. },
  45. // given a widget id, return a specific dynamically set property
  46. property (id, property) {
  47. if (Object.prototype.hasOwnProperty.call(state, id)) {
  48. return state[id][property]
  49. } else {
  50. return undefined
  51. }
  52. }
  53. }
  54. const setters = {
  55. // map the instance of Node-RED to this module
  56. setConfig (RED) {
  57. config.RED = RED
  58. },
  59. /**
  60. *
  61. * @param {*} base - associated ui-base node
  62. * @param {*} node - the Node-RED node object we're storing state for
  63. * @param {*} msg - the full received msg (allows us to check for credentials/socketid constraints)
  64. * @param {*} prop - the property we are setting on the node
  65. * @param {*} value - the value we are setting
  66. */
  67. set (base, node, msg, prop, value) {
  68. if (canSaveInStore(base, node, msg)) {
  69. if (!state[node.id]) {
  70. state[node.id] = {}
  71. }
  72. state[node.id][prop] = value
  73. }
  74. },
  75. // remove data associated to a given widget
  76. reset (id) {
  77. delete state[id]
  78. }
  79. }
  80. module.exports = {
  81. getAll: getters.all,
  82. getProperty: getters.property,
  83. RED: getters.RED,
  84. setConfig: setters.setConfig,
  85. set: setters.set,
  86. reset: setters.reset
  87. }