Skip to content

Commit

Permalink
refactor: use defineProperties and symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse committed Feb 23, 2017
1 parent 46ee6a5 commit ffb15e5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
45 changes: 27 additions & 18 deletions index.js
Expand Up @@ -5,6 +5,9 @@ const ContextSession = require('./lib/context');
const util = require('./lib/util');
const assert = require('assert');

const CONTEXT_SESSION = Symbol('context#contextSession');
const _CONTEXT_SESSION = Symbol('context#_contextSession');

/**
* Initialize session middleware with `opts`:
*
Expand Down Expand Up @@ -32,13 +35,14 @@ module.exports = function(opts, app) {
extendContext(app.context, opts);

return function* session(next) {
if (this.sess.store) yield this.sess.initFromExternal();
const sess = this[CONTEXT_SESSION];
if (sess.store) yield sess.initFromExternal();
try {
yield next;
} catch (err) {
throw err;
} finally {
yield this.sess.commit();
yield sess.commit();
}
};
};
Expand Down Expand Up @@ -93,21 +97,26 @@ function formatOpts(opts) {
*/

function extendContext(context, opts) {
context.__defineGetter__('sess', function() {
if (this._sess) return this._sess;
this._sess = new ContextSession(this, opts);
return this._sess;
});

context.__defineGetter__('session', function() {
return this.sess.get();
});

context.__defineSetter__('session', function(val) {
this.sess.set(val);
});

context.__defineGetter__('sessionOptions', function() {
return this.sess.opts;
Object.defineProperties(context, {
[CONTEXT_SESSION]: {
get() {
if (this[_CONTEXT_SESSION]) return this[_CONTEXT_SESSION];
this[_CONTEXT_SESSION] = new ContextSession(this, opts);
return this[_CONTEXT_SESSION];
},
},
session: {
get() {
return this[CONTEXT_SESSION].get();
},
set(val) {
this[CONTEXT_SESSION].set(val);
},
},
sessionOptions: {
get() {
return this[CONTEXT_SESSION].opts;
},
},
});
}
2 changes: 1 addition & 1 deletion lib/context.js
Expand Up @@ -79,7 +79,7 @@ class ContextSession {
return;
}

const json = yield this.store.get(externalKey, ctx);
const json = yield this.store.get(externalKey);
if (!this.valid(json)) {
// create a new `externalKey`
this.create();
Expand Down
5 changes: 4 additions & 1 deletion package.json
Expand Up @@ -29,8 +29,11 @@
"deep-equal": "^1.0.0",
"uid-safe": "^2.1.3"
},
"engines": {
"node": ">=4"
},
"scripts": {
"test": "npm run lint && NODE_ENV=test mocha --require should --reporter spec test/*.test.js",
"test": "npm run lint && NODE_ENV=test1 mocha --require should --reporter spec test/*.test.js",
"test-cov": "NODE_ENV=test node ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- --require should test/*.test.js",
"test-travis": "npm run lint && NODE_ENV=test node ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha --report lcovonly -- --require should test/*.test.js",
"lint": "eslint lib test index.js"
Expand Down

0 comments on commit ffb15e5

Please sign in to comment.