Last active
          September 19, 2015 08:45 
        
      - 
      
- 
        Save piotrze/06ef347965d3081a6f94 to your computer and use it in GitHub Desktop. 
    Emulate video tag api, for testing in phantomjs
  
        
  
    
      This file contains hidden or 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
    
  
  
    
  | React.createClass( | |
| componentDidMount: -> | |
| video = @getVideoElement() | |
| video.addEventListener "ended", @ended, false | |
| video.addEventListener "durationchange", @durationChange, false | |
| video.addEventListener "timeupdate", @timeUpdate, false | |
| video.play() | |
| componentWillUnmount: -> | |
| video = @getVideoElement() | |
| video.pause() | |
| video.removeEventListener("ended", @ended) | |
| video.removeEventListener("durationchange", @durationChange) | |
| video.removeEventListener("timeupdate", @timeUpdate) | |
| getVideoElement: -> | |
| # here could be more sofisticated check | |
| if window.RailsEnv == 'test' | |
| video = new VideoTagEmulator() | |
| else | |
| video = @getDOMNode() | |
| render: -> | |
| `<video src={this.props.url} poster={this.props.poster} ></video>` | |
  
    
      This file contains hidden or 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
    
  
  
    
  | class window.VideoTagEmulator | |
| speed: 0.1 | |
| constructor: -> | |
| @handlers = {} | |
| @duration = 5 | |
| @target = { | |
| duration: 0, | |
| currentTime: 0, | |
| ended: false | |
| } | |
| addEventListener: (eventType, handler) -> | |
| @handlers[eventType] = handler | |
| removeEventListener: -> | |
| @_stopLoop() | |
| @handlers = {} | |
| play: -> | |
| @_startLoop() | |
| pause: -> | |
| @_stopLoop() | |
| _playFrame: -> | |
| @target.currentTime += 1 | |
| @handlers.timeupdate(target: @target) | |
| if @target.currentTime == 1 | |
| @target.duration = @duration | |
| @handlers.durationchange(target: @target) | |
| else if @target.currentTime > @duration | |
| @_stopLoop() | |
| @target.ended = true | |
| @handlers.ended(target: @target) | |
| _startLoop: -> | |
| @timerId = window.setInterval( | |
| @_playFrame.bind(@), | |
| 1000 * @speed | |
| ) | |
| _stopLoop: -> | |
| window.clearInterval(@timerId) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment