|
| 1 | +/** |
| 2 | + * The copyright in this software is being made available under the BSD License, |
| 3 | + * included below. This software may be subject to other third party and contributor |
| 4 | + * rights, including patent rights, and no such rights are granted under this license. |
| 5 | + * |
| 6 | + * Copyright (c) 2013, Dash Industry Forum. |
| 7 | + * All rights reserved. |
| 8 | + * |
| 9 | + * Redistribution and use in source and binary forms, with or without modification, |
| 10 | + * are permitted provided that the following conditions are met: |
| 11 | + * * Redistributions of source code must retain the above copyright notice, this |
| 12 | + * list of conditions and the following disclaimer. |
| 13 | + * * Redistributions in binary form must reproduce the above copyright notice, |
| 14 | + * this list of conditions and the following disclaimer in the documentation and/or |
| 15 | + * other materials provided with the distribution. |
| 16 | + * * Neither the name of Dash Industry Forum nor the names of its |
| 17 | + * contributors may be used to endorse or promote products derived from this software |
| 18 | + * without specific prior written permission. |
| 19 | + * |
| 20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY |
| 21 | + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 22 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 23 | + * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 24 | + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 25 | + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 27 | + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | + * POSSIBILITY OF SUCH DAMAGE. |
| 30 | + */ |
| 31 | + |
| 32 | +/** |
| 33 | + * @classdesc Similar to Set<TextTrackCue>, but using the {@link areCuesEqual} function to compare cues, instead of ===. |
| 34 | + * @ignore |
| 35 | + */ |
| 36 | +class CueSet { |
| 37 | + |
| 38 | + /** |
| 39 | + * The cues contained in the set, grouped by start time. |
| 40 | + * |
| 41 | + * @instance |
| 42 | + * @type {Map<number, TextTrackCue[]>} |
| 43 | + * @name CueSet.cues |
| 44 | + * @memberof CueSet |
| 45 | + */ |
| 46 | + |
| 47 | + /** |
| 48 | + * Creates a new CueSet instance. |
| 49 | + * |
| 50 | + * @param {ArrayLike<TextTrackCue>} [initialCues] - Optional initial cues to add to the set. |
| 51 | + */ |
| 52 | + constructor(initialCues) { |
| 53 | + this.cues = new Map(); |
| 54 | + if (initialCues) { |
| 55 | + for (const cue of initialCues) { |
| 56 | + this.addCue(cue); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Checks if a cue is already in the set. |
| 63 | + * |
| 64 | + * @param {TextTrackCue} cue |
| 65 | + * @returns {boolean} |
| 66 | + */ |
| 67 | + hasCue(cue) { |
| 68 | + const cuesWithSameStartTime = this.cues.get(cue.startTime); |
| 69 | + return cuesWithSameStartTime && cuesWithSameStartTime.some(c => areCuesEqual(c, cue)); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Adds a cue to the set, if it is not already present. |
| 74 | + * |
| 75 | + * @param {TextTrackCue} cue |
| 76 | + */ |
| 77 | + addCue(cue) { |
| 78 | + const cuesWithSameStartTime = this.cues.get(cue.startTime); |
| 79 | + |
| 80 | + if (!cuesWithSameStartTime) { |
| 81 | + this.cues.set(cue.startTime, [cue]); |
| 82 | + } else if (!this.hasCue(cue)) { |
| 83 | + cuesWithSameStartTime.push(cue); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Compares two cues for equality. |
| 90 | + * |
| 91 | + * @param {TextTrackCue} cue1 |
| 92 | + * @param {TextTrackCue} cue2 |
| 93 | + * @returns {boolean} |
| 94 | + * @private |
| 95 | + */ |
| 96 | +function areCuesEqual(cue1, cue2) { |
| 97 | + if (cue1.startTime !== cue2.startTime || |
| 98 | + cue1.endTime !== cue2.endTime) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + if (cue1 instanceof VTTCue && cue2 instanceof VTTCue) { |
| 102 | + return cue1.text === cue2.text; |
| 103 | + } |
| 104 | + return false; |
| 105 | +} |
| 106 | + |
| 107 | +export { CueSet }; |
0 commit comments