UNPKG

storage-mule

Version:

The Realtime Framework Cloud Storage Mule utility

48 lines (41 loc) 983 B
(function (){ var timeout = 3 * 60 * 1000; //3 minutes var buffer = null; var ortc = null; function init(ortcClient){ buffer = []; ortc = ortcClient; } function add(data){ if(!data.ts) data.ts = new Date().getTime(); buffer.push(data); } function process(){ var tempBuffer = buffer.slice(); buffer.length = 0; buffer = []; var now = new Date().getTime(); for(var mess in tempBuffer){ if(tempBuffer[mess].ts + timeout > now) send(tempBuffer[mess]); } } //returns 1 when message was send immediately, 0 when was stored to be send when connected function send(data){ if(ortc && ortc.getIsConnected()){ if(data.applicationKey && data.privateKey){ ortc.sendProxy(data.applicationKey, data.privateKey, data.channel, data.message); } else { ortc.send(data.channel, data.message); } return 1; } else { add(data); return 0; } } exports.init = init; exports.process = process; exports.send = send; })();