UNPKG

@nent/core

Version:

Functional elements to add routing, data-binding, dynamic HTML, declarative actions, audio, video, and so much more. Supercharge static HTML files into web apps without script or builds.

92 lines (91 loc) 2.81 kB
/*! * NENT 2022 */ import { debugIf } from '../../../services/common'; import { VIDEO_COMMANDS, VIDEO_EVENTS, VIDEO_TOPIC, } from './interfaces'; /* It listens to the `VIDEO_TOPIC` topic on the `actionBus` and executes the command on the `childVideo` element */ export class VideoActionListener { constructor(childVideo, eventBus, actionBus, debug) { this.childVideo = childVideo; this.eventBus = eventBus; this.actionBus = actionBus; this.debug = debug; this.disposeHandle = this.actionBus.on(VIDEO_TOPIC, async (ev) => { debugIf(this.debug, `n-video actions: event received ${ev.topic}:${ev.command}`); await this.commandReceived(ev.command, ev.data); }); } async commandReceived(command, data) { switch (command) { case VIDEO_COMMANDS.Play: { await this.play(); break; } case VIDEO_COMMANDS.Pause: { this.pause(); break; } case VIDEO_COMMANDS.Resume: { await this.resume(); break; } case VIDEO_COMMANDS.Mute: { this.mute(data.value); break; } default: } } /** * If the child video exists, set the child video's muted property to the value of the muted * parameter, and if muted is true, emit the Muted event, otherwise emit the Unmuted event * @param {boolean} muted - boolean - true if you want to mute the video, false if you want to unmute * it. * @returns the value of the childVideo.muted property. */ mute(muted) { if (!this.childVideo) { return; } this.childVideo.muted = muted; if (muted) { this.eventBus.emit(VIDEO_EVENTS.Muted); } else { this.eventBus.emit(VIDEO_EVENTS.Unmuted); } } /** * It plays the video. */ async play() { var _a, _b; await ((_b = (_a = this.childVideo) === null || _a === void 0 ? void 0 : _a.play) === null || _b === void 0 ? void 0 : _b.call(this)); this.eventBus.emit(VIDEO_EVENTS.Played); } /** * If the childVideo object has a pause method, call it */ pause() { var _a, _b; (_b = (_a = this.childVideo) === null || _a === void 0 ? void 0 : _a.pause) === null || _b === void 0 ? void 0 : _b.call(this); this.eventBus.emit(VIDEO_EVENTS.Paused); } /** * It resumes the video. */ async resume() { var _a, _b; await ((_b = (_a = this.childVideo) === null || _a === void 0 ? void 0 : _a.play) === null || _b === void 0 ? void 0 : _b.call(this)); this.eventBus.emit(VIDEO_EVENTS.Resumed); } /** * If the `disposeHandle` property is not null, then call the function that it points to, passing in * the current instance of the class */ destroy() { var _a; (_a = this.disposeHandle) === null || _a === void 0 ? void 0 : _a.call(this); } }