diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d738d3d..d000027b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * Add default `App` classes * Add `Plugin` class * Refactor loading of requirements +* The `Alchemy.ClientSession` class should now be used as a map to store session data ## 1.3.22 (2023-12-21) diff --git a/lib/app/conduit/loopback_conduit.js b/lib/app/conduit/loopback_conduit.js index 929b3166..902d6a9d 100644 --- a/lib/app/conduit/loopback_conduit.js +++ b/lib/app/conduit/loopback_conduit.js @@ -29,14 +29,14 @@ var LoopConduit = Function.inherits('Alchemy.Conduit', function Loopback(parent_ }); /** - * Refer to the parent conduit for the sessionData property + * Refer to the parent conduit for the session_instance property * * @author Jelle De Loecker * @since 1.3.10 - * @version 1.3.10 + * @version 1.4.0 */ -LoopConduit.setProperty(function sessionData() { - return this.parent.sessionData; +LoopConduit.setProperty(function session_instance() { + return this.parent.session_instance; }); /** diff --git a/lib/class/conduit.js b/lib/class/conduit.js index 4422378b..4aa32f5e 100644 --- a/lib/class/conduit.js +++ b/lib/class/conduit.js @@ -2359,7 +2359,7 @@ Conduit.setMethod(function _sendStream(stream, options) { * * @author Jelle De Loecker * @since 0.2.0 - * @version 1.3.18 + * @version 1.4.0 * * @param {boolean} create Create a session if none exist * @@ -2368,8 +2368,8 @@ Conduit.setMethod(function _sendStream(stream, options) { Conduit.setMethod(function getSession(allow_create = true) { // Only do this once per request - if (this.sessionData != null) { - return this.sessionData; + if (this.session_instance != null) { + return this.session_instance; } let cookie_name = this.session_cookie_name, @@ -2395,7 +2395,7 @@ Conduit.setMethod(function getSession(allow_create = true) { } if (session) { - this.sessionData = session; + this.session_instance = session; session.request_count++; } else { return false; @@ -2468,7 +2468,7 @@ Conduit.setMethod(function registerBindings(arr) { * * @author Jelle De Loecker * @since 0.2.0 - * @version 0.4.0 + * @version 1.4.0 * * @param {string} name * @param {Mixed} value @@ -2477,17 +2477,17 @@ Conduit.setMethod(function registerBindings(arr) { */ Conduit.setMethod(function session(name, value) { - this.getSession(); + const session = this.getSession(); if (arguments.length === 0) { - return this.sessionData; + return session; } if (arguments.length === 1) { - return this.sessionData[name]; + return session.get(name); } - this.sessionData[name] = value; + session.set(name, value); }); /** diff --git a/lib/class/session.js b/lib/class/session.js index a29413e1..504d9861 100644 --- a/lib/class/session.js +++ b/lib/class/session.js @@ -5,7 +5,7 @@ var data_listeners = alchemy.shared('data_binding_listeners'); * * @author Jelle De Loecker * @since 0.2.0 - * @version 1.3.1 + * @version 1.4.0 * * @param {Conduit} conduit The initializing conduit */ @@ -14,6 +14,9 @@ var Session = Function.inherits('Alchemy.Base', function ClientSession(conduit) // Register the creation date this.created = new Date(); + // The values in the session + this.values = new Map(); + // Register when the last scene connected this.last_scene_connection = null; @@ -135,6 +138,37 @@ Session.setProperty(function is_active() { return false; }); +/** + * Set a value in the session + * + * @author Jelle De Loecker + * @since 1.4.0 + * @version 1.4.0 + * + * @param {*} key + * @param {*} value + */ +Session.setMethod(function set(key, value) { + if (value === undefined) { + this.values.delete(key); + } else { + this.values.set(key, value); + } +}); + +/** + * Get a value from the session + * + * @author Jelle De Loecker + * @since 1.4.0 + * @version 1.4.0 + * + * @param {*} key + */ +Session.setMethod(function get(key) { + return this.values.get(key); +}); + /** * Add the amount of time this client has been in the queue *