Skip to content

Commit

Permalink
💥 The Alchemy.ClientSession class should now be used as a map to st…
Browse files Browse the repository at this point in the history
…ore session data
  • Loading branch information
skerit committed Jan 29, 2024
1 parent 66810cb commit 6a99c18
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 4 additions & 4 deletions lib/app/conduit/loopback_conduit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
* @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;
});

/**
Expand Down
18 changes: 9 additions & 9 deletions lib/class/conduit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2359,7 +2359,7 @@ Conduit.setMethod(function _sendStream(stream, options) {
*
* @author Jelle De Loecker <[email protected]>
* @since 0.2.0
* @version 1.3.18
* @version 1.4.0
*
* @param {boolean} create Create a session if none exist
*
Expand All @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -2468,7 +2468,7 @@ Conduit.setMethod(function registerBindings(arr) {
*
* @author Jelle De Loecker <[email protected]>
* @since 0.2.0
* @version 0.4.0
* @version 1.4.0
*
* @param {string} name
* @param {Mixed} value
Expand All @@ -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);
});

/**
Expand Down
36 changes: 35 additions & 1 deletion lib/class/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var data_listeners = alchemy.shared('data_binding_listeners');
*
* @author Jelle De Loecker <[email protected]>
* @since 0.2.0
* @version 1.3.1
* @version 1.4.0
*
* @param {Conduit} conduit The initializing conduit
*/
Expand All @@ -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;

Expand Down Expand Up @@ -135,6 +138,37 @@ Session.setProperty(function is_active() {
return false;
});

/**
* Set a value in the session
*
* @author Jelle De Loecker <[email protected]>
* @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 <[email protected]>
* @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
*
Expand Down

0 comments on commit 6a99c18

Please sign in to comment.