From 478bd76aeb44b3d7bebfaaea45e4d75cc20e02b9 Mon Sep 17 00:00:00 2001 From: Tom Date: Thu, 5 Dec 2024 01:05:58 +0000 Subject: [PATCH] Added session & session manager --- models/session.js | 44 +++++++++++++++++++++++++++++++++++++ services/session-manager.js | 33 ++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 models/session.js create mode 100644 services/session-manager.js diff --git a/models/session.js b/models/session.js new file mode 100644 index 0000000..a129cf6 --- /dev/null +++ b/models/session.js @@ -0,0 +1,44 @@ +class Session { + constructor(id) { + this._id = id; + this._started = Date.now(); + this._current = null; + this._previous = null; + this._lastScrobble = null; + this._pauseDuration = 0; + } + + get id() { + return this._id; + } + + get playing() { + return this._current; + } + + set playing(value) { + this._current = value; + } + + get started() { + return this._started; + } + + get lastScrobbleTimestamp() { + return this._lastScrobble; + } + + set lastScrobbleTimestamp(value) { + this._lastScrobble = value; + } + + get pauseDuration() { + return this._pauseDuration; + } + + set pauseDuration(value) { + this._pauseDuration = value; + } +} + +module.exports = Session; \ No newline at end of file diff --git a/services/session-manager.js b/services/session-manager.js new file mode 100644 index 0000000..3fd4276 --- /dev/null +++ b/services/session-manager.js @@ -0,0 +1,33 @@ +class SessionManager { + constructor() { + this._sessions = {}; + } + + add(session) { + if (!session || !session.id) + return; + + this._sessions[session.id] = session; + } + + contains(sessionId) { + return this._sessions[sessionId] != null; + } + + get(sessionId) { + return this._sessions[sessionId]; + } + + getSessionIds() { + return Object.keys(this._sessions); + } + + remove(sessionId) { + if (!sessionId) + return; + + delete this._sessions[sessionId]; + } +} + +module.exports = new SessionManager(); \ No newline at end of file