import * as PIXI from "pixi.js" ; export abstract class Mediator < T = any > { view : T ; abstract initialize ? ( ) ; abstract destroy ? ( ) ; } export function mediate ( mediatorClass ) { return function ( target ) { let original = target ; function construct ( constructor , args ) { let c = function ( ) { return constructor . apply ( this , args ) ; } ; c . prototype = constructor . prototype ; let instance = new c ( ) ; let mediator = new mediatorClass ( ) ; mediator . view = instance ; if ( instance instanceof PIXI . DisplayObject ) { instance . on ( "added" , ( ) => { if ( mediator . initialize ) { mediator . initialize ( ) ; } } ) ; instance . on ( "removed" , ( ) => { if ( mediator . destroy ) { mediator . destroy ( ) ; } } ) ; } else if ( mediator . initialize ) { mediator . initialize ( ) ; } Object . defineProperty ( instance , "$mediator" , { value : mediator , enumerable : false , configurable : true , writable : true } ) ; return instance ; } let f = function ( ... args ) { return construct ( original , args ) ; } ; f . prototype = original . prototype ; return f ; } ; } export function action ( name ) { return function ( target , key , descriptor ) { if ( descriptor === undefined ) { descriptor = Object . getOwnPropertyDescriptor ( target , key ) ; } var originalMethod = descriptor . value as any ; ( < any > descriptor ) . value = function ( ) { var result = originalMethod . apply ( this , arguments ) ; if ( this . $mediator && this . $mediator [ name ] ) { this . $mediator [ name ] . apply ( this . $mediator , arguments ) ; } return result ; } ; return descriptor ; } ; }