Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(smooth): Adds the smooth() operator, the cool operator #2508

Merged
merged 1 commit into from
Apr 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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} */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this comment.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm the other tests have them. Not sure what it's for. figured it was some sort of build time thing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just say it's critical.

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);
});
}
}
}