Home Reference Source

src/conferences/ConferenceLog.js

import {bindMethods} from '../utils/bindMethods';
import {ConferenceLogEntry} from './ConferenceLogEntry';
import {EventEmitter} from '../eventemitter';

/**
 * This class allows access to the conference log. To obtain an instance of the
 * class @link {ConferenceManager#getConferenceLog} method must be used.
 */
export class ConferenceLog {
	/**
	 * @private
	 */
	constructor(conferenceLogService) {
		/** @private */
		this.conferenceLogService = conferenceLogService;
		/** @private */
		this.entries = [];
		/** @private */
		this.timestampOfTheLastEntryRecovered = null;
		/**
		 * - "create" event every time a new entry is prepended
		 * @type {EventEmitter}
		 */
		this.emitter = new EventEmitter();
		bindMethods(this, [
			'onCreate',
		]);
		this.conferenceLogService.emitter.on('create', this.onCreate);
	}

	/**
	 * @private
	 */
	onCreate(newConferenceLog) {
		const conferenceLogEntry = ConferenceLogEntry.of(newConferenceLog);
		this.entries.unshift(conferenceLogEntry);
		this.emitter.emit('create', conferenceLogEntry);
	}

	/**
	 * Fetches more conference log entries.
	 * @param {number} [limit=10] The number of new entries to be fetched at most
	 * @returns {Promise<ConferenceLogEntry>} When resolved, the update was completed and get the new
	 * conference log objects
	 */
	async fetch(limit = 10) {
		if (!this.timestampOfTheLastEntryRecovered) {
			this.timestampOfTheLastEntryRecovered = Date.now();
		}
		const res = await this.conferenceLogService.fetch(limit, this.timestampOfTheLastEntryRecovered);
		let conferenceLogEntries = res.map(conference => ConferenceLogEntry.of(conference));
		this.entries.push(...conferenceLogEntries);
		this.timestampOfTheLastEntryRecovered = this.getTimestampOfTheLastEntry();
		this.emitter.emit('fetch');
		return conferenceLogEntries;
	}

	/**
	 * @private
	 */
	getTimestampOfTheLastEntry() {
		return this.entries.slice(-1).length ? this.entries.slice(-1).pop().getStartTimestamp() : undefined;
	}

	/**
	 * Allows access to the list of conferences
	 * @return {ConferenceLogEntry[]}
	 */
	getEntries() {
		return this.entries;
	}
}