Skip to content

Commit

Permalink
feat(symbol exports): symbols now also exported without $$ prefix t…
Browse files Browse the repository at this point in the history
…o work with Babel UMD exporting (#2435)

* fix(symbol/observable): will be exported as observable to better support Babel UMD and others

To migrate the code follow the example bellow:

Before:

import { $$observable } from 'rxjs/symbol/observable';

After:

import { observable } from 'rxjs/symbol/observable';

Closes #2415

* fix(symbol/iterator): will be exported as iterator to better support Babel UMD and others

To migrate the code follow the example bellow:

Before:

import { $$iterator } from 'rxjs/symbol/iterator';

After:

import { iterator } from 'rxjs/symbol/iterator';

* fix(symbol/rxSubscriber): will be exported as rxSubscriber to better support Babel UMD and others

To migrate the code follow the example bellow:

Before:

import { $$rxSubscriber } from 'rxjs/symbol/rxSubscriber';

After:

import { rxSubscriber } from 'rxjs/symbol/rxSubscriber';
  • Loading branch information
DzmitryShylovich authored and benlesh committed Apr 3, 2017
1 parent 524259a commit 747bef6
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/Observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { root } from './util/root';
import { toSubscriber } from './util/toSubscriber';
import { IfObservable } from './observable/IfObservable';
import { ErrorObservable } from './observable/ErrorObservable';
import { $$observable } from './symbol/observable';
import { observable as Symbol_observable } from './symbol/observable';

export interface Subscribable<T> {
subscribe(observerOrNext?: PartialObserver<T> | ((value: T) => void),
Expand Down Expand Up @@ -177,7 +177,7 @@ export class Observable<T> implements Subscribable<T> {
* @method Symbol.observable
* @return {Observable} this instance of the observable
*/
[$$observable]() {
[Symbol_observable]() {
return this;
}
}
6 changes: 3 additions & 3 deletions src/Rx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ import { AsapScheduler } from './scheduler/AsapScheduler';
import { AsyncScheduler } from './scheduler/AsyncScheduler';
import { QueueScheduler } from './scheduler/QueueScheduler';
import { AnimationFrameScheduler } from './scheduler/AnimationFrameScheduler';
import { $$rxSubscriber as rxSubscriber } from './symbol/rxSubscriber';
import { $$iterator as iterator } from './symbol/iterator';
import { $$observable as observable } from './symbol/observable';
import { rxSubscriber } from './symbol/rxSubscriber';
import { iterator } from './symbol/iterator';
import { observable } from './symbol/observable';

/* tslint:enable:no-unused-variable */

Expand Down
4 changes: 2 additions & 2 deletions src/Subject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Subscriber } from './Subscriber';
import { ISubscription, Subscription, TeardownLogic } from './Subscription';
import { ObjectUnsubscribedError } from './util/ObjectUnsubscribedError';
import { SubjectSubscription } from './SubjectSubscription';
import { $$rxSubscriber } from './symbol/rxSubscriber';
import { rxSubscriber as rxSubscriberSymbol } from './symbol/rxSubscriber';

/**
* @class SubjectSubscriber<T>
Expand All @@ -21,7 +21,7 @@ export class SubjectSubscriber<T> extends Subscriber<T> {
*/
export class Subject<T> extends Observable<T> implements ISubscription {

[$$rxSubscriber]() {
[rxSubscriberSymbol]() {
return new SubjectSubscriber(this);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { isFunction } from './util/isFunction';
import { Observer, PartialObserver } from './Observer';
import { Subscription } from './Subscription';
import { empty as emptyObserver } from './Observer';
import { $$rxSubscriber } from './symbol/rxSubscriber';
import { rxSubscriber as rxSubscriberSymbol } from './symbol/rxSubscriber';

/**
* Implements the {@link Observer} interface and extends the
Expand All @@ -16,7 +16,7 @@ import { $$rxSubscriber } from './symbol/rxSubscriber';
*/
export class Subscriber<T> extends Subscription implements Observer<T> {

[$$rxSubscriber]() { return this; }
[rxSubscriberSymbol]() { return this; }

/**
* A static factory for a Subscriber, given a (potentially partial) definition
Expand Down
12 changes: 6 additions & 6 deletions src/observable/FromObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import { ArrayObservable } from './ArrayObservable';
import { ArrayLikeObservable } from './ArrayLikeObservable';

import { IScheduler } from '../Scheduler';
import { $$iterator } from '../symbol/iterator';
import { iterator as Symbol_iterator } from '../symbol/iterator';
import { Observable, ObservableInput } from '../Observable';
import { Subscriber } from '../Subscriber';
import { ObserveOnSubscriber } from '../operator/observeOn';
import { $$observable } from '../symbol/observable';
import { observable as Symbol_observable } from '../symbol/observable';

/**
* We need this JSDoc comment for affecting ESDoc.
Expand Down Expand Up @@ -84,7 +84,7 @@ export class FromObservable<T> extends Observable<T> {
*/
static create<T>(ish: ObservableInput<T>, scheduler?: IScheduler): Observable<T> {
if (ish != null) {
if (typeof ish[$$observable] === 'function') {
if (typeof ish[Symbol_observable] === 'function') {
if (ish instanceof Observable && !scheduler) {
return ish;
}
Expand All @@ -93,7 +93,7 @@ export class FromObservable<T> extends Observable<T> {
return new ArrayObservable<T>(ish, scheduler);
} else if (isPromise(ish)) {
return new PromiseObservable<T>(ish, scheduler);
} else if (typeof ish[$$iterator] === 'function' || typeof ish === 'string') {
} else if (typeof ish[Symbol_iterator] === 'function' || typeof ish === 'string') {
return new IteratorObservable<T>(ish, scheduler);
} else if (isArrayLike(ish)) {
return new ArrayLikeObservable(ish, scheduler);
Expand All @@ -107,9 +107,9 @@ export class FromObservable<T> extends Observable<T> {
const ish = this.ish;
const scheduler = this.scheduler;
if (scheduler == null) {
return ish[$$observable]().subscribe(subscriber);
return ish[Symbol_observable]().subscribe(subscriber);
} else {
return ish[$$observable]().subscribe(new ObserveOnSubscriber(subscriber, scheduler, 0));
return ish[Symbol_observable]().subscribe(new ObserveOnSubscriber(subscriber, scheduler, 0));
}
}
}
10 changes: 5 additions & 5 deletions src/observable/IteratorObservable.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { root } from '../util/root';
import { IScheduler } from '../Scheduler';
import { Observable } from '../Observable';
import { $$iterator } from '../symbol/iterator';
import { iterator as Symbol_iterator } from '../symbol/iterator';
import { TeardownLogic } from '../Subscription';
import { Subscriber } from '../Subscriber';

Expand Down Expand Up @@ -89,7 +89,7 @@ class StringIterator {
private idx: number = 0,
private len: number = str.length) {
}
[$$iterator]() { return (this); }
[Symbol_iterator]() { return (this); }
next() {
return this.idx < this.len ? {
done: false,
Expand All @@ -106,7 +106,7 @@ class ArrayIterator {
private idx: number = 0,
private len: number = toLength(arr)) {
}
[$$iterator]() { return this; }
[Symbol_iterator]() { return this; }
next() {
return this.idx < this.len ? {
done: false,
Expand All @@ -119,7 +119,7 @@ class ArrayIterator {
}

function getIterator(obj: any) {
const i = obj[$$iterator];
const i = obj[Symbol_iterator];
if (!i && typeof obj === 'string') {
return new StringIterator(obj);
}
Expand All @@ -129,7 +129,7 @@ function getIterator(obj: any) {
if (!i) {
throw new TypeError('object is not iterable');
}
return obj[$$iterator]();
return obj[Symbol_iterator]();
}

const maxSafeInteger = Math.pow(2, 53) - 1;
Expand Down
10 changes: 5 additions & 5 deletions src/operator/zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Subscriber } from '../Subscriber';
import { OuterSubscriber } from '../OuterSubscriber';
import { InnerSubscriber } from '../InnerSubscriber';
import { subscribeToResult } from '../util/subscribeToResult';
import { $$iterator } from '../symbol/iterator';
import { iterator as Symbol_iterator } from '../symbol/iterator';

/* tslint:disable:max-line-length */
export function zipProto<T, R>(this: Observable<T>, project: (v1: T) => R): Observable<R>;
Expand Down Expand Up @@ -135,8 +135,8 @@ export class ZipSubscriber<T, R> extends Subscriber<T> {
const iterators = this.iterators;
if (isArray(value)) {
iterators.push(new StaticArrayIterator(value));
} else if (typeof value[$$iterator] === 'function') {
iterators.push(new StaticIterator(value[$$iterator]()));
} else if (typeof value[Symbol_iterator] === 'function') {
iterators.push(new StaticIterator(value[Symbol_iterator]()));
} else {
iterators.push(new ZipBufferIterator(this.destination, this, value));
}
Expand Down Expand Up @@ -261,7 +261,7 @@ class StaticArrayIterator<T> implements LookAheadIterator<T> {
this.length = array.length;
}

[$$iterator]() {
[Symbol_iterator]() {
return this;
}

Expand Down Expand Up @@ -296,7 +296,7 @@ class ZipBufferIterator<T, R> extends OuterSubscriber<T, R> implements LookAhead
super(destination);
}

[$$iterator]() {
[Symbol_iterator]() {
return this;
}

Expand Down
7 changes: 6 additions & 1 deletion src/symbol/iterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ export function symbolIteratorPonyfill(root: any) {
}
}

export const $$iterator = symbolIteratorPonyfill(root);
export const iterator = symbolIteratorPonyfill(root);

/**
* @deprecated use iterator instead
*/
export const $$iterator = iterator;
7 changes: 6 additions & 1 deletion src/symbol/observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ export function getSymbolObservable(context: any) {
return $$observable;
}

export const $$observable = getSymbolObservable(root);
export const observable = getSymbolObservable(root);

/**
* @deprecated use observable instead
*/
export const $$observable = observable;
7 changes: 6 additions & 1 deletion src/symbol/rxSubscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@ import { root } from '../util/root';

const Symbol: any = root.Symbol;

export const $$rxSubscriber = (typeof Symbol === 'function' && typeof Symbol.for === 'function') ?
export const rxSubscriber = (typeof Symbol === 'function' && typeof Symbol.for === 'function') ?
Symbol.for('rxSubscriber') : '@@rxSubscriber';

/**
* @deprecated use rxSubscriber instead
*/
export const $$rxSubscriber = rxSubscriber;
12 changes: 6 additions & 6 deletions src/util/subscribeToResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { isPromise } from './isPromise';
import { isObject } from './isObject';
import { Subscriber } from '../Subscriber';
import { Observable, ObservableInput } from '../Observable';
import { $$iterator } from '../symbol/iterator';
import { iterator as Symbol_iterator } from '../symbol/iterator';
import { Subscription } from '../Subscription';
import { InnerSubscriber } from '../InnerSubscriber';
import { OuterSubscriber } from '../OuterSubscriber';
import { $$observable } from '../symbol/observable';
import { observable as Symbol_observable } from '../symbol/observable';

export function subscribeToResult<T, R>(outerSubscriber: OuterSubscriber<T, R>,
result: any,
Expand Down Expand Up @@ -54,8 +54,8 @@ export function subscribeToResult<T>(outerSubscriber: OuterSubscriber<any, any>,
root.setTimeout(() => { throw err; });
});
return destination;
} else if (result && typeof result[$$iterator] === 'function') {
const iterator = <any>result[$$iterator]();
} else if (result && typeof result[Symbol_iterator] === 'function') {
const iterator = <any>result[Symbol_iterator]();
do {
let item = iterator.next();
if (item.done) {
Expand All @@ -67,8 +67,8 @@ export function subscribeToResult<T>(outerSubscriber: OuterSubscriber<any, any>,
break;
}
} while (true);
} else if (result && typeof result[$$observable] === 'function') {
const obs = result[$$observable]();
} else if (result && typeof result[Symbol_observable] === 'function') {
const obs = result[Symbol_observable]();
if (typeof obs.subscribe !== 'function') {
destination.error(new TypeError('Provided object does not correctly implement Symbol.observable'));
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/util/toSubscriber.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Subscriber } from '../Subscriber';
import { $$rxSubscriber } from '../symbol/rxSubscriber';
import { rxSubscriber as rxSubscriberSymbol } from '../symbol/rxSubscriber';
import { PartialObserver, empty as emptyObserver } from '../Observer';

export function toSubscriber<T>(
Expand All @@ -12,8 +12,8 @@ export function toSubscriber<T>(
return (<Subscriber<T>> nextOrObserver);
}

if (nextOrObserver[$$rxSubscriber]) {
return nextOrObserver[$$rxSubscriber]();
if (nextOrObserver[rxSubscriberSymbol]) {
return nextOrObserver[rxSubscriberSymbol]();
}
}

Expand Down

0 comments on commit 747bef6

Please sign in to comment.