Skip to content

Commit 536dd76

Browse files
committed
Refactor context management into it's own context object
1 parent e546fc8 commit 536dd76

File tree

2 files changed

+45
-39
lines changed

2 files changed

+45
-39
lines changed

src/raven.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ var _Raven = window.Raven,
1010
lastCapturedException,
1111
lastEventId,
1212
globalServer,
13-
globalUser,
1413
globalKey,
1514
globalProject,
15+
globalContext = {},
1616
globalOptions = {
1717
logger: 'javascript',
1818
ignoreErrors: [],
@@ -21,9 +21,7 @@ var _Raven = window.Raven,
2121
includePaths: [],
2222
crossOrigin: 'anonymous',
2323
collectWindowErrors: true,
24-
tags: {},
25-
maxMessageLength: 100,
26-
extra: {}
24+
maxMessageLength: 100
2725
},
2826
isRavenInstalled = false,
2927
objectPrototype = Object.prototype,
@@ -78,7 +76,12 @@ var Raven = {
7876
// merge in options
7977
if (options) {
8078
each(options, function(key, value){
81-
globalOptions[key] = value;
79+
// tags and extra are special and need to be put into context
80+
if (key == 'tags' || key == 'extra') {
81+
globalContext[key] = value;
82+
} else {
83+
globalOptions[key] = value;
84+
}
8285
});
8386
}
8487

@@ -292,7 +295,7 @@ var Raven = {
292295
* @return {Raven}
293296
*/
294297
setUserContext: function(user) {
295-
globalUser = user;
298+
globalContext.user = user;
296299

297300
return Raven;
298301
},
@@ -304,7 +307,7 @@ var Raven = {
304307
* @return {Raven}
305308
*/
306309
setExtraContext: function(extra) {
307-
globalOptions.extra = extra || {};
310+
globalContext.extra = extra || {};
308311

309312
return Raven;
310313
},
@@ -316,7 +319,7 @@ var Raven = {
316319
* @return {Raven}
317320
*/
318321
setTagsContext: function(tags) {
319-
globalOptions.tags = tags || {};
322+
globalContext.tags = tags || {};
320323

321324
return Raven;
322325
},
@@ -720,8 +723,8 @@ function send(data) {
720723
data = objectMerge(baseData, data);
721724

722725
// Merge in the tags and extra separately since objectMerge doesn't handle a deep merge
723-
data.tags = objectMerge(objectMerge({}, globalOptions.tags), data.tags);
724-
data.extra = objectMerge(objectMerge({}, globalOptions.extra), data.extra);
726+
data.tags = objectMerge(objectMerge({}, globalContext.tags), data.tags);
727+
data.extra = objectMerge(objectMerge({}, globalContext.extra), data.extra);
725728

726729
// Send along our own collected metadata with extra
727730
data.extra = objectMerge({
@@ -731,9 +734,9 @@ function send(data) {
731734
// If there are no tags/extra, strip the key from the payload alltogther.
732735
if (isEmptyObject(data.tags)) delete data.tags;
733736

734-
if (globalUser) {
737+
if (globalContext.user) {
735738
// sentry.interfaces.User
736-
data.user = globalUser;
739+
data.user = globalContext.user;
737740
}
738741

739742
// Include the release if it's defined in globalOptions

test/raven.test.js

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ function flushRavenState() {
44
lastCapturedException = undefined;
55
lastEventId = undefined;
66
globalServer = undefined;
7-
globalUser = undefined;
87
globalProject = undefined;
8+
globalContext = {};
99
globalOptions = {
1010
logger: 'javascript',
1111
release: undefined,
@@ -15,9 +15,7 @@ function flushRavenState() {
1515
includePaths: [],
1616
crossOrigin: 'anonymous',
1717
collectWindowErrors: true,
18-
maxMessageLength: 100,
19-
tags: {},
20-
extra: {}
18+
maxMessageLength: 100
2119
},
2220
startTime = 0;
2321
ravenNotConfiguredError = undefined;
@@ -940,7 +938,7 @@ describe('globals', function() {
940938
logger: 'javascript'
941939
};
942940

943-
globalUser = {name: 'Matt'};
941+
globalContext.user = {name: 'Matt'};
944942

945943
send({foo: 'bar'});
946944
assert.deepEqual(window.makeRequest.lastCall.args[0].data, {
@@ -972,9 +970,9 @@ describe('globals', function() {
972970

973971
globalProject = '2';
974972
globalOptions = {
975-
logger: 'javascript',
976-
tags: {tag1: 'value1'}
973+
logger: 'javascript'
977974
};
975+
globalContext = {tags: {tag1: 'value1'}};
978976

979977

980978
send({tags: {tag2: 'value2'}});
@@ -993,7 +991,9 @@ describe('globals', function() {
993991
extra: {'session:duration': 100}
994992
});
995993
assert.deepEqual(globalOptions, {
996-
logger: 'javascript',
994+
logger: 'javascript'
995+
});
996+
assert.deepEqual(globalContext, {
997997
tags: {tag1: 'value1'}
998998
});
999999
});
@@ -1008,9 +1008,10 @@ describe('globals', function() {
10081008

10091009
globalProject = '2';
10101010
globalOptions = {
1011-
logger: 'javascript',
1012-
extra: {key1: 'value1'}
1011+
logger: 'javascript'
10131012
};
1013+
globalContext = {extra: {key1: 'value1'}};
1014+
10141015

10151016

10161017
send({extra: {key2: 'value2'}});
@@ -1028,7 +1029,9 @@ describe('globals', function() {
10281029
extra: {key1: 'value1', key2: 'value2', 'session:duration': 100}
10291030
});
10301031
assert.deepEqual(globalOptions, {
1031-
logger: 'javascript',
1032+
logger: 'javascript'
1033+
});
1034+
assert.deepEqual(globalContext, {
10321035
extra: {key1: 'value1'}
10331036
});
10341037
});
@@ -1045,7 +1048,7 @@ describe('globals', function() {
10451048
}
10461049
};
10471050

1048-
globalUser = {name: 'Matt'};
1051+
globalContext.user = {name: 'Matt'};
10491052

10501053
send({foo: 'bar'});
10511054
assert.deepEqual(window.makeRequest.lastCall.args[0].data, {
@@ -1819,41 +1822,41 @@ describe('Raven (public API)', function() {
18191822
});
18201823

18211824
describe('.setUserContext', function() {
1822-
it('should set the globalUser object', function() {
1825+
it('should set the globalContext.user object', function() {
18231826
Raven.setUserContext({name: 'Matt'});
1824-
assert.deepEqual(globalUser, {name: 'Matt'});
1827+
assert.deepEqual(globalContext.user, {name: 'Matt'});
18251828
});
18261829

1827-
it('should clear the globalUser with no arguments', function() {
1828-
globalUser = {name: 'Matt'};
1830+
it('should clear the globalContext.user with no arguments', function() {
1831+
globalContext.user = {name: 'Matt'};
18291832
Raven.setUserContext();
1830-
assert.isUndefined(globalUser);
1833+
assert.isUndefined(globalContext.user);
18311834
});
18321835
});
18331836

18341837
describe('.setExtraContext', function() {
1835-
it('should set the globalOptions.extra object', function() {
1838+
it('should set the globalContext.extra object', function() {
18361839
Raven.setExtraContext({name: 'Matt'});
1837-
assert.deepEqual(globalOptions.extra, {name: 'Matt'});
1840+
assert.deepEqual(globalContext.extra, {name: 'Matt'});
18381841
});
18391842

1840-
it('should clear globalOptions.extra with no arguments', function() {
1841-
globalOptions = {name: 'Matt'};
1843+
it('should clear globalContext.extra with no arguments', function() {
1844+
globalOptions.extra = {name: 'Matt'};
18421845
Raven.setExtraContext();
1843-
assert.deepEqual(globalOptions.extra, {});
1846+
assert.deepEqual(globalContext.extra, {});
18441847
});
18451848
});
18461849

18471850
describe('.setTagsContext', function() {
1848-
it('should set the globalOptions.tags object', function() {
1851+
it('should set the globalContext.tags object', function() {
18491852
Raven.setTagsContext({name: 'Matt'});
1850-
assert.deepEqual(globalOptions.tags, {name: 'Matt'});
1853+
assert.deepEqual(globalContext.tags, {name: 'Matt'});
18511854
});
18521855

1853-
it('should clear globalOptions.tags with no arguments', function() {
1854-
globalOptions = {name: 'Matt'};
1856+
it('should clear globalContext.tags with no arguments', function() {
1857+
globalContext.tags = {name: 'Matt'};
18551858
Raven.setTagsContext();
1856-
assert.deepEqual(globalOptions.tags, {});
1859+
assert.deepEqual(globalContext.tags, {});
18571860
});
18581861
});
18591862

0 commit comments

Comments
 (0)