Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace custom defer function with setImmediate #1021

Merged
merged 2 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 4 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,6 @@ var warning = 'Warning: connect.session() MemoryStore is not\n'
+ 'designed for a production environment, as it will leak\n'
+ 'memory, and will not scale past a single process.';

/**
* Node.js 0.8+ async implementation.
* @private
*/

/* istanbul ignore next */
var defer = typeof setImmediate === 'function'
? setImmediate
: function(fn){ process.nextTick(fn.bind.apply(fn, arguments)) }

/**
* Setup session store with the given `options`.
*
Expand Down Expand Up @@ -247,7 +237,7 @@ function session(options) {
try {
setcookie(res, name, req.sessionID, secrets[0], req.session.cookie.data)
} catch (err) {
defer(next, err)
setImmediate(next, err)
}
});

Expand Down Expand Up @@ -317,7 +307,7 @@ function session(options) {
debug('destroying');
store.destroy(req.sessionID, function ondestroy(err) {
if (err) {
defer(next, err);
setImmediate(next, err);
}

debug('destroyed');
Expand All @@ -342,7 +332,7 @@ function session(options) {
if (shouldSave(req)) {
req.session.save(function onsave(err) {
if (err) {
defer(next, err);
setImmediate(next, err);
}

writeend();
Expand All @@ -354,7 +344,7 @@ function session(options) {
debug('touching');
store.touch(req.sessionID, req.session, function ontouch(err) {
if (err) {
defer(next, err);
setImmediate(next, err);
}

debug('touched');
Expand Down
22 changes: 6 additions & 16 deletions session/memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@
var Store = require('./store')
var util = require('util')

/**
* Shim setImmediate for node.js < 0.10
* @private
*/

/* istanbul ignore next */
var defer = typeof setImmediate === 'function'
? setImmediate
: function(fn){ process.nextTick(fn.bind.apply(fn, arguments)) }

/**
* Module exports.
*/
Expand Down Expand Up @@ -68,7 +58,7 @@ MemoryStore.prototype.all = function all(callback) {
}
}

callback && defer(callback, null, sessions)
callback && setImmediate(callback, null, sessions)
}

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

MemoryStore.prototype.clear = function clear(callback) {
this.sessions = Object.create(null)
callback && defer(callback)
callback && setImmediate(callback)
}

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

MemoryStore.prototype.destroy = function destroy(sessionId, callback) {
delete this.sessions[sessionId]
callback && defer(callback)
callback && setImmediate(callback)
}

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

MemoryStore.prototype.get = function get(sessionId, callback) {
defer(callback, null, getSession.call(this, sessionId))
setImmediate(callback, null, getSession.call(this, sessionId))
}

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

MemoryStore.prototype.set = function set(sessionId, session, callback) {
this.sessions[sessionId] = JSON.stringify(session)
callback && defer(callback)
callback && setImmediate(callback)
}

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

callback && defer(callback)
callback && setImmediate(callback)
}

/**
Expand Down
11 changes: 3 additions & 8 deletions test/support/smart-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
var session = require('../../')
var util = require('util')

/* istanbul ignore next */
var defer = typeof setImmediate === 'function'
? setImmediate
: function(fn){ process.nextTick(fn.bind.apply(fn, arguments)) }

module.exports = SmartStore

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

SmartStore.prototype.destroy = function destroy (sid, callback) {
delete this.sessions[sid]
defer(callback, null)
setImmediate(callback, null)
}

SmartStore.prototype.get = function get (sid, callback) {
Expand All @@ -45,10 +40,10 @@ SmartStore.prototype.get = function get (sid, callback) {
}
}

defer(callback, null, sess)
setImmediate(callback, null, sess)
}

SmartStore.prototype.set = function set (sid, sess, callback) {
this.sessions[sid] = JSON.stringify(sess)
defer(callback, null)
setImmediate(callback, null)
}
Loading