bravey
Version:
A simple JavaScript NLP-like library to help you creating your own bot.
168 lines (137 loc) • 7.82 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: sessionmanagers/InMemorySessionManager.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: sessionmanagers/InMemorySessionManager.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* A session manager to be used as sessionManager for {@link Bravey.ContextManager}. Stores session IDs in memory.
* @param {Number} [extensions.sessionLength] - Session duration in milliseconds. (default is 600000 - 10 minutes)
* @constructor
*/
Bravey.SessionManager.InMemorySessionManager = function(extensions) {
if (!extensions) extensions = {};
var sessions = {};
var sessionLength = extensions.sessionLength || 600000;
function getTimestamp() {
return (new Date()).getTime();
}
function cleanSessions() {
var now = getTimestamp();
for (var a in sessions)
if (now - sessions[a].timestamp > sessionLength) delete sessions[a];
}
function makeNewSession() {
return {
context: ["default"],
data: {},
timestamp: getTimestamp()
};
}
/**
* Reserves a new session ID. It also clear expired sessions.
* @returns {string} The new session ID.
*/
this.reserveSessionId = function(id) {
if (id == undefined) {
do {
id = Bravey.Text.generateGUID();
} while (sessions[id]);
}
cleanSessions();
if (!sessions[id]) sessions[id] = makeNewSession();
return id;
}
/**
* Keep a session alive, resetting expiration time.
* @param {string} sessionid - The session ID.
* @returns {boolean} True if sessionid is found and kept alive.
*/
this.keepAlive = function(sessionid) {
if (sessions[sessionid]) {
sessions[sessionid].timestamp = getTimestamp();
return true;
} else
return false;
}
/**
* Set context for the specified session ID. Updates the user timestamp, preventing session expiring.
* @param {string} sessionid - The session ID.
* @param {string[]} contexttags - The context tags to be set.
* @returns {boolean} True if sessionid is found and context is set.
*/
this.setContext = function(sessionid, contexttags) {
if (sessions[sessionid]) {
sessions[sessionid].context = contexttags;
this.keepAlive(sessionid);
return true;
} else return false;
}
/**
* Set context data for the specified session ID. Updates the user timestamp, preventing session expiring.
* @param {string} sessionid - The session ID.
* @param {Object} data - Key/value pair for data to be set.
* @returns {boolean} True if sessionid is found and context is set.
*/
this.setData = function(sessionid, data) {
if (sessions[sessionid]) {
for (var a in data) sessions[sessionid].data[a] = data[a];
this.keepAlive(sessionid);
return true;
} else return false;
}
/**
* Empty context data for the specified session ID. Updates the user timestamp, preventing session expiring.
* @param {string} sessionid - The session ID.
* @returns {boolean} True if sessionid is found and context is set.
*/
this.clearData = function(sessionid, data) {
if (sessions[sessionid]) {
sessions[sessionid].data = {};
this.keepAlive(sessionid);
return true;
} else return false;
}
/**
* Get context for the specified session ID.
* @param {string} sessionid - The session ID.
* @returns {string[]} The related contexts.
*/
this.getContext = function(sessionid) {
if (sessions[sessionid]) return sessions[sessionid].context;
}
/**
* Get context data for the specified session ID.
* @param {string} sessionid - The session ID.
* @returns {string[]} The related data.
*/
this.getData = function(sessionid) {
if (sessions[sessionid]) return sessions[sessionid].data;
}
}</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Bravey.ApiAiAdapter.html">ApiAiAdapter</a></li><li><a href="Bravey.ContextManager.html">ContextManager</a></li><li><a href="Bravey.DocumentClassifier.html">DocumentClassifier</a></li><li><a href="Bravey.EMailEntityRecognizer.html">EMailEntityRecognizer</a></li><li><a href="Bravey.Filter.BasicFilter.html">BasicFilter</a></li><li><a href="Bravey.FreeTextEntityRecognizer.html">FreeTextEntityRecognizer</a></li><li><a href="Bravey.Language.EN.DateEntityRecognizer.html">DateEntityRecognizer</a></li><li><a href="Bravey.Language.EN.FreeTextEntityRecognizer.html">FreeTextEntityRecognizer</a></li><li><a href="Bravey.Language.EN.NumberEntityRecognizer.html">NumberEntityRecognizer</a></li><li><a href="Bravey.Language.EN.Stemmer.html">Stemmer</a></li><li><a href="Bravey.Language.EN.TimeEntityRecognizer.html">TimeEntityRecognizer</a></li><li><a href="Bravey.Language.EN.TimePeriodEntityRecognizer.html">TimePeriodEntityRecognizer</a></li><li><a href="Bravey.Language.IT.DateEntityRecognizer.html">DateEntityRecognizer</a></li><li><a href="Bravey.Language.IT.FreeTextEntityRecognizer.html">FreeTextEntityRecognizer</a></li><li><a href="Bravey.Language.IT.NumberEntityRecognizer.html">NumberEntityRecognizer</a></li><li><a href="Bravey.Language.IT.Stemmer.html">Stemmer</a></li><li><a href="Bravey.Language.IT.TimeEntityRecognizer.html">TimeEntityRecognizer</a></li><li><a href="Bravey.Language.IT.TimePeriodEntityRecognizer.html">TimePeriodEntityRecognizer</a></li><li><a href="Bravey.Language.PT.DateEntityRecognizer.html">DateEntityRecognizer</a></li><li><a href="Bravey.Language.PT.FreeTextEntityRecognizer.html">FreeTextEntityRecognizer</a></li><li><a href="Bravey.Language.PT.NumberEntityRecognizer.html">NumberEntityRecognizer</a></li><li><a href="Bravey.Language.PT.Stemmer.html">Stemmer</a></li><li><a href="Bravey.Language.PT.TimeEntityRecognizer.html">TimeEntityRecognizer</a></li><li><a href="Bravey.Language.PT.TimePeriodEntityRecognizer.html">TimePeriodEntityRecognizer</a></li><li><a href="Bravey.Nlp.Fuzzy.html">Fuzzy</a></li><li><a href="Bravey.Nlp.Sequential.html">Sequential</a></li><li><a href="Bravey.NumberEntityRecognizer.html">NumberEntityRecognizer</a></li><li><a href="Bravey.RegexEntityRecognizer.html">RegexEntityRecognizer</a></li><li><a href="Bravey.SessionManager.InMemorySessionManager.html">InMemorySessionManager</a></li><li><a href="Bravey.StringEntityRecognizer.html">StringEntityRecognizer</a></li><li><a href="Bravey.Text.RegexMap.html">RegexMap</a></li></ul><h3>Namespaces</h3><ul><li><a href="Bravey.html">Bravey</a></li><li><a href="Bravey.Data.html">Data</a></li><li><a href="Bravey.Date.html">Date</a></li><li><a href="Bravey.File.html">File</a></li><li><a href="Bravey.Filter.html">Filter</a></li><li><a href="Bravey.Language.html">Language</a></li><li><a href="Bravey.Language.EN.html">EN</a></li><li><a href="Bravey.Language.IT.html">IT</a></li><li><a href="Bravey.Language.PT.html">PT</a></li><li><a href="Bravey.Nlp.html">Nlp</a></li><li><a href="Bravey.SessionManager.html">SessionManager</a></li><li><a href="Bravey.Text.html">Text</a></li></ul><h3><a href="global.html">Global</a></h3>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a>
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>