Skip to content

Commit ee6a194

Browse files
authored
refactor: replace custom defer function with setImmediate (#1021)
1 parent 95f1edd commit ee6a194

File tree

3 files changed

+13
-38
lines changed

3 files changed

+13
-38
lines changed

index.js

+4-14
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,6 @@ var warning = 'Warning: connect.session() MemoryStore is not\n'
5555
+ 'designed for a production environment, as it will leak\n'
5656
+ 'memory, and will not scale past a single process.';
5757

58-
/**
59-
* Node.js 0.8+ async implementation.
60-
* @private
61-
*/
62-
63-
/* istanbul ignore next */
64-
var defer = typeof setImmediate === 'function'
65-
? setImmediate
66-
: function(fn){ process.nextTick(fn.bind.apply(fn, arguments)) }
67-
6858
/**
6959
* Setup session store with the given `options`.
7060
*
@@ -246,7 +236,7 @@ function session(options) {
246236
try {
247237
setcookie(res, name, req.sessionID, secrets[0], req.session.cookie.data)
248238
} catch (err) {
249-
defer(next, err)
239+
setImmediate(next, err)
250240
}
251241
});
252242

@@ -316,7 +306,7 @@ function session(options) {
316306
debug('destroying');
317307
store.destroy(req.sessionID, function ondestroy(err) {
318308
if (err) {
319-
defer(next, err);
309+
setImmediate(next, err);
320310
}
321311

322312
debug('destroyed');
@@ -341,7 +331,7 @@ function session(options) {
341331
if (shouldSave(req)) {
342332
req.session.save(function onsave(err) {
343333
if (err) {
344-
defer(next, err);
334+
setImmediate(next, err);
345335
}
346336

347337
writeend();
@@ -353,7 +343,7 @@ function session(options) {
353343
debug('touching');
354344
store.touch(req.sessionID, req.session, function ontouch(err) {
355345
if (err) {
356-
defer(next, err);
346+
setImmediate(next, err);
357347
}
358348

359349
debug('touched');

session/memory.js

+6-16
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,6 @@
1616
var Store = require('./store')
1717
var util = require('util')
1818

19-
/**
20-
* Shim setImmediate for node.js < 0.10
21-
* @private
22-
*/
23-
24-
/* istanbul ignore next */
25-
var defer = typeof setImmediate === 'function'
26-
? setImmediate
27-
: function(fn){ process.nextTick(fn.bind.apply(fn, arguments)) }
28-
2919
/**
3020
* Module exports.
3121
*/
@@ -68,7 +58,7 @@ MemoryStore.prototype.all = function all(callback) {
6858
}
6959
}
7060

71-
callback && defer(callback, null, sessions)
61+
callback && setImmediate(callback, null, sessions)
7262
}
7363

7464
/**
@@ -80,7 +70,7 @@ MemoryStore.prototype.all = function all(callback) {
8070

8171
MemoryStore.prototype.clear = function clear(callback) {
8272
this.sessions = Object.create(null)
83-
callback && defer(callback)
73+
callback && setImmediate(callback)
8474
}
8575

8676
/**
@@ -92,7 +82,7 @@ MemoryStore.prototype.clear = function clear(callback) {
9282

9383
MemoryStore.prototype.destroy = function destroy(sessionId, callback) {
9484
delete this.sessions[sessionId]
95-
callback && defer(callback)
85+
callback && setImmediate(callback)
9686
}
9787

9888
/**
@@ -104,7 +94,7 @@ MemoryStore.prototype.destroy = function destroy(sessionId, callback) {
10494
*/
10595

10696
MemoryStore.prototype.get = function get(sessionId, callback) {
107-
defer(callback, null, getSession.call(this, sessionId))
97+
setImmediate(callback, null, getSession.call(this, sessionId))
10898
}
10999

110100
/**
@@ -118,7 +108,7 @@ MemoryStore.prototype.get = function get(sessionId, callback) {
118108

119109
MemoryStore.prototype.set = function set(sessionId, session, callback) {
120110
this.sessions[sessionId] = JSON.stringify(session)
121-
callback && defer(callback)
111+
callback && setImmediate(callback)
122112
}
123113

124114
/**
@@ -153,7 +143,7 @@ MemoryStore.prototype.touch = function touch(sessionId, session, callback) {
153143
this.sessions[sessionId] = JSON.stringify(currentSession)
154144
}
155145

156-
callback && defer(callback)
146+
callback && setImmediate(callback)
157147
}
158148

159149
/**

test/support/smart-store.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@
33
var session = require('../../')
44
var util = require('util')
55

6-
/* istanbul ignore next */
7-
var defer = typeof setImmediate === 'function'
8-
? setImmediate
9-
: function(fn){ process.nextTick(fn.bind.apply(fn, arguments)) }
10-
116
module.exports = SmartStore
127

138
function SmartStore () {
@@ -19,7 +14,7 @@ util.inherits(SmartStore, session.Store)
1914

2015
SmartStore.prototype.destroy = function destroy (sid, callback) {
2116
delete this.sessions[sid]
22-
defer(callback, null)
17+
setImmediate(callback, null)
2318
}
2419

2520
SmartStore.prototype.get = function get (sid, callback) {
@@ -45,10 +40,10 @@ SmartStore.prototype.get = function get (sid, callback) {
4540
}
4641
}
4742

48-
defer(callback, null, sess)
43+
setImmediate(callback, null, sess)
4944
}
5045

5146
SmartStore.prototype.set = function set (sid, sess, callback) {
5247
this.sessions[sid] = JSON.stringify(sess)
53-
defer(callback, null)
48+
setImmediate(callback, null)
5449
}

0 commit comments

Comments
 (0)