Skip to content

Commit

Permalink
feat(Symbol.observable): is no longer polyfilled
Browse files Browse the repository at this point in the history
BREAKING CHANGE: RxJS will no longer be polyfilling Symbol.observable. That should be done by an actual polyfill library. This is to prevent duplication of code, and also to prevent having modules with side-effects in rxjs.
  • Loading branch information
benlesh committed Mar 7, 2018
1 parent 1e4ca05 commit dc575ba
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 54 deletions.
11 changes: 11 additions & 0 deletions spec/helpers/polyfills.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
if (typeof Symbol !== 'function') {
let id = 0;
const symbolFn: any = (description: string) =>
`Symbol_${id++} ${description} (RxJS Testing Polyfill)`;

Symbol = symbolFn;
}

if (!(Symbol as any).observable) {
(Symbol as any).observable = Symbol('Symbol.observable polyfill from RxJS Testing');
}
3 changes: 2 additions & 1 deletion spec/support/coverage.opts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
--require spec/helpers/polyfills.ts
--require spec/helpers/testScheduler-ui.ts
--ui spec/helpers/testScheduler-ui.ts

Expand All @@ -6,4 +7,4 @@
--globals WebSocket,FormData,XDomainRequest,ActiveXObject

--recursive
--timeout 5000
--timeout 5000
1 change: 1 addition & 0 deletions spec/support/default.opts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
--require .out/spec/helpers/polyfills.ts
--require .out/spec/helpers/testScheduler-ui.js
--ui .out/spec/helpers/testScheduler-ui.js

Expand Down
3 changes: 2 additions & 1 deletion spec/support/tests2png.opts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
--require spec-js/helpers/polyfills.ts
--require source-map-support/register
--require spec-js/helpers/tests2png/diagram-test-runner.js
--require spec-js/helpers/testScheduler-ui.js
Expand All @@ -6,4 +7,4 @@
--reporter dot

--recursive
--timeout 5000
--timeout 5000
5 changes: 3 additions & 2 deletions spec/support/webpack.mocha.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var path = require('path');
var glob = require('glob');
var webpack = require('webpack');

var globPattern = 'spec-js/**/!(mocha.sauce.gruntfile|mocha.sauce.runner|webpack.mocha.config|painter|diagram-test-runner|testScheduler-ui).js';
var globPattern = 'spec-js/**/!(mocha.sauce.gruntfile|mocha.sauce.runner|webpack.mocha.config|painter|diagram-test-runner|polyfills|testScheduler-ui).js';
var files = _.map(glob.sync(globPattern), function (x) {
return path.resolve('./', x);
});
Expand All @@ -18,6 +18,7 @@ module.exports = {
},

entry: {
'browser.polyfills': './spec-js/helpers/polyfills.js',
'browser.testscheduler': './spec-js/helpers/testScheduler-ui.js',
'browser.spec': files
},
Expand All @@ -31,4 +32,4 @@ module.exports = {
new webpack.optimize.CommonsChunkPlugin('browser.common.js'),
new webpack.IgnorePlugin(/^mocha$/)
]
};
};
10 changes: 0 additions & 10 deletions spec/symbol/observable-polyfilled-spec.ts

This file was deleted.

16 changes: 0 additions & 16 deletions spec/symbol/observable-spec.ts

This file was deleted.

41 changes: 18 additions & 23 deletions src/internal/symbol/observable.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
import { root } from '../util/root';

export function getSymbolObservable(context: { Symbol: SymbolConstructor; }): symbol {
let $$observable: symbol;
let Symbol = context.Symbol;

if (typeof Symbol === 'function') {
if (Symbol.observable) {
$$observable = Symbol.observable;
} else {
$$observable = Symbol('observable');
Symbol.observable = $$observable;
}
} else {
$$observable = <any>'@@observable';
/** Symbol.observable addition */
/* Note: This will add Symbol.observable globally for all TypeScript users,
however, we are no longer polyfilling Symbol.observable */
declare global {
interface SymbolConstructor {
observable: symbol;
}

return $$observable;
}

export const observable = getSymbolObservable(root);
/* NOTE: Warning users that they don't have a Symbol.observable
polyfill. It's not something we'd throw on, because it's not
really *required*, however, they should be warned to give them a
clue as to why they might be seeing errors when trying to convert
ObservableLikes to Observables */
if (!Symbol || !Symbol.observable) {
console.warn('RxJS: Symbol.observable does not exist');
}

/**
* @deprecated use observable instead
* @deprecated use {@link observable}
*/
export const $$observable = observable;
export const $$observable = Symbol && Symbol.observable || '@@observable';

declare global {
interface SymbolConstructor {
observable: symbol;
}
}
/** Symbol.observable or a string "@@observable". Used for interop */
export const observable = $$observable;
3 changes: 2 additions & 1 deletion src/internal/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Observable } from './Observable';
import { PartialObserver } from './types';
import { observable as Symbol_observable } from './symbol/observable';

/** OPERATOR INTERFACES */

Expand Down Expand Up @@ -73,4 +74,4 @@ export interface Observer<T> {
next: (value: T) => void;
error: (err: any) => void;
complete: () => void;
}
}

0 comments on commit dc575ba

Please sign in to comment.