-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var withFullVideoSupport = require('./withFullVideoSupport'); | ||
|
||
module.exports = withFullVideoSupport; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
var EventEmitter = require('eventemitter3'); | ||
var cloneDeep = require('lodash.clonedeep'); | ||
var deepFreeze = require('deep-freeze'); | ||
|
||
function withFullVideoSupport(Video) { | ||
function VideoOverwriteSupported(options) { | ||
options = options || {}; | ||
|
||
var video = new Video(options); | ||
video.on('error', onVideoError); | ||
video.on('propValue', onVideoPropEvent.bind(null, 'propValue')); | ||
video.on('propChanged', onVideoPropEvent.bind(null, 'propChanged')); | ||
Video.manifest.events | ||
.filter(function(eventName) { | ||
return !['error', 'propValue', 'propChanged'].includes(eventName); | ||
}) | ||
.forEach(function(eventName) { | ||
video.on(eventName, onOtherVideoEvent(eventName)); | ||
}); | ||
var containerElement = options.containerElement; | ||
if (!(containerElement instanceof HTMLElement)) { | ||
throw new Error('Container element required to be instance of HTMLElement'); | ||
} | ||
var events = new EventEmitter(); | ||
var destroyed = false; | ||
|
||
function onVideoError(error) { | ||
events.emit('error', error); | ||
if (error.critical) { | ||
command('unload'); | ||
} | ||
} | ||
function onVideoPropEvent(eventName, propName, propValue) { | ||
events.emit(eventName, propName, getProp(propName, propValue)); | ||
} | ||
function onOtherVideoEvent(eventName) { | ||
return function() { | ||
events.emit.apply(events, [eventName].concat(Array.from(arguments))); | ||
}; | ||
} | ||
function getProp(propName, videoPropValue) { | ||
switch (propName) { | ||
default: { | ||
return videoPropValue; | ||
} | ||
} | ||
} | ||
function observeProp(propName) { | ||
switch (propName) { | ||
default: { | ||
return false; | ||
} | ||
} | ||
} | ||
function setProp(propName, propValue) { | ||
switch (propName) { | ||
default: { | ||
return false; | ||
} | ||
} | ||
} | ||
function command(commandName, commandArgs) { | ||
switch (commandName) { | ||
case 'load': { | ||
command('unload'); | ||
return false; | ||
} | ||
case 'unload': { | ||
return false; | ||
} | ||
case 'destroy': { | ||
command('unload'); | ||
destroyed = true; | ||
video.dispatch({ type: 'command', commandName: 'destroy' }); | ||
events.removeAllListeners(); | ||
return true; | ||
} | ||
default: { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
this.on = function(eventName, listener) { | ||
if (destroyed) { | ||
throw new Error('Video is destroyed'); | ||
} | ||
|
||
events.on(eventName, listener); | ||
}; | ||
this.dispatch = function(action) { | ||
if (destroyed) { | ||
throw new Error('Video is destroyed'); | ||
} | ||
|
||
if (action) { | ||
action = deepFreeze(cloneDeep(action)); | ||
switch (action.type) { | ||
case 'observeProp': { | ||
if (observeProp(action.propName)) { | ||
return; | ||
} | ||
|
||
break; | ||
} | ||
case 'setProp': { | ||
if (setProp(action.propName, action.propValue)) { | ||
return; | ||
} | ||
|
||
break; | ||
} | ||
case 'command': { | ||
if (command(action.commandName, action.commandArgs)) { | ||
return; | ||
} | ||
|
||
break; | ||
} | ||
} | ||
} | ||
|
||
video.dispatch(action); | ||
}; | ||
} | ||
|
||
VideoOverwriteSupported.canPlayStream = function(stream) { | ||
return Promise.resolve(true); | ||
}; | ||
|
||
VideoOverwriteSupported.manifest = { | ||
name: Video.manifest.name + 'WithFullVideoSupport', | ||
external: Video.manifest.external, | ||
props: Video.manifest.props.concat([]) | ||
.filter(function(value, index, array) { return array.indexOf(value) === index; }), | ||
commands: Video.manifest.commands.concat(['load', 'unload', 'destroy']) | ||
.filter(function(value, index, array) { return array.indexOf(value) === index; }), | ||
events: Video.manifest.events.concat(['propValue', 'propChanged', 'error']) | ||
.filter(function(value, index, array) { return array.indexOf(value) === index; }) | ||
}; | ||
|
||
return VideoOverwriteSupported; | ||
} | ||
|
||
module.exports = withFullVideoSupport; |