Skip to content

Commit

Permalink
refactor(Symbol): introduce unified Symbol module
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Dec 1, 2015
1 parent 2896556 commit 76f1bf0
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 90 deletions.
2 changes: 1 addition & 1 deletion spec/observables/from-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
var Rx = require('../../dist/cjs/Rx');
var Promise = require('promise');
var Observable = Rx.Observable;
var $$iterator = require('../../dist/cjs/util/Symbol_iterator').$$iterator;
var $$iterator = require('../../dist/cjs/util/SymbolShim').SymbolShim.iterator;

describe('Observable.from', function () {
it('should enumerate an Array', function (done) {
Expand Down
4 changes: 2 additions & 2 deletions src/Observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {Subscriber} from './Subscriber';
import {Subscription} from './Subscription';
import {root} from './util/root';
import {CoreOperators } from './CoreOperators';
import {$$observable} from './util/Symbol_observable';
import {SymbolShim} from './util/SymbolShim';
import {GroupedObservable} from './operators/groupBy-support';
import {ConnectableObservable} from './observable/ConnectableObservable';
import {Subject} from './Subject';
Expand Down Expand Up @@ -67,7 +67,7 @@ export class Observable<T> implements CoreOperators<T> {
* @returns {Observable} this instance of the observable
* @description an interop point defined by the es7-observable spec https://github.com/zenparsing/es-observable
*/
[$$observable]() {
[SymbolShim.observable]() {
return this;
}

Expand Down
10 changes: 5 additions & 5 deletions src/observable/IteratorObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Scheduler} from '../Scheduler';
import {Observable} from '../Observable';

import {root} from '../util/root';
import {$$iterator} from '../util/Symbol_iterator';
import {SymbolShim} from '../util/SymbolShim';
import {tryCatch} from '../util/tryCatch';
import {errorObject} from '../util/errorObject';

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

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

const maxSafeInteger = Math.pow(2, 53) - 1;
Expand Down
11 changes: 5 additions & 6 deletions src/observable/from.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import {IteratorObservable} from'./IteratorObservable';
import {ArrayObservable} from './fromArray';

import {Scheduler} from '../Scheduler';
import {$$observable} from '../util/Symbol_observable';
import {$$iterator} from '../util/Symbol_iterator';
import {SymbolShim} from '../util/SymbolShim';
import {Observable} from '../Observable';
import {Subscriber} from '../Subscriber';
import {ObserveOnSubscriber} from '../operators/observeOn-support';
Expand All @@ -23,12 +22,12 @@ export class FromObservable<T> extends Observable<T> {
return new ArrayObservable(ish, scheduler);
} else if (typeof ish.then === 'function') {
return new PromiseObservable(ish, scheduler);
} else if (typeof ish[$$observable] === 'function') {
} else if (typeof ish[SymbolShim.observable] === 'function') {
if (ish instanceof Observable) {
return ish;
}
return new FromObservable(ish, scheduler);
} else if (typeof ish[$$iterator] === 'function') {
} else if (typeof ish[SymbolShim.iterator] === 'function') {
return new IteratorObservable(ish, null, null, scheduler);
}
}
Expand All @@ -40,9 +39,9 @@ export class FromObservable<T> extends Observable<T> {
const ish = this.ish;
const scheduler = this.scheduler;
if (scheduler === immediate) {
return ish[$$observable]().subscribe(subscriber);
return ish[SymbolShim.observable]().subscribe(subscriber);
} else {
return ish[$$observable]().subscribe(new ObserveOnSubscriber(subscriber, scheduler, 0));
return ish[SymbolShim.observable]().subscribe(new ObserveOnSubscriber(subscriber, scheduler, 0));
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/operators/zip-support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {tryCatch} from '../util/tryCatch';
import {errorObject} from '../util/errorObject';
import {OuterSubscriber} from '../OuterSubscriber';
import {subscribeToResult} from '../util/subscribeToResult';
import {$$iterator} from '../util/Symbol_iterator';
import {SymbolShim} from '../util/SymbolShim';

const isArray = Array.isArray;

Expand Down Expand Up @@ -43,8 +43,8 @@ export class ZipSubscriber<T, R> extends Subscriber<T> {
const index = this.index++;
if (isArray(value)) {
iterators.push(new StaticArrayIterator(value));
} else if (typeof value[$$iterator] === 'function') {
iterators.push(new StaticIterator(value[$$iterator]()));
} else if (typeof value[SymbolShim.iterator] === 'function') {
iterators.push(new StaticIterator(value[SymbolShim.iterator]()));
} else {
iterators.push(new ZipBufferIterator(this.destination, this, value, index));
}
Expand Down Expand Up @@ -158,7 +158,7 @@ class StaticArrayIterator<T> implements LookAheadIterator<T> {
this.length = array.length;
}

[$$iterator]() {
[SymbolShim.iterator]() {
return this;
}

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

[$$iterator]() {
[SymbolShim.iterator]() {
return this;
}

Expand Down
84 changes: 48 additions & 36 deletions src/util/Symbol_iterator.ts → src/util/SymbolShim.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,48 @@
import {root} from './root';

if (!root.Symbol) {
root.Symbol = {};
}

if (!root.Symbol.iterator) {
if (typeof root.Symbol.for === 'function') {
root.Symbol.iterator = root.Symbol.for('iterator');
} else if (root.Set && typeof new root.Set()['@@iterator'] === 'function') {
// Bug for mozilla version
root.Symbol.iterator = '@@iterator';
} else if (root.Map) {
// es6-shim specific logic
let keys = Object.getOwnPropertyNames(root.Map.prototype);
for (let i = 0; i < keys.length; ++i) {
let key = keys[i];
if (key !== 'entries' && key !== 'size' && root.Map.prototype[key] === root.Map.prototype['entries']) {
root.Symbol.iterator = key;
break;
}
}
} else {
root.Symbol.iterator = '@@iterator';
}
}

export const $$iterator = root.Symbol.iterator;

// // Shim in iterator support
// export var $iterator$ = (typeof Symbol === 'function' && Symbol.iterator) || '_es6shim_iterator_';

// // Bug for mozilla version
// if (root.Set && typeof new root.Set()['@@iterator'] === 'function') {
// $iterator$ = '@@iterator';
// }
import {root} from './root';

if (!root.Symbol) {
root.Symbol = {};
}

if (!root.Symbol.observable) {
if (typeof root.Symbol.for === 'function') {
root.Symbol.observable = root.Symbol.for('observable');
} else {
root.Symbol.observable = '@@observable';
}
}

if (!root.Symbol.iterator) {
if (typeof root.Symbol.for === 'function') {
root.Symbol.iterator = root.Symbol.for('iterator');
} else if (root.Set && typeof new root.Set()['@@iterator'] === 'function') {
// Bug for mozilla version
root.Symbol.iterator = '@@iterator';
} else if (root.Map) {
// es6-shim specific logic
let keys = Object.getOwnPropertyNames(root.Map.prototype);
for (let i = 0; i < keys.length; ++i) {
let key = keys[i];
if (key !== 'entries' && key !== 'size' && root.Map.prototype[key] === root.Map.prototype['entries']) {
root.Symbol.iterator = key;
break;
}
}
} else {
root.Symbol.iterator = '@@iterator';
}
}

if (!root.Symbol.dispose) {
if (typeof root.Symbol.for === 'function') {
root.Symbol.dispose = root.Symbol.for('dispose');
} else {
root.Symbol.dispose = '@@dispose';
}
}

export module SymbolShim {
export const observable: symbol = root.Symbol.observable;
export const iterator: symbol = root.Symbol.iterator;
export const dispose: symbol = root.Symbol.dispose;
};
15 changes: 0 additions & 15 deletions src/util/Symbol_dispose.ts

This file was deleted.

15 changes: 0 additions & 15 deletions src/util/Symbol_observable.ts

This file was deleted.

9 changes: 4 additions & 5 deletions src/util/subscribeToResult.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {Subscriber} from '../Subscriber';
import {Observable} from '../Observable';
import {$$iterator} from '../util/Symbol_iterator';
import {$$observable} from '../util/Symbol_observable';
import {SymbolShim} from '../util/SymbolShim';
import {Subscription} from '../Subscription';
import {InnerSubscriber} from '../InnerSubscriber';
import {OuterSubscriber} from '../OuterSubscriber';
Expand Down Expand Up @@ -47,7 +46,7 @@ export function subscribeToResult<T, R>(outerSubscriber: OuterSubscriber<T, R>,
setTimeout(() => { throw err; });
});
return destination;
} else if (typeof result[$$iterator] === 'function') {
} else if (typeof result[SymbolShim.iterator] === 'function') {
for (let item of result) {
destination.next(item);
if (destination.isUnsubscribed) {
Expand All @@ -57,8 +56,8 @@ export function subscribeToResult<T, R>(outerSubscriber: OuterSubscriber<T, R>,
if (!destination.isUnsubscribed) {
destination.complete();
}
} else if (typeof result[$$observable] === 'function') {
const obs = result[$$observable]();
} else if (typeof result[SymbolShim.observable] === 'function') {
const obs = result[SymbolShim.observable]();
if (typeof obs.subscribe !== 'function') {
destination.error('invalid observable');
} else {
Expand Down

0 comments on commit 76f1bf0

Please sign in to comment.