Ejemplo n.º 1
0
export function invokeCommand<TObj extends ReactiveObject, TArg, TResult>(source: Observable<TArg>, obj: TObj, command: string | ReactiveCommand<TArg, TResult>): Observable<TResult> {
    var commandObservable: Observable<ReactiveCommand<TArg, TResult>>;
    var canExecute: Observable<boolean>;
    var isExecuting: Observable<boolean>;
    if (typeof command === "string") {
        // Make sure that the current command is observed
        commandObservable = obj.whenSingle(command, true).map(e => e.newPropertyValue);
        canExecute = commandObservable.map(c => c.canExecute).switch();
    } else {
        commandObservable = Observable.of(command);
        canExecute = command.canExecute;
    }
    var results = source
        .withLatestFrom(commandObservable, canExecute, (v1, command, canExecute) => {
            return {
                canExecute,
                command,
                observedValue: v1
            };
        })
        .filter(o => o.canExecute && o.command != null)
        .distinctUntilChanged()
        .flatMap(o => {
            return o.command.execute(o.observedValue);
        });
    return results;
}