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.

241 lines (240 loc) 6.65 kB
/*! * NENT 2022 */ import { Component, Element, h, Host, Method, Prop, State, } from '@stencil/core'; import { actionBus } from '../../services/actions'; import { debugIf } from '../../services/common/logging'; import { AudioType, AUDIO_TOPIC, DiscardStrategy, LoadStrategy, } from '../n-audio/services/interfaces'; import { audioState, onAudioStateChange, } from '../n-audio/services/state'; import { playedTrack } from '../n-audio/services/tracks'; /** * This element declares audio used within this \<n-view-prompt\> route. * The \<n-audio-action-sound-load\> instructs the player to load audio files * while defining play behaviors. * * The audio player will pre-load or play when the route is active. * The player manages them according to their settings. * * @system audio * @system actions */ export class AudioSoundLoad { constructor() { this.sent = false; /** * This is the loading strategy that determines * what it should do after the file is retrieved. */ this.mode = 'load'; /** * The discard strategy the player should use for this file. */ this.discard = 'route'; /** * If set, disables auto-rendering of this instance. * To fetch the contents change to false or remove * attribute. */ this.deferLoad = false; } /** * Get the underlying actionEvent instance. */ async getAction() { return { topic: AUDIO_TOPIC, command: this.mode || LoadStrategy.load, data: { trackId: this.trackId || this.src, src: this.src, discard: this.discard || DiscardStrategy.route, loop: false, type: AudioType.sound, mode: this.mode || LoadStrategy.load, }, }; } /** * Send this action to the the action messaging system. */ async sendAction(data) { const action = await this.getAction(); if (data) Object.assign(action.data, data); if (audioState.hasAudioComponent) { const trackPlayed = await playedTrack(this.trackId); if (this.mode == 'play' && trackPlayed) return; actionBus.emit(action.topic, action); } else { this.dispose = onAudioStateChange('hasAudioComponent', async (loaded) => { if (loaded) { actionBus.emit(action.topic, action); debugIf(audioState.debug, `n-audio-action-sound-load: load-action sent for ${this.trackId}`); this.sent = true; } }); } } async componentWillRender() { if (this.deferLoad || this.sent) return; await this.sendAction(); } render() { return h(Host, null); } disconnectedCallback() { var _a; (_a = this.dispose) === null || _a === void 0 ? void 0 : _a.call(this); } static get is() { return "n-audio-action-sound-load"; } static get encapsulation() { return "shadow"; } static get properties() { return { "src": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": true, "optional": false, "docs": { "tags": [], "text": "The path to the audio-file." }, "attribute": "src", "reflect": false }, "trackId": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": true, "optional": false, "docs": { "tags": [], "text": "The identifier for this music track" }, "attribute": "track-id", "reflect": false }, "mode": { "type": "string", "mutable": false, "complexType": { "original": "'queue' | 'play' | 'load'", "resolved": "\"load\" | \"play\" | \"queue\"", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "This is the loading strategy that determines\nwhat it should do after the file is retrieved." }, "attribute": "mode", "reflect": false, "defaultValue": "'load'" }, "discard": { "type": "string", "mutable": false, "complexType": { "original": "'route' | 'next' | 'none'", "resolved": "\"next\" | \"none\" | \"route\"", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "The discard strategy the player should use for this file." }, "attribute": "discard", "reflect": false, "defaultValue": "'route'" }, "deferLoad": { "type": "boolean", "mutable": true, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": false, "docs": { "tags": [], "text": "If set, disables auto-rendering of this instance.\nTo fetch the contents change to false or remove\nattribute." }, "attribute": "defer-load", "reflect": false, "defaultValue": "false" } }; } static get states() { return { "sent": {} }; } static get methods() { return { "getAction": { "complexType": { "signature": "() => Promise<EventAction<AudioInfo | AudioRequest | any>>", "parameters": [], "references": { "Promise": { "location": "global" }, "EventAction": { "location": "import", "path": "../../services/actions" }, "AudioInfo": { "location": "import", "path": "../n-audio/services/interfaces" }, "AudioRequest": { "location": "import", "path": "../n-audio/services/interfaces" } }, "return": "Promise<EventAction<any>>" }, "docs": { "text": "Get the underlying actionEvent instance.", "tags": [] } }, "sendAction": { "complexType": { "signature": "(data?: Record<string, any> | undefined) => Promise<void>", "parameters": [{ "tags": [], "text": "" }], "references": { "Promise": { "location": "global" }, "Record": { "location": "global" } }, "return": "Promise<void>" }, "docs": { "text": "Send this action to the the action messaging system.", "tags": [] } } }; } static get elementRef() { return "el"; } }