Skip to content

Commit 21c54e1

Browse files
committed
feat(Symbol.observable): is no longer polyfilled
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.
1 parent 6319f3c commit 21c54e1

8 files changed

Lines changed: 37 additions & 53 deletions

File tree

spec/helpers/polyfills.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
if (typeof Symbol !== 'function') {
2+
let id = 0;
3+
const symbolFn: any = (description: string) =>
4+
`Symbol_${id++} ${description} (RxJS Testing Polyfill)`;
5+
6+
Symbol = symbolFn;
7+
}
8+
9+
if (!(Symbol as any).observable) {
10+
(Symbol as any).observable = Symbol('Symbol.observable polyfill from RxJS Testing');
11+
}

spec/support/coverage.opts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
--require spec/helpers/polyfills.ts
12
--require spec/helpers/testScheduler-ui.ts
23
--ui spec/helpers/testScheduler-ui.ts
34

@@ -6,4 +7,4 @@
67
--globals WebSocket,FormData,XDomainRequest,ActiveXObject
78

89
--recursive
9-
--timeout 5000
10+
--timeout 5000

spec/support/default.opts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
--require .out/spec/helpers/polyfills.ts
12
--require .out/spec/helpers/testScheduler-ui.js
23
--ui .out/spec/helpers/testScheduler-ui.js
34

spec/support/tests2png.opts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
--require spec-js/helpers/polyfills.ts
12
--require source-map-support/register
23
--require spec-js/helpers/tests2png/diagram-test-runner.js
34
--require spec-js/helpers/testScheduler-ui.js
@@ -6,4 +7,4 @@
67
--reporter dot
78

89
--recursive
9-
--timeout 5000
10+
--timeout 5000

spec/support/webpack.mocha.config.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var path = require('path');
33
var glob = require('glob');
44
var webpack = require('webpack');
55

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

2020
entry: {
21+
'browser.polyfills': './spec-js/helpers/polyfills.js',
2122
'browser.testscheduler': './spec-js/helpers/testScheduler-ui.js',
2223
'browser.spec': files
2324
},
@@ -31,4 +32,4 @@ module.exports = {
3132
new webpack.optimize.CommonsChunkPlugin('browser.common.js'),
3233
new webpack.IgnorePlugin(/^mocha$/)
3334
]
34-
};
35+
};

spec/symbol/observable-polyfilled-spec.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

spec/symbol/observable-spec.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/internal/symbol/observable.ts

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,27 @@
11
import { root } from '../util/root';
22

3-
export function getSymbolObservable(context: { Symbol: SymbolConstructor; }): symbol {
4-
let $$observable: symbol;
5-
let Symbol = context.Symbol;
6-
7-
if (typeof Symbol === 'function') {
8-
if (Symbol.observable) {
9-
$$observable = Symbol.observable;
10-
} else {
11-
$$observable = Symbol('observable');
12-
Symbol.observable = $$observable;
13-
}
14-
} else {
15-
$$observable = <any>'@@observable';
3+
/** Symbol.observable addition */
4+
/* Note: This will add Symbol.observable globally for all TypeScript users,
5+
however, we are no longer polyfilling Symbol.observable */
6+
declare global {
7+
interface SymbolConstructor {
8+
observable: symbol;
169
}
17-
18-
return $$observable;
1910
}
2011

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

2321
/**
24-
* @deprecated use observable instead
22+
* @deprecated use {@link observable}
2523
*/
26-
export const $$observable = observable;
24+
export const $$observable = Symbol && Symbol.observable || '@@observable';
2725

28-
declare global {
29-
interface SymbolConstructor {
30-
observable: symbol;
31-
}
32-
}
26+
/** Symbol.observable or a string "@@observable". Used for interop */
27+
export const observable = $$observable;

0 commit comments

Comments
 (0)