From fc5034d26ab5a422abb2c3e638f41fe5a1e51e36 Mon Sep 17 00:00:00 2001 From: Calvin Montgomery Date: Tue, 2 Jul 2013 23:19:17 -0400 Subject: [PATCH] I think I'm on the right track here --- channel.js | 2 +- playlist.js | 112 +++++++++++++++++++++++++++++++--------------------- user.js | 2 +- 3 files changed, 68 insertions(+), 48 deletions(-) diff --git a/channel.js b/channel.js index 4fd1a184..e653bad8 100644 --- a/channel.js +++ b/channel.js @@ -165,7 +165,7 @@ Channel.prototype.loadDump = function() { if(e.temp !== undefined) { p.temp = e.temp; } - this.playlist.append(p); + this.playlist.items.append(p); } this.sendAll("playlist", this.playlist.items.toArray()); if(this.playlist.current) diff --git a/playlist.js b/playlist.js index 1b0de13e..c2d528ab 100644 --- a/playlist.js +++ b/playlist.js @@ -61,9 +61,18 @@ Playlist.prototype.queueAction = function(data) { this._qaInterval = setInterval(function() { if(!pl.lock) { var data = pl.alter_queue.shift(); - pl[data.fn].apply(pl, data.args); + if(data.waiting) { + if(!("expire" in data)) + data.expire = Date.now() + 5000; + if(Date.now() < data.expire) + pl.alter_queue.unshift(data); + return; + } + //pl[data.fn].apply(pl, data.args); + data.fn(); if(pl.alter_queue.length == 0) { clearInterval(pl._qaInterval); + pl._qaInterval = false; } } }, 100); @@ -73,7 +82,7 @@ Playlist.prototype.dump = function() { var arr = this.items.toArray(); var pos = 0; for(var i in arr) { - if(arr[i].uid == this.current.uid) { + if(this.current && arr[i].uid == this.current.uid) { pos = i; break; } @@ -128,23 +137,23 @@ Playlist.prototype.makeItem = function(media) { } Playlist.prototype.add = function(item, pos) { + var success; if(pos == "append") - return this.items.append(item); + success = this.items.append(item); else if(pos == "prepend") - return this.items.prepend(item); + success = this.items.prepend(item); else - return this.items.insertAfter(item, pos); + success = this.items.insertAfter(item, pos); + + if(success && this.items.length == 1) { + this.current = item; + this.startPlayback(); + } + + return success; } Playlist.prototype.addMedia = function(data, callback) { - if(this.lock) { - this.queueAction({ - fn: "addMedia", - args: arguments - }); - return; - } - this.lock = true; var pos = "append"; if(data.pos == "next") { if(!this.current) @@ -153,55 +162,66 @@ Playlist.prototype.addMedia = function(data, callback) { pos = this.current.uid; } + var it = this.makeItem(null); var pl = this; + var action = { + fn: function() { + if(pl.add(it, pos)) + callback(false, it); + }, + waiting: true + }; + this.queueAction(action); + InfoGetter.getMedia(data.id, data.type, function(err, media) { if(err) { + action.expire = 0; callback(err, null); - pl.lock = false; return; } - var it = pl.makeItem(media); + it.media = media; it.temp = data.temp; it.queueby = data.queueby; - if(!pl.add(it, pos)) - callback(true, null); - else - callback(false, it); - pl.lock = false; + action.waiting = false; + }); +} + +Playlist.prototype.addMediaList = function(data, callback) { + if(data.pos == "next") + data.list = data.list.reverse(); + + var pl = this; + data.list.forEach(function(x) { + x.pos = data.pos; + pl.addMedia(x, callback); }); } Playlist.prototype.remove = function(uid, callback) { - if(this.lock) { - this.queueAction({ - fn: "remove", - args: arguments - }); - return; - } - this.lock = true; - var item = this.items.find(uid); - if(this.items.remove(uid)) { - if(item == this.current) - this._next(); - if(callback) - callback(); - } - this.lock = false; + var pl = this; + this.queueAction({ + fn: function() { + var item = pl.items.find(uid); + if(pl.items.remove(uid)) { + if(item == pl.current) + pl._next(); + if(callback) + callback(); + } + }, + waiting: false + }); } Playlist.prototype.move = function(from, after, callback) { - if(this.lock) { - this.queueAction({ - fn: "move", - args: arguments - }); - return; - } - this.lock = true; - this._move(from, after, callback); - this.lock = false; + var pl = this; + this.queueAction({ + fn: function() { + pl._move(from, after, callback); + }, + waiting: false + }); } Playlist.prototype._move = function(from, after, callback) { diff --git a/user.js b/user.js index 4653e110..d6a84ff5 100644 --- a/user.js +++ b/user.js @@ -484,7 +484,7 @@ User.prototype.initCallbacks = function() { return; } - var pl = this.channel.playlist.toArray(); + var pl = this.channel.playlist.items.toArray(); var result = Database.saveUserPlaylist(pl, this.name, data.name); this.socket.emit("savePlaylist", { success: result,