Skip to content

Commit

Permalink
Defer the implementation of functions in the tech to source handlers …
Browse files Browse the repository at this point in the history
…if they provide them
  • Loading branch information
jrivera committed Oct 28, 2015
1 parent 51f1863 commit 4b67a05
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions src/js/tech/tech.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,16 +568,36 @@ Tech.withSourceHandlers = function(_Tech){
return '';
};

let originalSeekable = _Tech.prototype.seekable;

// when a source handler is registered, prefer its implementation of
// seekable when present.
_Tech.prototype.seekable = function() {
if (this.sourceHandler_ && this.sourceHandler_.seekable) {
return this.sourceHandler_.seekable();
}
return originalSeekable.call(this);
};
/*
* When using a source handler, prefer its implementation of
* any function normally provided by the tech.
*/
let techProto = _Tech.prototype;
let deferrable = [
'seekable',
'duration'
];

Object.keys(techProto)
.filter(function onlyFunctions(key) {
return typeof this[key] === 'function';
}, techProto)
.filter(function onlyDeferrableKeys(key) {
return Boolean(deferrable.indexOf(key) + 1);
}, techProto)
.map(function getFunctions(key) {
return this[key];
}, techProto)
.forEach(function wrapFunctions(originalFn) {
let fnName = originalFn.name;

this[fnName] = function() {
if (this.sourceHandler_ && this.sourceHandler_[fnName]) {
return this.sourceHandler_[fnName].apply(this.sourceHandler_, arguments);
}
return originalFn.apply(this, arguments);
};
}, techProto);

/*
* Create a function for setting the source using a source object
Expand Down

0 comments on commit 4b67a05

Please sign in to comment.