import { Observer } from './entities' ; import { TaskQueue } from './helpers' ; export default class Engine { private _observers : Array < Observer > ; private _intervalId : number ; private _taskQueue : TaskQueue ; constructor ( taskQueue ) { this . _observers = [ ] ; this . _intervalId = null ; this . _taskQueue = taskQueue ; } addObserver ( observer ) { this . _observers . push ( observer ) ; } removeObserver ( observer ) { this . _observers = this . _observers . filter ( o => o !== observer ) ; } loop ( ) { if ( ! this . _taskQueue . isTaskAvailable ) return ; this . _observers . forEach ( o => o . update ( this . _taskQueue . currentTask ) ) ; this . _taskQueue . endCurrentTask ( ) ; } start ( ) { this . _intervalId = window . setInterval ( this . loop . bind ( this ) , 1000 / 60 ) ; } stop ( ) { clearInterval ( this . _intervalId ) ; } init ( ) { this . start ( ) ; } }