import { ProgressBarState } from "../ProgressBar" ; import { Token , TokenDict } from "../token" ; import RenderTrigger from "./RenderTrigger" ; export abstract class ProgressBarRenderer { private readonly _triggers : RenderTrigger [ ] = [ ] ; public get triggers ( ) { return Object . freeze ( Array . of ( ... this . _triggers ) ) ; } protected constructor ( public readonly template , public readonly tokens ) { } public abstract render ( bar ) ; public makeString ( bar ) { return Object . entries ( this . tokens ) . reduce ( ( str , [ key , token ] ) => str . split ( `$${ key }$` ) . join ( token ( bar ) ) , this . template ) ; } public attach ( attacher ) { const trigger = attacher ( this ) ; this . _triggers . push ( trigger ) ; trigger . init ( ) ; return this ; } public detach ( rendererTrigger ) { const index = this . triggers . indexOf ( rendererTrigger ) ; if ( index === - 1 ) throw new Error ( 'RenderTrigger not found' ) ; this . _triggers . splice ( index , 1 ) . forEach ( it => it . finalize ( ) ) ; } } export abstract class MultiProgressBarRenderer extends ProgressBarRenderer { protected constructor ( template , tokens , protected readonly bars ) { super ( template , tokens ) ; } indexOfBar ( bar ) { return this . bars . indexOf ( bar ) ; } }