smartuniverse/dist_ts/smartuniverse.classes.universeconnection.js

110 lines
7.9 KiB
JavaScript
Raw Permalink Normal View History

2020-09-24 18:13:48 +00:00
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UniverseConnection = void 0;
const plugins = __importStar(require("./smartuniverse.plugins"));
const smartuniverse_classes_universechannel_1 = require("./smartuniverse.classes.universechannel");
/**
* represents a connection to the universe
*/
class UniverseConnection {
constructor(optionsArg) {
this.terminatedDeferred = plugins.smartpromise.defer();
this.authenticationRequests = [];
this.authenticatedChannels = [];
this.failedToJoinChannels = [];
this.universeRef = optionsArg.universe;
this.authenticationRequests = optionsArg.authenticationRequests;
this.socketConnection = optionsArg.socketConnection;
this.socketConnection.eventSubject.subscribe(async (eventArg) => {
switch (eventArg) {
case 'disconnected':
await this.disconnect('upstreamevent');
break;
}
});
}
/**
*
* @param universeConnectionArg
*/
static async addConnectionToCache(universeRef, universeConnectionArg) {
let universeConnection = universeConnectionArg;
universeConnection = await UniverseConnection.deduplicateUniverseConnection(universeRef.universeCache, universeConnection);
universeConnection = await UniverseConnection.authenticateAuthenticationRequests(universeRef, universeConnection);
universeRef.universeCache.connectionMap.add(universeConnection);
console.log('hi');
}
/**
* deduplicates UniverseConnections
*/
static async deduplicateUniverseConnection(universeCache, universeConnectionArg) {
let connectionToReturn;
universeCache.connectionMap.forEach(async (existingConnection) => {
if (existingConnection.socketConnection === universeConnectionArg.socketConnection) {
connectionToReturn = await this.mergeUniverseConnections(existingConnection, universeConnectionArg);
}
});
if (!connectionToReturn) {
connectionToReturn = universeConnectionArg;
}
return connectionToReturn;
}
/**
* authenticate AuthenticationRequests
*/
static async authenticateAuthenticationRequests(universeRef, universeConnectionArg) {
for (const authenticationRequest of universeConnectionArg.authenticationRequests) {
const universeChannelToAuthenticateAgainst = smartuniverse_classes_universechannel_1.UniverseChannel.getUniverseChannelByName(universeRef, authenticationRequest.name);
if (universeChannelToAuthenticateAgainst.passphrase === authenticationRequest.passphrase) {
universeConnectionArg.authenticatedChannels.push(universeChannelToAuthenticateAgainst);
}
}
return universeConnectionArg;
}
/**
* merges two UniverseConnections
*/
static mergeUniverseConnections(connectionArg1, connectionArg2) {
return connectionArg1;
}
/**
* finds a UniverseConnection by providing a socket connection
*/
static findUniverseConnectionBySocketConnection(universeCache, socketConnectionArg) {
const universeConnection = universeCache.connectionMap.find(universeConnectionArg => {
return universeConnectionArg.socketConnection === socketConnectionArg;
});
return universeConnection;
}
/**
* disconnect the connection
*/
async disconnect(reason = 'triggered') {
if (reason === 'triggered') {
await this.socketConnection.disconnect();
}
this.universeRef.universeCache.connectionMap.remove(this);
this.terminatedDeferred.resolve();
}
}
exports.UniverseConnection = UniverseConnection;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnR1bml2ZXJzZS5jbGFzc2VzLnVuaXZlcnNlY29ubmVjdGlvbi5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0dW5pdmVyc2UuY2xhc3Nlcy51bml2ZXJzZWNvbm5lY3Rpb24udHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7OztBQUFBLGlFQUFtRDtBQUVuRCxtR0FBMEU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