import m from 'mithril' ; export abstract class AbstractComponent < A > implements m . Component < A > { protected timeoutStack : number [ ] = [ ] ; protected attrs : A = { } as A ; protected prevAttrs : A ; public abstract view ( vnode ) ; public abstract getDefaultAttrs ( ) ; public oninit ( vnode ) { vnode . attrs = vnode . attrs || { } as A ; this . setAttrs ( vnode ) ; } public onbeforeupdate ( vnode , prev ) { this . setAttrs ( vnode ) ; this . prevAttrs = prev . attrs ; } private setAttrs ( vnode ) { vnode . attrs = this . getAttrs ( vnode . attrs ) ; this . attrs = vnode . attrs ; } private getAttrs ( attrs ) { return { ... this . getDefaultAttrs ( ) as Object , ... attrs as Object } as A ; } protected setTimeout = ( callback , timeout ? ) => { const handle = window . setTimeout ( callback , timeout ) ; this . timeoutStack . push ( handle ) ; return ( ) => window . clearTimeout ( handle ) ; } ; protected clearTimeouts = ( ) => { if ( this . timeoutStack . length ) { this . timeoutStack . map ( ( timeout ) => clearTimeout ( timeout ) ) ; this . timeoutStack = [ ] ; } } ; }