Skip to content

Commit 35d6c0a

Browse files
committed
refactor: replace custom defer function with setImmediate
1 parent bbeca94 commit 35d6c0a

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
@@ -56,16 +56,6 @@ var warning = 'Warning: connect.session() MemoryStore is not\n'
5656
+ 'designed for a production environment, as it will leak\n'
5757
+ 'memory, and will not scale past a single process.';
5858

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

@@ -317,7 +307,7 @@ function session(options) {
317307
debug('destroying');
318308
store.destroy(req.sessionID, function ondestroy(err) {
319309
if (err) {
320-
defer(next, err);
310+
setImmediate(next, err);
321311
}
322312

323313
debug('destroyed');
@@ -342,7 +332,7 @@ function session(options) {
342332
if (shouldSave(req)) {
343333
req.session.save(function onsave(err) {
344334
if (err) {
345-
defer(next, err);
335+
setImmediate(next, err);
346336
}
347337

348338
writeend();
@@ -354,7 +344,7 @@ function session(options) {
354344
debug('touching');
355345
store.touch(req.sessionID, req.session, function ontouch(err) {
356346
if (err) {
357-
defer(next, err);
347+
setImmediate(next, err);
358348
}
359349

360350
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)