From 4c6513627bd8d0b8e99e778b80442a5b3addf464 Mon Sep 17 00:00:00 2001 From: Ben Lesh Date: Tue, 13 Oct 2015 15:52:25 -0700 Subject: [PATCH] perf(reduce): add fast-path for reducing over scalar observables --- src/observables/ScalarObservable.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/observables/ScalarObservable.ts b/src/observables/ScalarObservable.ts index 867c5cae69..20c4fdc89c 100644 --- a/src/observables/ScalarObservable.ts +++ b/src/observables/ScalarObservable.ts @@ -73,4 +73,16 @@ export default class ScalarObservable extends Observable { return new EmptyObservable(); } } + + reduce(project: (acc: R, x: T) => R, acc?: R): Observable { + if (typeof acc === 'undefined') { + return this; + } + let result = tryCatch(project)(acc, this.value); + if (result === errorObject) { + return new ErrorObservable(errorObject.e); + } else { + return new ScalarObservable(result); + } + } }