all files / src/core/util/ cache.js

67.86% Statements 19/28
61.11% Branches 11/18
69.23% Functions 9/13
67.86% Lines 19/28
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168                                                              12×             18×               12×         11×               25×     25×                               10×                                                 10×                     56×                                                                                      
import Config from '../Config';
import StreamManager from '../stream/StreamManager';
import PeerConnectionManager from '../webrtc/PeerConnectionManager';
import Webcom from 'webcom/webcom';
 
/**
 * singleton
 * @type {CacheManager}
 * @private
 */
let _cache = null;
 
/**
 * Singleton to handle global references
 * @access protected
 */
class CacheManager {
	/**
	 * Construct singleton
	 * @return {CacheManager}
	 */
	constructor() {
		Eif(!_cache) {
			_cache = this;
		}
		return _cache;
	}
	/**
	 * The base reference
	 * @param {Webcom} url the reference or its url
	 */
	set base(url) {
		/**
		 * @ignore
		 */
		this._base = url instanceof Webcom ? url : new Webcom(url);
	}
	/**
	 * The base reference
	 * @type {Webcom}
	 */
	get base() {
		return this._base;
	}
 
	/**
	 * The connected user
	 * @param {Config} config the connected user
	 */
	set config(config) {
		if(!this._config) {
			/**
			 * @ignore
			 */
			this._config = new Config(config);
		} else {
			this._config.assign(config);
		}
	}
	/**
	 * The connected user
	 * @type {Config}
	 */
	get config() {
		Iif(!this._config) {
			this._config = new Config();
		}
		return this._config;
	}
	/**
	 * The connected user
	 * @param {User} user the connected user
	 */
	set user(user) {
		/**
		 * @ignore
		 */
		this._user = user;
	}
	/**
	 * The connected user
	 * @type {User}
	 */
	get user() {
		return this._user;
	}
	/**
	 * The current device
	 * @param {string} device the current device
	 */
	set device(device) {
		/**
		 * @ignore
		 */
		this._device = device;
	}
	/**
	 * The current device
	 * @type {string}
	 */
	get device() {
		return this._device;
	}
 
	/**
	 * The log level (DEBUG, INFO, WARN, ERROR)
	 * @type {string}
	 */
	set logLevel(level) {
		if(/^DEBUG|INFO|WARN|ERROR$/i.test(level)) {
			/**
			 * @ignore
			 */
			this._logLevel = level;
		} else Eif(level) {
			throw new Error('Unsupported log level (DEBUG, INFO, WARN, ERROR)');
		}
	}
	/**
	 * The log level (DEBUG, INFO, WARN, ERROR)
	 * @returns {string}
	 */
	get logLevel() {
		return this._logLevel || 'ERROR';
	}
 
	/**
	 * Streams manager
	 * @returns {StreamManager}
	 */
	get streams() {
		if(!this._streams) {
			/**
			 * @ignore
			 */
			this._streams = new StreamManager();
		}
		return this._streams;
	}
 
	/**
	 * PeerConnections manager
	 * @returns {PeerConnectionManager}
	 */
	get peerConnections() {
		if(!this._peerConnections) {
			/**
			 * @ignore
			 */
			this._peerConnections = new PeerConnectionManager();
		}
		return this._peerConnections;
	}
}
 
/**
 * The singleton instance
 * @ignore
 * @type {CacheManager}
 */
const cache = new CacheManager();
 
/**
 * Singleton for shared references
 * @access protected
 */
export default cache;