UNPKG

st8flo

Version:
189 lines (171 loc) 4.88 kB
(function (root, factory) { //Environment Detection if (typeof define === 'function' && define.amd) { //AMD define([], factory); } else if (typeof exports === 'object') { //CommonJS module.exports = factory(); } else { // Script tag import i.e., IIFE var connector = factory(); connector.attach(root.St8); } }(this, function () { var St8Connector = { enableLog:false, logCodes:{ FLOW_CREATE:1, FLOW_PARENTDETACHED:2, FLOW_DELETED:3, FLOWTRIGGERED:4, FLOWTRIGGERED_ERROR:5, TEMPLATE_CREATE:6, TEMPLATE_TRIGGERED:7, TEMPLATE_PENDINGCLEAR:8, TEMPLATE_PENDINGADDED:9, FLOWST8_CREATE:10, FLOWST8_SETUP:11, FLOWST8_STATECHANGE:12, FLOWST8_NEWDATA:13, FLOWST8_RESULTS:14, FLOWST8_CLEARED:15, FLOWST8_DELETED:16, FLOWST8_DIGESTERROR:17, FLOWST8_USERLOG:18, MARKER_CHANGE:19, MARKER_CREATE:20, }, St8: null, dsClient: null, appID:null, logs: [], logSize: 2000, realTimeUpdates: false, dsConnect: function (serverPath, appID, authDetails) { console.log('ST8FLO-connector - connecting to server at ', serverPath); this.dsClient = deepstream(serverPath).login(authDetails); var dsClient = this.dsClient; this.dsClient.on('connectionStateChanged', function () { console.log('ST8FLO-connector - deepstream connection state changed to ', dsClient.getConnectionState()); }); this.dsClient.on('error', function (err, ev, topic) { console.warn('ST8FLO-connector - deepstream ERROR ', err, ev, topic); }); this.appID = appID; this.setupRPC(); return this.dsClient; }, attach: function (st8Container) { if (st8Container) { st8Container.connector = St8Connector; this.St8 = st8Container; } }, getFlowStructures: function (options) { var _self = this; var r = {}; if (_self.St8) { Object.keys(_self.St8.st8Data.flows).forEach(function (fKey) { r[fKey] = _self.St8.st8Data.flows[fKey].exportStructure(options); }); } return r; }, getMarkerStructures:function(options){ var _self = this; var r = {}; if(_self.St8){ Object.keys(_self.St8.st8Data.appMarkers).forEach(function (fKey) { r[fKey] = _self.St8.st8Data.appMarkers[fKey].exportStructure(options); }); } return r; }, getTemplateStructures:function(options){ var _self = this; var r = {}; if(_self.St8){ Object.keys(_self.St8.st8Data.stateTemplates).forEach(function (fKey) { r[fKey] = _self.St8.st8Data.stateTemplates[fKey].exportStructure(options); }); } return r; }, getUserLogMessages:function(){ return this.St8.st8Data.userLogMessages; }, getStructure:function(options){ var r = { appMarkers:this.getMarkerStructures(options), stateTemplates:this.getTemplateStructures(options), flows:this.getFlowStructures(options), userLogMessages:this.getUserLogMessages(), }; return r; }, activateLogging:function(enable){ this.enableLog = !!enable; }, log: function (logCode, params) { this.logs.push({ timeStamp:Date.now(), logCode:logCode, params:params }); if(this.dsClient && this.enableLog){ this.dsClient.event.emit('st8Diag/logLength/'+this.appID, this.logs.length); } if(this.logs.length >= this.logSize){ this.sendLog(); } }, sendLog: function () { if(this.dsClient && this.enableLog){ var _self = this; this.dsClient.rpc.make('st8Diag/logListenerExists/'+this.appID,{}, function(e){ if(e){ console.log('St8Flo Connector - ERROR - no log listener detected - ',e); _self.logs.splice(0); }else{ _self.dsClient.rpc.make('st8Diag/logs/'+_self.appID,{logs:_self.logs.splice(0)}, function(e){ if(e){ console.log('St8Flo Connector - sendLog Error - ',e); } }); } }); } }, setupRPC:function(){ if(this.dsClient){ var _self = this; this.dsClient.rpc.provide('st8Diag/enable/'+this.appID, function(data, res){ if(typeof data !== 'undefined'){ _self.enableLog = data; if(data === true) res.send('enabled'); else res.send('disabled'); } }); this.dsClient.event.subscribe('st8Diag/getLogs/'+this.appID, function(data){ _self.sendLog(); }); this.dsClient.rpc.provide('st8Diag/getStructure/'+this.appID, function(data, res){ res.send(_self.getStructure()); }); this.dsClient.rpc.provide('st8Diag/getFlowStructures/'+this.appID, function(data, res){ res.send(_self.getFlowStructures()); }); this.dsClient.rpc.provide('st8Diag/getTemplateStructures/'+this.appID, function(data, res){ res.send(_self.getTemplateStructures()); }); this.dsClient.rpc.provide('st8Diag/getMarkerStructures/'+this.appID, function(data, res){ res.send(_self.getMarkerStructures()); }); } } }; return St8Connector; }));