Skip to content

Commit

Permalink
feat(cache): add cache operator
Browse files Browse the repository at this point in the history
  • Loading branch information
benlesh committed Feb 10, 2016
1 parent b35564e commit 4308a04
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 0 deletions.
125 changes: 125 additions & 0 deletions spec/operators/cache-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/* globals describe, it, expect, expectObservable, hot, rxTestScheduler */
var Rx = require('../../dist/cjs/Rx');

var asap = Rx.Scheduler.asap;
var Observable = Rx.Observable;

describe('Observable.prototype.cache', function () {
it('should replay values upon subscription', function () {
var s1 = hot('---^---a---b---c---| ').cache();
var expected1 = '----a---b---c---| ';
var expected2 = ' (abc|)';
var t = time( '----------------|');

expectObservable(s1).toBe(expected1);

rxTestScheduler.schedule(function () {
expectObservable(s1).toBe(expected2);
}, t);
});

it('should replay values and error', function () {
var s1 = hot('---^---a---b---c---# ').cache();
var expected1 = '----a---b---c---# ';
var expected2 = ' (abc#)';
var t = time( '----------------|');

expectObservable(s1).toBe(expected1);

rxTestScheduler.schedule(function () {
expectObservable(s1).toBe(expected2);
}, t);
});

it('should replay values and and share', function () {
var s1 = hot('---^---a---b---c------------d--e--f-|').cache();
var expected1 = '----a---b---c------------d--e--f-|';
var expected2 = ' (abc)----d--e--f-|';
var t = time( '----------------|');

expectObservable(s1).toBe(expected1);

rxTestScheduler.schedule(function () {
expectObservable(s1).toBe(expected2);
}, t);
});

it('should have a bufferCount that limits the replay test 1', function () {
var s1 = hot('---^---a---b---c------------d--e--f-|').cache(1);
var expected1 = '----a---b---c------------d--e--f-|';
var expected2 = ' c--------d--e--f-|';
var t = time( '----------------|');

expectObservable(s1).toBe(expected1);

rxTestScheduler.schedule(function () {
expectObservable(s1).toBe(expected2);
}, t);
});

it('should have a bufferCount that limits the replay test 2', function () {
var s1 = hot('---^---a---b---c------------d--e--f-|').cache(2);
var expected1 = '----a---b---c------------d--e--f-|';
var expected2 = ' (bc)-----d--e--f-|';
var t = time( '----------------|');

expectObservable(s1).toBe(expected1);

rxTestScheduler.schedule(function () {
expectObservable(s1).toBe(expected2);
}, t);
});

it('should accept a windowTime that limits the replay', function () {
var w = time( '----------|');
var s1 = hot('---^---a---b---c------------d--e--f-|').cache(Number.POSITIVE_INFINITY, w, rxTestScheduler);
var expected1 = '----a---b---c------------d--e--f-|';
var expected2 = ' (bc)-----d--e--f-|';
var t = time( '----------------|');

expectObservable(s1).toBe(expected1);

rxTestScheduler.schedule(function () {
expectObservable(s1).toBe(expected2);
}, t);
});

it('should handle empty', function () {
var s1 = cold('|').cache();
var expected1 = '|';
var expected2 = ' |';
var t = time( '----------------|');

expectObservable(s1).toBe(expected1);

rxTestScheduler.schedule(function () {
expectObservable(s1).toBe(expected2);
}, t);
});

it('should handle throw', function () {
var s1 = cold('#').cache();
var expected1 = '#';
var expected2 = ' #';
var t = time( '----------------|');

expectObservable(s1).toBe(expected1);

rxTestScheduler.schedule(function () {
expectObservable(s1).toBe(expected2);
}, t);
});

it('should handle never', function () {
var s1 = cold('-').cache();
var expected1 = '-';
var expected2 = ' -';
var t = time( '----------------|');

expectObservable(s1).toBe(expected1);

rxTestScheduler.schedule(function () {
expectObservable(s1).toBe(expected2);
}, t);
});
});
1 change: 1 addition & 0 deletions src/Observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ export class Observable<T> implements CoreOperators<T> {
bufferTime: BufferTimeSignature<T>;
bufferToggle: BufferToggleSignature<T>;
bufferWhen: BufferWhenSignature<T>;
cache: (bufferSize?: number, windowTime?: number, scheduler?: Scheduler) => Observable<T>;
catch: (selector: (err: any, source: Observable<T>, caught: Observable<any>) => Observable<any>) => Observable<T>;
combineAll: <R>(project?: (...values: Array<any>) => R) => Observable<R>;
combineLatest: CombineLatestSignature<T>;
Expand Down
1 change: 1 addition & 0 deletions src/Rx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import './add/operator/bufferCount';
import './add/operator/bufferTime';
import './add/operator/bufferToggle';
import './add/operator/bufferWhen';
import './add/operator/cache';
import './add/operator/catch';
import './add/operator/combineAll';
import './add/operator/combineLatest';
Expand Down
7 changes: 7 additions & 0 deletions src/add/operator/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

import {Observable} from '../../Observable';
import {cache} from '../../operator/cache';

Observable.prototype.cache = cache;

export var _void: void;
10 changes: 10 additions & 0 deletions src/operator/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {Observable} from '../Observable';
import {publishReplay} from './publishReplay';
import {Scheduler} from '../Scheduler';
import {ConnectableObservable} from '../observable/ConnectableObservable';

export function cache<T>(bufferSize: number = Number.POSITIVE_INFINITY,
windowTime: number = Number.POSITIVE_INFINITY,
scheduler?: Scheduler): Observable<T> {
return (<ConnectableObservable<any>>publishReplay.call(this, bufferSize, windowTime, scheduler)).refCount();
}

0 comments on commit 4308a04

Please sign in to comment.