UNPKG

diffusion

Version:

Diffusion JavaScript client

137 lines (110 loc) 4.08 kB
var _implements = require('util/interface')._implements; // Internal components var Emitter = require('events/emitter'); var Result = require('events/result'); // Namespaced features var Messages = require('features/messages'); var TopicNotifications = require('features/topic-notifications'); var Security = require('features/security').Security; var TopicControl = require('features/topic-control'); var TopicUpdate = require('features/topic-update'); var ClientControl = require('features/client-control'); var TimeSeries = require('features/time-series/time-series-impl'); var LogRetrieval = require('features/log-retrieval'); // Self-assigned features var features = [ require('features/topics'), require('features/ping') ]; // API components var Options = require('../../options'); var Session = require('../../session'); var SessionImpl = _implements(Session, function SessionImpl(sessionID, options, internalSession) { var emitter = Emitter.assign(this); var self = this; // Re-emit internal session events internalSession.on({ reconnect: function () { emitter.emit('reconnect'); }, disconnect: function (reason) { emitter.emit('disconnect', reason); }, error: function (err) { emitter.emit('error', err); }, close: function (reason) { emitter.emit('close', reason); } }); // Self-assign feature implementations onto session features.forEach(function(feature) { var instance = new feature(internalSession); for (var k in instance) { self[k] = instance[k]; } }); this.topics = new TopicControl(internalSession, this); this.clients = new ClientControl(internalSession, this); this.messages = new Messages(internalSession, this); this.notifications = new TopicNotifications(internalSession, this); this.security = new Security(internalSession, this); this.timeseries = new TimeSeries(internalSession, this); this.topicUpdate = new TopicUpdate(internalSession, this); this.logRetrieval = new LogRetrieval(internalSession); this.sessionID = sessionID.toString(); // Remove credentials to prevent 3rd party access this.options = options.with({ credentials : undefined }); this.close = function() { internalSession.close(); return self; }; this.isClosed = function() { var state = internalSession.getState(); return state === "closing" || state === "closed"; }; this.isConnected = function() { return internalSession.isConnected(); }; this.toString = function() { return "Session<" + self.sessionID + " " + internalSession.getState() + ">"; }; this.getPrincipal = function() { return options.principal || ""; }; this.lock = function(lockName, scope) { return internalSession.lock(lockName, scope); }; }); module.exports = SessionImpl; module.exports.create = function(internalSessionFactory, options) { // Merge defaults if (typeof options === 'string') { options = new Options({ host : options }); } else { options = new Options(options); } // Connection promise var emitter = new Emitter(); var result = new Result(emitter); function onConnect(sessionID) { internalSession.off('connect', onConnect); internalSession.off('close', onError); internalSession.off('error', onError); emitter.emit('complete', new SessionImpl(sessionID, options, internalSession)); } function onError(err) { internalSession.off('connect', onConnect); internalSession.off('close', onError); internalSession.off('error', onError); emitter.error(err); } var internalSession = internalSessionFactory(options); internalSession.on('connect', onConnect); internalSession.on('close', onError); internalSession.on('error', onError); internalSession.connect(); return result; };