Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Re-use same Javascript object for sessions

Allocating several session pointers from C causes some unstability
(segfaults) so each time we construct a session from javascript, we
actually clean the previous session object, give it a different ID and
reuse that object
  • Loading branch information...
commit a1260066fdd2b9245b72158d78af4468f38e82fb 1 parent 56eea54
@Floby authored
Showing with 25 additions and 42 deletions.
  1. +15 −13 lib/Session.js
  2. +10 −28 src/session.cc
  3. +0 −1  test/test-010-session02-login.js
View
28 lib/Session.js
@@ -24,9 +24,16 @@ var defaultSessionConfig = {
function Session (params) {
if (Session.currentSession) {
- throw new Error('A spotify session is currently in use. libspotify does not support multiple sessions');
+ Session.currentSession.close();
+ Session.currentSession._id++;
+ Session.currentSession.removeAllListeners();
+ Session.currentSession._setupNativeCallbacks();
+ Session.currentSession._startProcessingEvents();
+ return Session.currentSession;
}
+ this._id = 0;
+
params = params || {};
var cparams = new Object(defaultSessionConfig);
for(var key in params) {
@@ -59,34 +66,29 @@ util.inherits(Session, EventEmitter);
Session.prototype._setupNativeCallbacks = function _setupNativeCallbacks() {
var self = this;
+ var session_id = this._id;
this._sp_session.logged_in = function(err) {
- console.log('JS LOGIN CALLBACK!!!');
+ if(self._id != session_id) return;
self.emit('login', err);
}
this._sp_session.logged_out = function() {
- console.log('JS LOGOUT CALLBACK')
+ if(self._id != session_id) return;
self.emit('logout');
}
};
Session.prototype._startProcessingEvents = function _startProcessingEvents() {
+ if(this._process_events_timeout) return;
var self = this;
+ var id = this._id;
this._process_events_timeout = setInterval(function() {
- console.log('processing events', self.name);
+ console.log('processing events', id);
+ if(id != self._id) return;
var next_timeout = b.session_process_events(self._sp_session);
}, 800);
};
Session.prototype.close = function close() {
- if(this._sp_session) {
- //console.log('there was a session to release')
- b.session_release(this._sp_session);
- this._sp_session = null;
- }
- if(Session.currentSession === this) {
- Session.currentSession = null;
- }
- console.log('clearing interval');
clearInterval(this._process_events_timeout);
this._process_events_timeout = null;
};
View
38 src/session.cc
@@ -31,7 +31,6 @@ static void call_logged_in_callback(sp_session* session, sp_error error) {
Handle<Object> o = s->object;
Handle<Value> cbv = o->Get(String::New("logged_in"));
if(!cbv->IsFunction()) {
- fprintf(stderr, "NOT A FUNCTION\n");
return;
}
@@ -43,7 +42,6 @@ static void call_logged_in_callback(sp_session* session, sp_error error) {
}
Local<Value> argv[argc] = { Local<Value>::New(err) };
cb->Call(Context::GetCurrent()->Global(), argc, argv);
- fprintf(stderr, "KIKOO LOGIN\n");
return;
}
@@ -52,7 +50,6 @@ static void call_logged_out_callback(sp_session* session) {
Handle<Object> o = s->object;
Handle<Value> cbv = o->Get(String::New("logged_out"));
if(!cbv->IsFunction()) {
- fprintf(stderr, "NOT A FUNCTION\n");
return;
}
@@ -60,7 +57,6 @@ static void call_logged_out_callback(sp_session* session) {
const unsigned int argc = 0;
Local<Value> argv[argc] = {};
cb->Call(Context::GetCurrent()->Global(), argc, argv);
- fprintf(stderr, "KIKOO LOGOUT\n");
return;
}
@@ -177,40 +173,29 @@ static Handle<Value> Session_Config(const Arguments& args) {
return scope.Close(session_config->object);
}
-static sp_session_config spconfig = {
- .api_version = SPOTIFY_API_VERSION,
- .cache_location = "tmp",
- .settings_location = "tmp",
- .application_key = NULL,
- .application_key_size = 0,
- .user_agent = "spot",
- .callbacks = &spcallbacks,
-};
+//static sp_session_config spconfig = {
+ //.api_version = SPOTIFY_API_VERSION,
+ //.cache_location = "tmp",
+ //.settings_location = "tmp",
+ //.application_key = NULL,
+ //.application_key_size = 0,
+ //.user_agent = "spot",
+ //.callbacks = &spcallbacks,
+//};
static Handle<Value> Session_Create(const Arguments& args) {
HandleScope scope;
- fprintf(stderr, "SIZEOF config %d\n", sizeof(sp_session_config));
- fprintf(stderr, "SIZEOF callbacks %d\n", sizeof(sp_session_callbacks));
- fprintf(stderr, "SIZEOF config wrapper %d\n", sizeof(ObjectHandle<sp_session_config>));
assert(args.Length() == 1);
ObjectHandle<sp_session>* session = new ObjectHandle<sp_session>("sp_session");
- fprintf(stderr, "CREATED HANDLE at %x\n", session);
ObjectHandle<sp_session_config>* session_config = ObjectHandle<sp_session_config>::Unwrap(args[0]);
- spconfig.application_key = session_config->pointer->application_key;
- spconfig.application_key_size = session_config->pointer->application_key_size;
- spconfig.userdata = session;
-
-
session_config->pointer->userdata = session;
- //sp_error error = sp_session_create(session_config->pointer, &session->pointer);
- sp_error error = sp_session_create(&spconfig, &(session->pointer));
+ sp_error error = sp_session_create(session_config->pointer, &session->pointer);
NSP_THROW_IF_ERROR(error);
- fprintf(stderr, "CREATED SESSION at %x IN HANDLE %x (%x)\n", session->pointer, session, &session->pointer);
return scope.Close(session->object);
}
@@ -222,13 +207,10 @@ static Handle<Value> Session_Release(const Arguments& args) {
ObjectHandle<sp_session>* session = ObjectHandle<sp_session>::Unwrap(args[0]);
assert(NULL != session->pointer);
- fprintf(stderr, "RELEASING %x FROM HANDLE %x (%x)\n", session->pointer, session, &session->pointer);
sp_error error = sp_session_release(session->pointer);
- fprintf(stderr, "RELEAED %x\n", session->pointer);
NSP_THROW_IF_ERROR(error);
session->pointer = NULL;
- fprintf(stderr, "NEW VALUE %x\n", session->pointer);
return scope.Close(Undefined());
}
View
1  test/test-010-session02-login.js
@@ -19,7 +19,6 @@ exports.testLoginIsSucessful = function(test) {
session.name = 'chose';
session.login(cred.login, cred.password);
session.on('login', function(err) {
- console.log('ON TEST LOGIN CALLBACK');
test.equal(err, null, 'There should be no error');
session.logout(function() {
session.close();
Please sign in to comment.
Something went wrong with that request. Please try again.