Skip to content

Commit 9650240

Browse files
committed
Move Session methods onto prototype so that it can be cloned
Functions cannot be cloned by the HTML5 structured clone algorithm. This prevents the session being able to be written to an IndexedDb object store.
1 parent a658383 commit 9650240

File tree

1 file changed

+20
-28
lines changed

1 file changed

+20
-28
lines changed

src/Session.js

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,38 +25,30 @@ import SessionState from "./SessionState";
2525
* @param {Session|Object} [session] - another session. If passed, this session will clone its state.
2626
* @constructor
2727
*/
28-
function Session(session) {
29-
var self = this;
30-
31-
var states = [];
32-
33-
if (session) {
34-
for (let state of session.states) {
35-
states.push(new SessionState(state));
28+
export default class Session {
29+
constructor(session) {
30+
this.states = [];
31+
if (session) {
32+
for (let state of session.states) {
33+
this.states.push(new SessionState(state));
34+
}
3635
}
36+
Object.seal(this);
3737
}
3838

39-
Object.defineProperty(self, "states", {
40-
get: () => states
41-
});
42-
43-
self.mostRecentState = () => {
44-
return states[0];
45-
};
39+
mostRecentState() {
40+
return this.states[0];
41+
}
4642

47-
self.addState = (state) => {
48-
states.unshift(state);
49-
if (states.length > ProtocolConstants.maximumSessionStatesPerIdentity) {
50-
states.pop();
43+
addState(state) {
44+
this.states.unshift(state);
45+
if (this.states.length > ProtocolConstants.maximumSessionStatesPerIdentity) {
46+
this.states.pop();
5147
}
52-
};
53-
54-
self.removeState = (state) => {
55-
var index = states.indexOf(state);
56-
states.splice(index, 1);
57-
};
48+
}
5849

59-
Object.freeze(this);
50+
removeState(state) {
51+
var index = this.states.indexOf(state);
52+
this.states.splice(index, 1);
53+
}
6054
}
61-
62-
export default Session;

0 commit comments

Comments
 (0)