Skip to content

Instantly share code, notes, and snippets.

@webfrank
Created January 21, 2017 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webfrank/730abfda9cc791253bafdd832db85668 to your computer and use it in GitHub Desktop.
Save webfrank/730abfda9cc791253bafdd832db85668 to your computer and use it in GitHub Desktop.
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":[]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment