Delay until no more changes

Not strictly a flow, but a function. Could be realized a simple node.

The functions delay emitting the message until no more updates arrives up to prefixed timeout, after which the latest msg is sent.

if(!context.get('cache')) { context.set('cache', {}); } var cache = context.get('cache'); if(cache[msg.payload]) { clearTimeout(cache[msg.payload]); } cache[msg.payload] = setTimeout(function() { node.send(msg); delete cache[msg.payload]; }, 5000)

[{"id":"b33b1428.207bd","type":"watch","z":"f3384309.2a3b68","name":"","files":"/backups","x":82.5,"y":495,"wires":[["82bf0ec1.cd9408"]]},{"id":"82bf0ec1.cd9408","type":"function","z":"f3384309.2a3b68","name":"Delay until no more changes","func":"if(!context.get('cache')) {\n    context.set('cache', {});\n}\nvar cache = context.get('cache');\nif(cache[msg.payload]) {\n    clearTimeout(cache[msg.payload]);\n}\ncache[msg.payload] = setTimeout(function() {\n    msg.localFilename = msg.payload;\n    node.send(msg);\n    delete cache[msg.payload];\n}, 5000)","outputs":1,"noerr":0,"x":296.5,"y":495,"wires":[["33905f08.430158"]]},{"id":"33905f08.430158","type":"box out","z":"f3384309.2a3b68","box":"","filename":"/NodeRed/","localFilename":"","name":"","x":542.5,"y":495,"wires":[]}]
webfrank

Flow Info

created 3 months, 1 week ago

Node Types

Core
  • function (x1)
  • watch (x1)
Other

Tags

  • watch
  • delay
  • file
  • changes
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option