Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore/use vhs utils #295

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 13 additions & 78 deletions lib/utils/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,77 +9,12 @@
*/
'use strict';

var Stream = function() {
this.init = function() {
var listeners = {};
/**
* Add a listener for a specified event type.
* @param type {string} the event name
* @param listener {function} the callback to be invoked when an event of
* the specified type occurs
*/
this.on = function(type, listener) {
if (!listeners[type]) {
listeners[type] = [];
}
listeners[type] = listeners[type].concat(listener);
};
/**
* Remove a listener for a specified event type.
* @param type {string} the event name
* @param listener {function} a function previously registered for this
* type of event through `on`
*/
this.off = function(type, listener) {
var index;
if (!listeners[type]) {
return false;
}
index = listeners[type].indexOf(listener);
listeners[type] = listeners[type].slice();
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 type {string} the event name
*/
this.trigger = function(type) {
var callbacks, i, length, args;
callbacks = 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 = [];
i = arguments.length;
for (i = 1; i < arguments.length; ++i) {
args.push(arguments[i]);
}
length = callbacks.length;
for (i = 0; i < length; ++i) {
callbacks[i].apply(this, args);
}
}
};
/**
* Destroys the stream and cleans up.
*/
this.dispose = function() {
listeners = {};
};
};
};
var Stream = require('@videojs/vhs-utils/dist/stream.cjs.js');

var MuxStream = function() {};

MuxStream.prototype = new Stream();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Object.create(Stream.prototype) would be preferable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potentially, setting the constructor prop could be done in the same call to Object.create, but we'd want to double check that won't be an issue on IE11: https://github.com/kaelzhang/node-util-inherits/blob/master/index.js#L10-L17

MuxStream.prototype.init = MuxStream.prototype.constructor;

/**
* Forwards all `data` events on this stream to the destination stream. The
Expand All @@ -90,7 +25,7 @@ var Stream = function() {
* when the current stream emits a 'done' event
* @see http://nodejs.org/api/stream.html#stream_readable_pipe_destination_options
*/
Stream.prototype.pipe = function(destination) {
MuxStream.prototype.pipe = function(destination) {
this.on('data', function(data) {
destination.push(data);
});
Expand Down Expand Up @@ -118,24 +53,24 @@ Stream.prototype.pipe = function(destination) {
// actual work. These are provided by the prototype as a sort of no-op
// implementation so that we don't have to check for their existence in the
// `pipe` function above.
Stream.prototype.push = function(data) {
MuxStream.prototype.push = function(data) {
this.trigger('data', data);
};

Stream.prototype.flush = function(flushSource) {
MuxStream.prototype.flush = function(flushSource) {
this.trigger('done', flushSource);
};

Stream.prototype.partialFlush = function(flushSource) {
MuxStream.prototype.partialFlush = function(flushSource) {
this.trigger('partialdone', flushSource);
};

Stream.prototype.endTimeline = function(flushSource) {
MuxStream.prototype.endTimeline = function(flushSource) {
this.trigger('endedtimeline', flushSource);
};

Stream.prototype.reset = function(flushSource) {
MuxStream.prototype.reset = function(flushSource) {
this.trigger('reset', flushSource);
};

module.exports = Stream;
module.exports = MuxStream;
56 changes: 52 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mux.js",
"version": "5.2.0",
"version": "5.2.1-0",
"description": "A collection of lightweight utilities for inspecting and manipulating video container formats.",
"repository": {
"type": "git",
Expand Down Expand Up @@ -78,5 +78,8 @@
"uglify-js": "^2.6.2",
"watchify": "^3.6.1",
"webworkify": "^1.0.2"
},
"dependencies": {
"@videojs/vhs-utils": "github:videojs/vhs-utils#force-dist"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first dependency. It was nice that this lib didn't have any previously.

}
}