module Sfx { export class ActionCollection { public collection : Action [ ] = [ ] ; constructor ( private telemetry , private $q ) { } public runWithTelemetry ( action , source ) { if ( ! action . canRun || action . running ) { return this . $q . when ( true ) ; } return this . runInternal ( action , source ) ; } public get length ( ) : number { return this . collection . length ; } public add ( action ) { this . collection . push ( action ) ; } public get anyRunning ( ) : boolean { return _ . some ( this . collection , "running" ) ; } public get title ( ) : string { let runningAction = _ . find ( this . collection , a => a . running ) ; if ( runningAction ) { return runningAction . runningTitle ; } else { return "Actions" ; } } private runInternal ( action , source ) { let params = [ ( result ) => { this . telemetry . trackActionEvent ( action . name , source , true ) ; } , ( reason ) => { let result = reason && reason . statusText && reason . status && reason . status + ": " + reason . statusText || false ; this . telemetry . trackActionEvent ( action . name , source , result ) ; return this . $q . reject ( reason ) ; } ] ; return action . runWithCallbacks . apply ( action , params ) ; } } }