Skip to content
This repository was archived by the owner on Apr 20, 2018. It is now read-only.

Commit de3d945

Browse files
Merge pull request #880 from fghibellini/master
tryCatch no longer uses global state to pass the target
2 parents baf7fa9 + bf02264 commit de3d945

3 files changed

Lines changed: 22 additions & 10 deletions

File tree

src/core/internal/trycatch.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
var errorObj = {e: {}};
2-
var tryCatchTarget;
3-
function tryCatcher() {
4-
try {
5-
return tryCatchTarget.apply(this, arguments);
6-
} catch (e) {
7-
errorObj.e = e;
8-
return errorObj;
2+
function tryCatcherGen(tryCatchTarget) {
3+
return function tryCatcher() {
4+
try {
5+
return tryCatchTarget.apply(this, arguments);
6+
} catch (e) {
7+
errorObj.e = e;
8+
return errorObj;
9+
}
910
}
1011
}
11-
function tryCatch(fn) {
12+
var tryCatch = Rx.internals.tryCatch = function tryCatch(fn) {
1213
if (!isFunction(fn)) { throw new TypeError('fn must be a function'); }
13-
tryCatchTarget = fn;
14-
return tryCatcher;
14+
return tryCatcherGen(fn);
1515
}
1616
function thrower(e) {
1717
throw e;

tests/internal/trycatch.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
QUnit.module('tryCatch');
2+
3+
var tryCatch = Rx.internals.tryCatch;
4+
5+
test('tryCatch_multiple', function () {
6+
var catcher1 = tryCatch(function() { return 1; });
7+
var catcher2 = tryCatch(function() { return 2; });
8+
9+
ok(catcher1() === 1, "Shouldn't store state in global context");
10+
ok(catcher2() === 2, "Shouldn't store state in global context");
11+
});

tests/rx.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595

9696
<!-- Individual Tests -->
9797
<script src="internal/isequal.js"></script>
98+
<script src="internal/trycatch.js"></script>
9899
<script src="subjects/asyncsubject.js"></script>
99100
<script src="subjects/subject.js"></script>
100101

0 commit comments

Comments
 (0)