Browse Source

remove unnecessary stream class (#1172)

pull/6/head
Matthew Neil 8 years ago
committed by GitHub
parent
commit
611fe98307
  1. 11
      src/playlist-loader.js
  2. 100
      src/stream.js

11
src/playlist-loader.js

@ -6,9 +6,8 @@
* *
*/ */
import resolveUrl from './resolve-url'; import resolveUrl from './resolve-url';
import {mergeOptions} from 'video.js';
import { mergeOptions, EventTarget } from 'video.js';
import { isEnabled } from './playlist.js'; import { isEnabled } from './playlist.js';
import Stream from './stream';
import m3u8 from 'm3u8-parser'; import m3u8 from 'm3u8-parser';
import window from 'global/window'; import window from 'global/window';
@ -121,7 +120,6 @@ const PlaylistLoader = function(srcUrl, hls, withCredentials) {
/* eslint-disable consistent-this */ /* eslint-disable consistent-this */
let loader = this; let loader = this;
/* eslint-enable consistent-this */ /* eslint-enable consistent-this */
let dispose;
let mediaUpdateTimeout; let mediaUpdateTimeout;
let request; let request;
let playlistRequestError; let playlistRequestError;
@ -203,16 +201,13 @@ const PlaylistLoader = function(srcUrl, hls, withCredentials) {
// initialize the loader state // initialize the loader state
loader.state = 'HAVE_NOTHING'; loader.state = 'HAVE_NOTHING';
// capture the prototype dispose function
dispose = this.dispose;
/** /**
* Abort any outstanding work and clean up. * Abort any outstanding work and clean up.
*/ */
loader.dispose = function() { loader.dispose = function() {
loader.stopRequest(); loader.stopRequest();
window.clearTimeout(mediaUpdateTimeout); window.clearTimeout(mediaUpdateTimeout);
dispose.call(this);
loader.off();
}; };
loader.stopRequest = () => { loader.stopRequest = () => {
@ -563,6 +558,6 @@ const PlaylistLoader = function(srcUrl, hls, withCredentials) {
}; };
}; };
PlaylistLoader.prototype = new Stream();
PlaylistLoader.prototype = new EventTarget();
export default PlaylistLoader; export default PlaylistLoader;

100
src/stream.js

@ -1,100 +0,0 @@
/**
* @file stream.js
*/
/**
* A lightweight readable stream implemention that handles event dispatching.
*
* @class Stream
*/
export default class Stream {
constructor() {
this.listeners = {};
}
/**
* Add a listener for a specified event type.
*
* @param {String} type the event name
* @param {Function} listener the callback to be invoked when an event of
* the specified type occurs
*/
on(type, listener) {
if (!this.listeners[type]) {
this.listeners[type] = [];
}
this.listeners[type].push(listener);
}
/**
* Remove a listener for a specified event type.
*
* @param {String} type the event name
* @param {Function} listener a function previously registered for this
* type of event through `on`
* @return {Boolean} if we could turn it off or not
*/
off(type, listener) {
let index;
if (!this.listeners[type]) {
return false;
}
index = this.listeners[type].indexOf(listener);
this.listeners[type].splice(index, 1);
return index > -1;
}
/**
* Trigger an event of the specified type on this stream. Any additional
* arguments to this function are passed as parameters to event listeners.
*
* @param {String} type the event name
*/
trigger(type) {
let callbacks;
let i;
let length;
let args;
callbacks = this.listeners[type];
if (!callbacks) {
return;
}
// Slicing the arguments on every invocation of this method
// can add a significant amount of overhead. Avoid the
// intermediate object creation for the common case of a
// single callback argument
if (arguments.length === 2) {
length = callbacks.length;
for (i = 0; i < length; ++i) {
callbacks[i].call(this, arguments[1]);
}
} else {
args = Array.prototype.slice.call(arguments, 1);
length = callbacks.length;
for (i = 0; i < length; ++i) {
callbacks[i].apply(this, args);
}
}
}
/**
* Destroys the stream and cleans up.
*/
dispose() {
this.listeners = {};
}
/**
* Forwards all `data` events on this stream to the destination stream. The
* destination stream should provide a method `push` to receive the data
* events as they arrive.
*
* @param {Stream} destination the stream that will receive all `data` events
* @see http://nodejs.org/api/stream.html#stream_readable_pipe_destination_options
*/
pipe(destination) {
this.on('data', function(data) {
destination.push(data);
});
}
}
Loading…
Cancel
Save