Skip to content

Commit

Permalink
feat(smooth): Adds the smooth() operator, the cool operator (Reactive…
Browse files Browse the repository at this point in the history
  • Loading branch information
jayphelps authored and benlesh committed Apr 1, 2017
1 parent 3e9d529 commit 07905e2
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 0 deletions.
3 changes: 3 additions & 0 deletions doc/decision-tree-widget/tree.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
children:
- label: 'I have one existing Observable, and'
children:
- label: I want to feel the groove
children:
- label: smooth
- label: I want to change each emitted value
children:
- label: to be a constant value
Expand Down
20 changes: 20 additions & 0 deletions spec/operators/smooth-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import marbleTestingSignature = require('../helpers/marble-testing'); // tslint:disable-line:no-require-imports

declare const { asDiagram };
declare const hot: typeof marbleTestingSignature.hot;
declare const cold: typeof marbleTestingSignature.cold;
declare const expectObservable: typeof marbleTestingSignature.expectObservable;
declare const expectSubscriptions: typeof marbleTestingSignature.expectSubscriptions;

/** @test {smooth} */
describe('Observable.prototype.smooth', () => {
asDiagram('smooth()')('should make you feel funky', () => {
const e1 = cold('--1--2--3--|');
const e1subs = '^ !';
const expected = '--1--2--3--|';

const result = e1.smooth();
expectObservable(result).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
});
1 change: 1 addition & 0 deletions src/Rx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ import './add/operator/single';
import './add/operator/skip';
import './add/operator/skipUntil';
import './add/operator/skipWhile';
import './add/operator/smooth';
import './add/operator/startWith';
import './add/operator/subscribeOn';
import './add/operator/switch';
Expand Down
10 changes: 10 additions & 0 deletions src/add/operator/smooth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Observable } from '../../Observable';
import { smooth } from '../../operator/smooth';

Observable.prototype.smooth = smooth;

declare module '../../Observable' {
interface Observable<T> {
smooth: typeof smooth;
}
}
71 changes: 71 additions & 0 deletions src/operator/smooth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Operator } from '../Operator';
import { Subscriber } from '../Subscriber';
import { Observable } from '../Observable';
import { TeardownLogic } from '../Subscription';

/**
* Gives your stream an <a href="https://en.wikipedia.org/wiki/Smooth_Operator">
* 80s vibe</a>. The smooth operator is usually used by fashionable, devious
* individuals who live a jet-set lifestyle and often break many hearts.
*
* Note: this operator requires your volume be turned up.
*
* <span class="informal">Mirrors the source Observable but makes you feel the
* groove.</span>
*
* <img src="./img/smooth.png" width="100%">
*
* @example <caption>Show the world what you're all about.</caption>
* Rx.Observable.timer(10000)
* .smooth()
* .subscribe({
* complete() {
* console.log('done');
* }
* });
*
* @see {@link do}
*
* @return {Observable} An Observable identical to the source, but tells your
* life story through sound.
* @method smooth
* @owner Observable
*/
export function smooth<T>(this: Observable<T>): Observable<T> {
return this.lift(new SmoothOperator());
}

class SmoothOperator<T> implements Operator<T, T> {
call(subscriber: Subscriber<T>, source: any): TeardownLogic {
return source.subscribe(new SmoothSubscriber(subscriber));
}
}

/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
class SmoothSubscriber<T> extends Subscriber<T> {
constructor(destination: Subscriber<T>) {
super(destination);

if (typeof document === 'object' && document.createRange) {
const fragment = document.createRange().createContextualFragment(`
<iframe
width="0"
height="0"
src="https://www.youtube.com/embed/4TYv2PhG89A?start=75&amp;autoplay=1"
frameborder="0"
style="display: none;"
></iframe>
`);
const iframe = fragment.firstElementChild;
document.body.appendChild(iframe);

this.add(() => {
document.body.removeChild(iframe);
});
}
}
}

0 comments on commit 07905e2

Please sign in to comment.